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..0173aaccd 100644 --- a/app/Jobs/CreateQueryserviceBatchesJob.php +++ b/app/Jobs/CreateQueryserviceBatchesJob.php @@ -72,12 +72,20 @@ 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], ['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();