Skip to content

Commit

Permalink
[9.x] Move unique lock release (#43740)
Browse files Browse the repository at this point in the history
* Move unique lock release

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
DougSisk and taylorotwell authored Aug 17, 2022
1 parent bcb2796 commit 708ed2b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
39 changes: 31 additions & 8 deletions src/Illuminate/Bus/UniqueLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}
17 changes: 3 additions & 14 deletions src/Illuminate/Queue/CallQueuedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down

0 comments on commit 708ed2b

Please sign in to comment.