Skip to content

Commit 7bbd8f2

Browse files
committed
improve migrate/cleanup handling + refactoring
1 parent 946be51 commit 7bbd8f2

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/Jobs/SetupIndex.php

+36-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Limenet\LaravelElasticaBridge\Jobs;
66

7+
use Elastica\Exception\ClientException;
8+
use Elastica\Exception\ConnectionException;
9+
use Elastica\Exception\ResponseException;
710
use Illuminate\Bus\Batchable;
811
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
912
use Limenet\LaravelElasticaBridge\Exception\Index\BlueGreenIndicesIncorrectlySetupException;
@@ -24,14 +27,42 @@ public function handle(ElasticaClient $elastica): void
2427
if ($this->batch()?->cancelled()) {
2528
return;
2629
}
30+
$this->migrate($elastica);
31+
$this->cleanup($elastica);
32+
$this->setup($elastica);
33+
}
34+
35+
private function migrate(ElasticaClient $elastica): void
36+
{
37+
if (!$this->indexConfig->hasBlueGreenIndices()) {
38+
return;
39+
}
2740

28-
if ($this->deleteExisting && $this->indexConfig->hasBlueGreenIndices()) {
29-
$index = $elastica->getClient()->getIndex($this->indexConfig->getName());
41+
$index = $elastica->getClient()->getIndex($this->indexConfig->getName());
42+
43+
try {
44+
$response = $elastica->getClient()->request(sprintf('_alias/%s', $this->indexConfig->getName()));
45+
} catch (ClientException|ConnectionException|ResponseException) {
3046
if (count($index->getAliases()) === 0) {
3147
$index->delete();
3248
}
49+
50+
return;
51+
}
52+
53+
if ($response->hasError()) {
54+
return;
55+
}
56+
57+
if (array_keys($response->getData())[0] !== $this->indexConfig->getName()) {
58+
return;
3359
}
3460

61+
$index->delete();
62+
}
63+
64+
private function cleanup(ElasticaClient $elastica): void
65+
{
3566
foreach (IndexInterface::INDEX_SUFFIXES as $suffix) {
3667
$name = $this->indexConfig->getName().$suffix;
3768
$aliasIndex = $elastica->getIndex($name);
@@ -44,7 +75,10 @@ public function handle(ElasticaClient $elastica): void
4475
$aliasIndex->create($this->indexConfig->getCreateArguments());
4576
}
4677
}
78+
}
4779

80+
private function setup(ElasticaClient $elastica): void
81+
{
4882
try {
4983
$this->indexConfig->getBlueGreenActiveSuffix();
5084
} catch (BlueGreenIndicesIncorrectlySetupException $exception) {

0 commit comments

Comments
 (0)