4
4
5
5
namespace Limenet \LaravelElasticaBridge \Jobs ;
6
6
7
+ use Elastica \Exception \ClientException ;
8
+ use Elastica \Exception \ConnectionException ;
9
+ use Elastica \Exception \ResponseException ;
7
10
use Illuminate \Bus \Batchable ;
8
11
use Limenet \LaravelElasticaBridge \Client \ElasticaClient ;
9
12
use Limenet \LaravelElasticaBridge \Exception \Index \BlueGreenIndicesIncorrectlySetupException ;
@@ -24,14 +27,42 @@ public function handle(ElasticaClient $elastica): void
24
27
if ($ this ->batch ()?->cancelled()) {
25
28
return ;
26
29
}
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
+ }
27
40
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 ) {
30
46
if (count ($ index ->getAliases ()) === 0 ) {
31
47
$ index ->delete ();
32
48
}
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 ;
33
59
}
34
60
61
+ $ index ->delete ();
62
+ }
63
+
64
+ private function cleanup (ElasticaClient $ elastica ): void
65
+ {
35
66
foreach (IndexInterface::INDEX_SUFFIXES as $ suffix ) {
36
67
$ name = $ this ->indexConfig ->getName ().$ suffix ;
37
68
$ aliasIndex = $ elastica ->getIndex ($ name );
@@ -44,7 +75,10 @@ public function handle(ElasticaClient $elastica): void
44
75
$ aliasIndex ->create ($ this ->indexConfig ->getCreateArguments ());
45
76
}
46
77
}
78
+ }
47
79
80
+ private function setup (ElasticaClient $ elastica ): void
81
+ {
48
82
try {
49
83
$ this ->indexConfig ->getBlueGreenActiveSuffix ();
50
84
} catch (BlueGreenIndicesIncorrectlySetupException $ exception ) {
0 commit comments