Skip to content

Commit 68f3a55

Browse files
committed
Prevent ElasticSearchAliasInit jobs overlapping
Elasticsearch can only process cluster state changes, such as adding new aliases, sequentially on the master node. Runing multiple instances of this job (e.g. with the `EnsureElasticSearchAliases` command) can overload the Elasticsearch master node causing Jobs to fail. Prevent multiple `ElasticSearchAliasInit` jobs from runing at the same time by using the `WithoutOverlapping` middleware [1] [1]: https://laravel.com/docs/10.x/queues#preventing-job-overlaps Bug: T416158
1 parent 8f25277 commit 68f3a55

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

app/Jobs/ElasticSearchAliasInit.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Http\Curl\HttpRequest;
66
use App\WikiDb;
77
use Illuminate\Foundation\Bus\Dispatchable;
8+
use Illuminate\Queue\Middleware\WithoutOverlapping;
89
use Illuminate\Support\Facades\Log;
910

1011
class ElasticSearchAliasInit extends Job {
@@ -24,6 +25,18 @@ public function __construct(int $wikiId, string $esHost, ?string $sharedPrefix =
2425
$this->sharedPrefix = $sharedPrefix ?? getenv('ELASTICSEARCH_SHARED_INDEX_PREFIX');
2526
}
2627

28+
/**
29+
* Get the middleware the job should pass through.
30+
*
31+
* @return array<int, object>
32+
*/
33+
public function middleware(): array {
34+
return [
35+
// Only allow one job per ES host to run at a time to avoid DoSing the ES cluster with alias updates
36+
new WithoutOverlapping("elasticsearch-alias-init-{$this->esHost}"),
37+
];
38+
}
39+
2740
public function handle(HttpRequest $request) {
2841
Log::info(__METHOD__ . ": Updating Elasticsearch aliases for $this->wikiId");
2942

0 commit comments

Comments
 (0)