From 708ed2b85e37c7f0381de1aef40dd5b3b899c128 Mon Sep 17 00:00:00 2001 From: Doug Sisk Date: Wed, 17 Aug 2022 15:57:57 -0400 Subject: [PATCH] [9.x] Move unique lock release (#43740) * Move unique lock release * formatting Co-authored-by: Taylor Otwell --- src/Illuminate/Bus/UniqueLock.php | 39 +++++++++++++++++----- src/Illuminate/Queue/CallQueuedHandler.php | 17 ++-------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/Illuminate/Bus/UniqueLock.php b/src/Illuminate/Bus/UniqueLock.php index d1bd774cfe8e..a4066b77c1c6 100644 --- a/src/Illuminate/Bus/UniqueLock.php +++ b/src/Illuminate/Bus/UniqueLock.php @@ -32,10 +32,6 @@ public function __construct(Cache $cache) */ public function acquire($job) { - $uniqueId = method_exists($job, 'uniqueId') - ? $job->uniqueId() - : ($job->uniqueId ?? ''); - $uniqueFor = method_exists($job, 'uniqueFor') ? $job->uniqueFor() : ($job->uniqueFor ?? 0); @@ -44,9 +40,36 @@ public function acquire($job) ? $job->uniqueVia() : $this->cache; - return (bool) $cache->lock( - $key = 'laravel_unique_job:'.get_class($job).$uniqueId, - $uniqueFor - )->get(); + return (bool) $cache->lock($this->getKey($job), $uniqueFor)->get(); + } + + /** + * Release the lock for the given job. + * + * @param mixed $job + * @return void + */ + public function release($job) + { + $cache = method_exists($job, 'uniqueVia') + ? $job->uniqueVia() + : $this->cache; + + $cache->lock($this->getKey($job))->forceRelease(); + } + + /** + * Generate the lock key for the given job. + * + * @param mixed $job + * @return string + */ + protected function getKey($job) + { + $uniqueId = method_exists($job, 'uniqueId') + ? $job->uniqueId() + : ($job->uniqueId ?? ''); + + return 'laravel_unique_job:'.get_class($job).$uniqueId; } } diff --git a/src/Illuminate/Queue/CallQueuedHandler.php b/src/Illuminate/Queue/CallQueuedHandler.php index 74769b8bba92..22fe9ccab89a 100644 --- a/src/Illuminate/Queue/CallQueuedHandler.php +++ b/src/Illuminate/Queue/CallQueuedHandler.php @@ -4,6 +4,7 @@ use Exception; use Illuminate\Bus\Batchable; +use Illuminate\Bus\UniqueLock; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Contracts\Container\Container; @@ -200,21 +201,9 @@ protected function ensureSuccessfulBatchJobIsRecorded($command) */ protected function ensureUniqueJobLockIsReleased($command) { - if (! $command instanceof ShouldBeUnique) { - return; + if ($command instanceof ShouldBeUnique) { + (new UniqueLock($this->container->make(Cache::class)))->release($command); } - - $uniqueId = method_exists($command, 'uniqueId') - ? $command->uniqueId() - : ($command->uniqueId ?? ''); - - $cache = method_exists($command, 'uniqueVia') - ? $command->uniqueVia() - : $this->container->make(Cache::class); - - $cache->lock( - 'laravel_unique_job:'.get_class($command).$uniqueId - )->forceRelease(); } /**