From 95c21deb51e3c8845d44dd2664c37f725f234be5 Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Thu, 15 May 2025 15:59:13 +0200 Subject: [PATCH 1/9] Fix aborted retryable jobs --- src/Queue.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Queue.php b/src/Queue.php index 8182460e2..d57e9d253 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -225,6 +225,11 @@ public function getWorkerPid() protected function handleMessage($id, $message, $ttr, $attempt) { list($job, $error) = $this->unserializeMessage($message); + + if ($attempt > 1 && !($job instanceof RetryableJobInterface)) { + return false; + } + $event = new ExecEvent([ 'id' => $id, 'job' => $job, From 83fac4c1abc9ccb339653748172f807af89cd14a Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Mon, 19 May 2025 09:48:46 +0200 Subject: [PATCH 2/9] Added comments --- src/Queue.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Queue.php b/src/Queue.php index d57e9d253..ce631caf9 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -226,8 +226,14 @@ protected function handleMessage($id, $message, $ttr, $attempt) { list($job, $error) = $this->unserializeMessage($message); - if ($attempt > 1 && !($job instanceof RetryableJobInterface)) { - return false; + // Handle aborted jobs without thrown error + if ($attempt > 1) { + if ($job instanceof RetryableJobInterface && !$job->canRetry($attempt, $error)) { + return true; + } else { + // Non RetryableJobs can have a maximum of one attempt + return true; + } } $event = new ExecEvent([ From 2c5d0ffbf85d2f32bc118aba2ec03dddf63e7641 Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Mon, 19 May 2025 10:13:36 +0200 Subject: [PATCH 3/9] Reduce attempt count before checking canRetry --- src/Queue.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Queue.php b/src/Queue.php index ce631caf9..d80180f13 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -228,7 +228,7 @@ protected function handleMessage($id, $message, $ttr, $attempt) // Handle aborted jobs without thrown error if ($attempt > 1) { - if ($job instanceof RetryableJobInterface && !$job->canRetry($attempt, $error)) { + if ($job instanceof RetryableJobInterface && !$job->canRetry($attempt - 1, $error)) { return true; } else { // Non RetryableJobs can have a maximum of one attempt From 18e342f6eca7f2b861cf471e7ee8582e0e00aba7 Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Wed, 21 May 2025 22:12:13 +0200 Subject: [PATCH 4/9] Handle use queue default attempts --- src/Queue.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Queue.php b/src/Queue.php index d80180f13..f9db11ed2 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -228,10 +228,9 @@ protected function handleMessage($id, $message, $ttr, $attempt) // Handle aborted jobs without thrown error if ($attempt > 1) { - if ($job instanceof RetryableJobInterface && !$job->canRetry($attempt - 1, $error)) { + if ($job instanceof RetryableJobInterface && !$job->canRetry($attempt, $error)) { return true; - } else { - // Non RetryableJobs can have a maximum of one attempt + } elseif ($attempt > $this->attempts) { return true; } } From 14bfe7014868a123f5c6a4debe95680627a6f9dc Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Wed, 21 May 2025 23:01:48 +0200 Subject: [PATCH 5/9] Added to CHANGELOG --- CHANGELOG.md | 2 +- src/Queue.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bdf0fc75..5e4b7b901 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ Yii2 Queue Extension Change Log ----------------------- - Enh #516: Ensure Redis driver messages are consumed at least once (soul11201) - Bug #522: Fix SQS driver type error with custom value passed to `queue/listen` (flaviovs) - +- Bug #528: Prevent multiple execution of aborted jobs 2.3.7 April 29, 2024 -------------------- diff --git a/src/Queue.php b/src/Queue.php index f9db11ed2..fbc23ec5a 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -228,7 +228,7 @@ protected function handleMessage($id, $message, $ttr, $attempt) // Handle aborted jobs without thrown error if ($attempt > 1) { - if ($job instanceof RetryableJobInterface && !$job->canRetry($attempt, $error)) { + if ($job instanceof RetryableJobInterface && !$job->canRetry($attempt - 1, $error)) { return true; } elseif ($attempt > $this->attempts) { return true; From 1cbc2786ed2592d9d1375624e3499633835f1a9e Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Wed, 21 May 2025 23:15:18 +0200 Subject: [PATCH 6/9] Fix condition for non retryable jobs --- src/Queue.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Queue.php b/src/Queue.php index fbc23ec5a..9a3376e4f 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -230,7 +230,7 @@ protected function handleMessage($id, $message, $ttr, $attempt) if ($attempt > 1) { if ($job instanceof RetryableJobInterface && !$job->canRetry($attempt - 1, $error)) { return true; - } elseif ($attempt > $this->attempts) { + } elseif (!($job instanceof RetryableJobInterface) && $attempt > $this->attempts) { return true; } } From 99d9f892df7a340aaae5e61f6c23361c0eea8243 Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Thu, 29 May 2025 17:20:41 +0200 Subject: [PATCH 7/9] Combined Conditions --- src/Queue.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Queue.php b/src/Queue.php index 9a3376e4f..fb8370c1b 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -227,12 +227,10 @@ protected function handleMessage($id, $message, $ttr, $attempt) list($job, $error) = $this->unserializeMessage($message); // Handle aborted jobs without thrown error - if ($attempt > 1) { - if ($job instanceof RetryableJobInterface && !$job->canRetry($attempt - 1, $error)) { - return true; - } elseif (!($job instanceof RetryableJobInterface) && $attempt > $this->attempts) { - return true; - } + if ($attempt > 1 && + (($job instanceof RetryableJobInterface && !$job->canRetry($attempt - 1, $error)) + || (!($job instanceof RetryableJobInterface) && $attempt > $this->attempts))) { + return true; } $event = new ExecEvent([ From 77feac5049f25c18160825bfd5b31cc7bcac027f Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Fri, 30 May 2025 08:30:50 +0200 Subject: [PATCH 8/9] Update CHANGELOG.md Co-authored-by: Alexander Makarov --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e4b7b901..541d80ee0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ Yii2 Queue Extension Change Log ----------------------- - Enh #516: Ensure Redis driver messages are consumed at least once (soul11201) - Bug #522: Fix SQS driver type error with custom value passed to `queue/listen` (flaviovs) -- Bug #528: Prevent multiple execution of aborted jobs +- Bug #528: Prevent multiple execution of aborted jobs (luke-) 2.3.7 April 29, 2024 -------------------- From d76bdc6c470b7af561ac4a51de1e46af482d6a6d Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Fri, 30 May 2025 08:31:01 +0200 Subject: [PATCH 9/9] Update src/Queue.php Co-authored-by: Alexander Makarov --- src/Queue.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Queue.php b/src/Queue.php index fb8370c1b..122bdb313 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -226,7 +226,7 @@ protected function handleMessage($id, $message, $ttr, $attempt) { list($job, $error) = $this->unserializeMessage($message); - // Handle aborted jobs without thrown error + // Handle aborted jobs without throwing an error. if ($attempt > 1 && (($job instanceof RetryableJobInterface && !$job->canRetry($attempt - 1, $error)) || (!($job instanceof RetryableJobInterface) && $attempt > $this->attempts))) {