Skip to content

Commit 827563c

Browse files
committed
prevent overlapping index runs
1 parent 846c265 commit 827563c

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/Commands/IndexCommand.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class IndexCommand extends Command
1616
{
17-
protected $signature = 'elastica-bridge:index {index?*} {--delete}';
17+
protected $signature = 'elastica-bridge:index {index?*} {--delete} {--force}';
1818

1919
protected $description = 'Re-create the ES index and populate with data';
2020

@@ -37,15 +37,31 @@ public function handle(): int
3737

3838
$this->info(sprintf('Indexing %s', $indexConfig->getName()));
3939

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+
4051
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+
],
4356
])
4457
->onConnection(config('elastica-bridge.connection'))
4558
->then(function () use ($indexConfig): void {
4659
ActivateIndex::dispatch($indexConfig)
4760
->onConnection(config('elastica-bridge.connection'));
4861
})
62+
->finally(function () use ($indexConfig): void {
63+
$indexConfig->indexingLock()->forceRelease();
64+
})
4965
->name('ES index: '.$indexConfig->getName())
5066
->dispatch();
5167
}

src/Index/AbstractIndex.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use Elastica\Index;
1010
use Elastica\Query;
1111
use Elastica\ResultSet;
12+
use Illuminate\Contracts\Cache\Lock;
1213
use Illuminate\Database\Eloquent\Model;
14+
use Illuminate\Support\Facades\Cache;
1315
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
1416
use Limenet\LaravelElasticaBridge\Exception\Index\BlueGreenIndicesIncorrectlySetupException;
1517
use Limenet\LaravelElasticaBridge\Model\ElasticsearchableInterface;
@@ -168,4 +170,9 @@ final public function getBlueGreenInactiveElasticaIndex(): Index
168170
{
169171
return $this->client->getIndex($this->getName().$this->getBlueGreenInactiveSuffix());
170172
}
173+
174+
final public function indexingLock(): Lock
175+
{
176+
return Cache::lock(__CLASS__.$this->getName());
177+
}
171178
}

src/Index/IndexInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Elastica\Document;
88
use Elastica\Index;
9+
use Illuminate\Contracts\Cache\Lock;
910
use Illuminate\Database\Eloquent\Model;
1011
use Limenet\LaravelElasticaBridge\Exception\Index\BlueGreenIndicesIncorrectlySetupException;
1112
use Limenet\LaravelElasticaBridge\Model\ElasticsearchableInterface;
@@ -139,4 +140,6 @@ public function getModelInstance(Document $document): Model;
139140
* @return Document
140141
*/
141142
public function getDocumentInstance(Model|ElasticsearchableInterface $model): ?Document;
143+
144+
public function indexingLock(): Lock;
142145
}

0 commit comments

Comments
 (0)