From 042d59cff664f6be8a0a372ecbf01e457b41d9ec Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Mon, 15 Jan 2024 16:51:00 +0100 Subject: [PATCH 1/2] fix: add pessimistic lock when fetching next qs batch --- app/Http/Controllers/Backend/QsController.php | 1 + app/Jobs/CreateQueryserviceBatchesJob.php | 4 +++- app/Jobs/RequeuePendingQsBatchesJob.php | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Backend/QsController.php b/app/Http/Controllers/Backend/QsController.php index 9c5e3af53..3beaa570e 100644 --- a/app/Http/Controllers/Backend/QsController.php +++ b/app/Http/Controllers/Backend/QsController.php @@ -19,6 +19,7 @@ public function getBatches(Request $request): \Illuminate\Http\Response ['failed', '=', false] ]) ->orderBy('id') + ->lockForUpdate() ->first(); if ($oldestBatch === null) { diff --git a/app/Jobs/CreateQueryserviceBatchesJob.php b/app/Jobs/CreateQueryserviceBatchesJob.php index 1c99abd57..a5a90ead4 100644 --- a/app/Jobs/CreateQueryserviceBatchesJob.php +++ b/app/Jobs/CreateQueryserviceBatchesJob.php @@ -77,7 +77,9 @@ private function tryToAppendEntitesToExistingBatches(array $entityIdsFromEvents, ['pending_since', '=', null], ['failed', '=', false], ['wiki_id', '=', $wikiId], - ])->get(); + ]) + ->lockForUpdate() + ->get(); foreach ($notDoneBatches as $qsBatch) { $entitiesOnBatch = explode(',', $qsBatch->entityIds); diff --git a/app/Jobs/RequeuePendingQsBatchesJob.php b/app/Jobs/RequeuePendingQsBatchesJob.php index 5a6358ba5..d630b65ea 100644 --- a/app/Jobs/RequeuePendingQsBatchesJob.php +++ b/app/Jobs/RequeuePendingQsBatchesJob.php @@ -35,6 +35,7 @@ private function markBatchesFailed(): array ['done', '=', false] ]) ->select('id') + ->lockForUpdate() ->get() ->pluck('id') ->toArray(); From 2e9e8310675155803dc6f6d200c2933d0a91203e Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Thu, 18 Jan 2024 15:51:03 +0100 Subject: [PATCH 2/2] feat: ensure child method is only run in transaction --- app/Jobs/CreateQueryserviceBatchesJob.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/Jobs/CreateQueryserviceBatchesJob.php b/app/Jobs/CreateQueryserviceBatchesJob.php index a5a90ead4..0173aaccd 100644 --- a/app/Jobs/CreateQueryserviceBatchesJob.php +++ b/app/Jobs/CreateQueryserviceBatchesJob.php @@ -72,6 +72,12 @@ private function getNewEntities(int $latestCheckpoint): array private function tryToAppendEntitesToExistingBatches(array $entityIdsFromEvents, int $wikiId): bool { + if (DB::transactionLevel() < 1) { + throw new \Exception( + 'This method can only be run within the context of a transaction.' + ); + } + $notDoneBatches = QsBatch::where([ ['done', '=', 0], ['pending_since', '=', null],