File tree Expand file tree Collapse file tree 3 files changed +29
-3
lines changed Expand file tree Collapse file tree 3 files changed +29
-3
lines changed Original file line number Diff line number Diff line change 14
14
15
15
class IndexCommand extends Command
16
16
{
17
- protected $ signature = 'elastica-bridge:index {index?*} {--delete} ' ;
17
+ protected $ signature = 'elastica-bridge:index {index?*} {--delete} {--force} ' ;
18
18
19
19
protected $ description = 'Re-create the ES index and populate with data ' ;
20
20
@@ -37,15 +37,31 @@ public function handle(): int
37
37
38
38
$ this ->info (sprintf ('Indexing %s ' , $ indexConfig ->getName ()));
39
39
40
+ $ lock = $ indexConfig ->indexingLock ();
41
+
42
+ if ((bool ) $ this ->option ('force ' )) {
43
+ $ lock ->forceRelease ();
44
+ }
45
+
46
+ if (!$ lock ->get ()) {
47
+ $ this ->warn ('Not indexing as another job is still running. ' );
48
+ continue ;
49
+ }
50
+
40
51
Bus::batch ([
41
- [new SetupIndex ($ indexConfig , (bool ) $ this ->option ('delete ' ))],
42
- [new PopulateIndex ($ indexConfig )],
52
+ [
53
+ new SetupIndex ($ indexConfig , (bool ) $ this ->option ('delete ' )),
54
+ new PopulateIndex ($ indexConfig ),
55
+ ],
43
56
])
44
57
->onConnection (config ('elastica-bridge.connection ' ))
45
58
->then (function () use ($ indexConfig ): void {
46
59
ActivateIndex::dispatch ($ indexConfig )
47
60
->onConnection (config ('elastica-bridge.connection ' ));
48
61
})
62
+ ->finally (function () use ($ indexConfig ): void {
63
+ $ indexConfig ->indexingLock ()->forceRelease ();
64
+ })
49
65
->name ('ES index: ' .$ indexConfig ->getName ())
50
66
->dispatch ();
51
67
}
Original file line number Diff line number Diff line change 9
9
use Elastica \Index ;
10
10
use Elastica \Query ;
11
11
use Elastica \ResultSet ;
12
+ use Illuminate \Contracts \Cache \Lock ;
12
13
use Illuminate \Database \Eloquent \Model ;
14
+ use Illuminate \Support \Facades \Cache ;
13
15
use Limenet \LaravelElasticaBridge \Client \ElasticaClient ;
14
16
use Limenet \LaravelElasticaBridge \Exception \Index \BlueGreenIndicesIncorrectlySetupException ;
15
17
use Limenet \LaravelElasticaBridge \Model \ElasticsearchableInterface ;
@@ -168,4 +170,9 @@ final public function getBlueGreenInactiveElasticaIndex(): Index
168
170
{
169
171
return $ this ->client ->getIndex ($ this ->getName ().$ this ->getBlueGreenInactiveSuffix ());
170
172
}
173
+
174
+ final public function indexingLock (): Lock
175
+ {
176
+ return Cache::lock (__CLASS__ .$ this ->getName ());
177
+ }
171
178
}
Original file line number Diff line number Diff line change 6
6
7
7
use Elastica \Document ;
8
8
use Elastica \Index ;
9
+ use Illuminate \Contracts \Cache \Lock ;
9
10
use Illuminate \Database \Eloquent \Model ;
10
11
use Limenet \LaravelElasticaBridge \Exception \Index \BlueGreenIndicesIncorrectlySetupException ;
11
12
use Limenet \LaravelElasticaBridge \Model \ElasticsearchableInterface ;
@@ -139,4 +140,6 @@ public function getModelInstance(Document $document): Model;
139
140
* @return Document
140
141
*/
141
142
public function getDocumentInstance (Model |ElasticsearchableInterface $ model ): ?Document ;
143
+
144
+ public function indexingLock (): Lock ;
142
145
}
You can’t perform that action at this time.
0 commit comments