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] Share WithoutOverlapping key across jobs #44227

Merged
merged 2 commits into from
Sep 21, 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
23 changes: 22 additions & 1 deletion src/Illuminate/Queue/Middleware/WithoutOverlapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class WithoutOverlapping
*/
public $prefix = 'laravel-queue-overlap:';

/**
* Share the key across different jobs.
*
* @var bool
*/
public $shareKey = false;

/**
* Create a new middleware instance.
*
Expand Down Expand Up @@ -128,6 +135,18 @@ public function withPrefix(string $prefix)
return $this;
}

/**
* Indicate that the lock key should be shared across job classes.
*
* @return void
*/
public function shared()
{
$this->shareKey = true;

return $this;
}

/**
* Get the lock key for the given job.
*
Expand All @@ -136,6 +155,8 @@ public function withPrefix(string $prefix)
*/
public function getLockKey($job)
{
return $this->prefix.get_class($job).':'.$this->key;
return $this->shareKey
? $this->prefix.$this->key
: $this->prefix.get_class($job).':'.$this->key;
}
}
81 changes: 81 additions & 0 deletions tests/Integration/Queue/WithoutOverlappingJobsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,53 @@ public function testOverlappingJobsCanBeSkipped()

$this->assertFalse(SkipOverlappingTestJob::$handled);
}

public function testCanShareKeyAcrossJobs()
{
OverlappingTestJobWithSharedKeyOne::$handled = false;
$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);

$lockKey = (new WithoutOverlapping)->shared()->getLockKey(new OverlappingTestJobWithSharedKeyTwo);
$this->app->get(Cache::class)->lock($lockKey, 10)->acquire();

$job = m::mock(Job::class);

$job->shouldReceive('release')->once();
$job->shouldReceive('hasFailed')->andReturn(false);
$job->shouldReceive('isReleased')->andReturn(true);
$job->shouldReceive('isDeletedOrReleased')->andReturn(true);

$instance->call($job, [
'command' => serialize(new OverlappingTestJobWithSharedKeyOne),
]);

$this->assertFalse(OverlappingTestJob::$handled);
}

public function testGetLock()
{
$job = new OverlappingTestJob;

$this->assertSame(
'laravel-queue-overlap:Illuminate\\Tests\\Integration\\Queue\\OverlappingTestJob:key',
(new WithoutOverlapping('key'))->getLockKey($job)
);

$this->assertSame(
'laravel-queue-overlap:key',
(new WithoutOverlapping('key'))->shared()->getLockKey($job)
);

$this->assertSame(
'prefix:Illuminate\\Tests\\Integration\\Queue\\OverlappingTestJob:key',
(new WithoutOverlapping('key'))->withPrefix('prefix:')->getLockKey($job)
);

$this->assertSame(
'prefix:key',
(new WithoutOverlapping('key'))->withPrefix('prefix:')->shared()->getLockKey($job)
);
}
}

class OverlappingTestJob
Expand Down Expand Up @@ -148,3 +195,37 @@ public function handle()
throw new Exception;
}
}

class OverlappingTestJobWithSharedKeyOne
{
use InteractsWithQueue, Queueable;

public static $handled = false;

public function handle()
{
static::$handled = true;
}

public function middleware()
{
return [(new WithoutOverlapping)->shared()];
}
}

class OverlappingTestJobWithSharedKeyTwo
{
use InteractsWithQueue, Queueable;

public static $handled = false;

public function handle()
{
static::$handled = true;
}

public function middleware()
{
return [(new WithoutOverlapping)->shared()];
}
}