From 68f3a554a73df8d077a4a7a4471c43956a0d1ab8 Mon Sep 17 00:00:00 2001 From: Ollie Date: Fri, 13 Feb 2026 16:44:34 +0000 Subject: [PATCH] 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 --- app/Jobs/ElasticSearchAliasInit.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/Jobs/ElasticSearchAliasInit.php b/app/Jobs/ElasticSearchAliasInit.php index 1bfc3c09..651448ad 100644 --- a/app/Jobs/ElasticSearchAliasInit.php +++ b/app/Jobs/ElasticSearchAliasInit.php @@ -5,6 +5,7 @@ use App\Http\Curl\HttpRequest; use App\WikiDb; use Illuminate\Foundation\Bus\Dispatchable; +use Illuminate\Queue\Middleware\WithoutOverlapping; use Illuminate\Support\Facades\Log; class ElasticSearchAliasInit extends Job { @@ -24,6 +25,18 @@ public function __construct(int $wikiId, string $esHost, ?string $sharedPrefix = $this->sharedPrefix = $sharedPrefix ?? getenv('ELASTICSEARCH_SHARED_INDEX_PREFIX'); } + /** + * Get the middleware the job should pass through. + * + * @return array + */ + public function middleware(): array { + return [ + // Only allow one job per ES host to run at a time to avoid DoSing the ES cluster with alias updates + new WithoutOverlapping("elasticsearch-alias-init-{$this->esHost}"), + ]; + } + public function handle(HttpRequest $request) { Log::info(__METHOD__ . ": Updating Elasticsearch aliases for $this->wikiId");