Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 17, 2025
1 parent c2a60a7 commit ed33b51
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Bus/UniqueLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Foundation/Bus/PendingDispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class PendingDispatch
{
use InteractsWithUniqueJobs;

/**
* The job.
*
Expand Down Expand Up @@ -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) {
Expand All @@ -221,6 +222,6 @@ public function __destruct()
app(Dispatcher::class)->dispatch($this->job);
}

$this->forgetLockIfJobIsUnique($this->job);
$this->removeUniqueJobInformationFromContext($this->job);
}
}
77 changes: 31 additions & 46 deletions src/Illuminate/Foundation/Queue/InteractsWithUniqueJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit ed33b51

Please sign in to comment.