diff --git a/src/Illuminate/Bus/UniqueLock.php b/src/Illuminate/Bus/UniqueLock.php index dea12303b719..9a2726e9d8a5 100644 --- a/src/Illuminate/Bus/UniqueLock.php +++ b/src/Illuminate/Bus/UniqueLock.php @@ -64,7 +64,7 @@ public function release($job) * @param mixed $job * @return string */ - protected function getKey($job) + public static function getKey($job) { $uniqueId = method_exists($job, 'uniqueId') ? $job->uniqueId() diff --git a/src/Illuminate/Foundation/Bus/PendingDispatch.php b/src/Illuminate/Foundation/Bus/PendingDispatch.php index e1a2e7dca397..4addfe3e0ae3 100644 --- a/src/Illuminate/Foundation/Bus/PendingDispatch.php +++ b/src/Illuminate/Foundation/Bus/PendingDispatch.php @@ -12,6 +12,7 @@ class PendingDispatch { use InteractsWithUniqueJobs; + /** * The job. * @@ -209,10 +210,10 @@ public function __call($method, $parameters) */ public function __destruct() { - $this->rememberLockIfJobIsUnique($this->job); + $this->addUniqueJobInformationToContext($this->job); if (! $this->shouldDispatch()) { - $this->forgetLockIfJobIsUnique($this->job); + $this->removeUniqueJobInformationFromContext($this->job); return; } elseif ($this->afterResponse) { @@ -221,6 +222,6 @@ public function __destruct() app(Dispatcher::class)->dispatch($this->job); } - $this->forgetLockIfJobIsUnique($this->job); + $this->removeUniqueJobInformationFromContext($this->job); } } diff --git a/src/Illuminate/Foundation/Queue/InteractsWithUniqueJobs.php b/src/Illuminate/Foundation/Queue/InteractsWithUniqueJobs.php index 08a843b8eb2b..c517dd092b35 100644 --- a/src/Illuminate/Foundation/Queue/InteractsWithUniqueJobs.php +++ b/src/Illuminate/Foundation/Queue/InteractsWithUniqueJobs.php @@ -2,80 +2,65 @@ namespace Illuminate\Foundation\Queue; -use Illuminate\Cache\Repository; +use Illuminate\Bus\UniqueLock; use Illuminate\Contracts\Queue\ShouldBeUnique; -use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Context; trait InteractsWithUniqueJobs { /** - * Saves the used cache driver for the lock and - * the lock key for emergency forceRelease in - * case we can't instantiate a job instance. + * Store unique job information in the context in case we can't resolve the job on the queue side. + * + * @param object $job + * @return void */ - public function rememberLockIfJobIsUnique($job): void + public function addUniqueJobInformationToContext($job): void { if ($this->isUniqueJob($job)) { - context()->addHidden([ - 'laravel_unique_job_cache_driver' => $this->getCacheDriver($job), - 'laravel_unique_job_key' => $this->getKey($job), + Context::addHidden([ + 'laravel_unique_job_cache_driver' => $this->getUniqueJobCacheStore($job), + 'laravel_unique_job_key' => UniqueLock::getKey($job), ]); } } /** - * forget the used lock. + * Remove the unique job information from the context. + * + * @param object $job + * @return void */ - public function forgetLockIfJobIsUnique($job): void + public function removeUniqueJobInformationFromContext($job): void { if ($this->isUniqueJob($job)) { - context()->forgetHidden(['laravel_unique_job_cache_driver', 'laravel_unique_job_key']); + Context::forgetHidden([ + 'laravel_unique_job_cache_driver', + 'laravel_unique_job_key', + ]); } } /** - * Determine if job has unique lock. - */ - private function isUniqueJob($job): bool - { - return $job instanceof ShouldBeUnique; - } - - /** - * Get the used cache driver as string from the config, - * CacheManger will handle invalid drivers. + * Determine the cache store used by the unique job to acquire locks. + * + * @param object $job + * @return string */ - private function getCacheDriver($job): ?string + private function getUniqueJobCacheStore($job): ?string { - /** @var \Illuminate\Cache\Repository */ - $cache = method_exists($job, 'uniqueVia') ? - $job->uniqueVia() : - app()->make(Repository::class); - - $store = collect(config('cache')['stores']) - - ->firstWhere( - function ($store) use ($cache) { - return $cache === rescue(fn () => cache()->driver($store['driver'])); - } - ); - - return Arr::get($store, 'driver'); + return method_exists($job, 'uniqueVia') + ? $job->uniqueVia() + : config('cache.default'); } - //NOTE: can I change visibility of the original method in src/Illuminate/Bus/UniqueLock.php ? /** - * Generate the lock key for the given job. + * Determine if job should be unique. * * @param mixed $job - * @return string + * @return bool */ - private function getKey($job) + private function isUniqueJob($job): bool { - $uniqueId = method_exists($job, 'uniqueId') - ? $job->uniqueId() - : ($job->uniqueId ?? ''); - - return 'laravel_unique_job:'.get_class($job).':'.$uniqueId; + return $job instanceof ShouldBeUnique; } }