From f1ca52e7fd9425a5cbe0eedff76851f58983ca47 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Fri, 19 Jun 2020 14:57:45 +0200 Subject: [PATCH 1/2] delete and release db jobs inside a transaction --- src/Illuminate/Queue/DatabaseQueue.php | 20 ++++++++++++++++++++ src/Illuminate/Queue/Jobs/DatabaseJob.php | 14 +++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Queue/DatabaseQueue.php b/src/Illuminate/Queue/DatabaseQueue.php index 0acae600572f..83e945c9096b 100644 --- a/src/Illuminate/Queue/DatabaseQueue.php +++ b/src/Illuminate/Queue/DatabaseQueue.php @@ -321,6 +321,26 @@ public function deleteReserved($queue, $id) }); } + /** + * Delete a reserved job from the reserved queue and release it. + * + * @param string $queue + * @param \Illuminate\Queue\Jobs\DatabaseJob $job + * @param int $delay + * @return void + */ + public function deleteAndRelease($queue, $job, $delay) + { + $this->database->transaction(function () use ($queue, $job, $delay) { + if ($this->database->table($this->table)->lockForUpdate()->find($job->getJobId())) { + $this->database->table($this->table)->where('id', $job->getJobId())->delete(); + } + + $this->release($queue, $job->getJobRecord(), $delay); + }); + } + + /** * Get the queue or return the default. * diff --git a/src/Illuminate/Queue/Jobs/DatabaseJob.php b/src/Illuminate/Queue/Jobs/DatabaseJob.php index 18f3c0696b77..15c8ea32be2b 100644 --- a/src/Illuminate/Queue/Jobs/DatabaseJob.php +++ b/src/Illuminate/Queue/Jobs/DatabaseJob.php @@ -51,9 +51,7 @@ public function release($delay = 0) { parent::release($delay); - $this->delete(); - - return $this->database->release($this->queue, $this->job, $delay); + return $this->database->deleteAndRelease($this->queue, $this, $delay); } /** @@ -97,4 +95,14 @@ public function getRawBody() { return $this->job->payload; } + + /** + * Get the database job record. + * + * @return \Illuminate\Queue\Jobs\DatabaseJobRecord + */ + public function getJobRecord() + { + return $this->job; + } } From f5eac6f58ff770482654f870ec7292cc4add0442 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Fri, 19 Jun 2020 15:07:32 +0200 Subject: [PATCH 2/2] fix style --- src/Illuminate/Queue/DatabaseQueue.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Illuminate/Queue/DatabaseQueue.php b/src/Illuminate/Queue/DatabaseQueue.php index 83e945c9096b..5abb989f8551 100644 --- a/src/Illuminate/Queue/DatabaseQueue.php +++ b/src/Illuminate/Queue/DatabaseQueue.php @@ -340,7 +340,6 @@ public function deleteAndRelease($queue, $job, $delay) }); } - /** * Get the queue or return the default. *