Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Move unique lock release #43740

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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