Skip to content

Commit c2c35e9

Browse files
draft: fix: Don't release lock for ShouldBeUniqueUntilProcessing Job that gets released (#54261)
* fix: Don't release lock for ShouldBeUniqueUntilProcessing Job that gets released * Apply fixes from StyleCI * fix: Don't release lock for ShouldBeUniqueUntilProcessing Job that gets released * fix: Don't release lock for ShouldBeUniqueUntilProcessing Job that gets released * fix: Don't release lock for ShouldBeUniqueUntilProcessing Job that gets released * fix: Don't release lock for ShouldBeUniqueUntilProcessing Job that gets released * fix: Don't release lock for ShouldBeUniqueUntilProcessing Job that gets released * Apply fixes from StyleCI * fix: Don't release lock for ShouldBeUniqueUntilProcessing Job that gets released * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent 787cf26 commit c2c35e9

File tree

2 files changed

+93
-4
lines changed

2 files changed

+93
-4
lines changed

src/Illuminate/Queue/CallQueuedHandler.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ public function call(Job $job, array $data)
6666
return $this->handleModelNotFound($job, $e);
6767
}
6868

69-
if ($command instanceof ShouldBeUniqueUntilProcessing) {
70-
$this->ensureUniqueJobLockIsReleased($command);
71-
}
72-
7369
$this->dispatchThroughMiddleware($job, $command);
7470

7571
if (! $job->isReleased() && ! $command instanceof ShouldBeUniqueUntilProcessing) {
@@ -123,6 +119,10 @@ protected function dispatchThroughMiddleware(Job $job, $command)
123119
return (new Pipeline($this->container))->send($command)
124120
->through(array_merge(method_exists($command, 'middleware') ? $command->middleware() : [], $command->middleware ?? []))
125121
->then(function ($command) use ($job) {
122+
if ($command instanceof ShouldBeUniqueUntilProcessing) {
123+
$this->ensureUniqueJobLockIsReleased($command);
124+
}
125+
126126
return $this->dispatcher->dispatchNow(
127127
$command, $this->resolveHandler($job, $command)
128128
);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Queue;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Foundation\Bus\Dispatchable;
9+
use Illuminate\Queue\InteractsWithQueue;
10+
use Illuminate\Support\Facades\DB;
11+
use Orchestra\Testbench\Attributes\WithMigration;
12+
13+
#[WithMigration]
14+
#[WithMigration('cache')]
15+
#[WithMigration('queue')]
16+
class UniqueUntilProcessingJobTest extends QueueTestCase
17+
{
18+
protected function defineEnvironment($app)
19+
{
20+
parent::defineEnvironment($app);
21+
$app['config']->set('queue.default', 'database');
22+
$app['config']->set('cache.default', 'database');
23+
$this->driver = 'database';
24+
}
25+
26+
public function testShouldBeUniqueUntilProcessingReleasesLockWhenJobIsReleasedByAMiddleware()
27+
{
28+
// Job that does not release and gets processed
29+
UniqueTestJobThatDoesNotRelease::dispatch();
30+
$lockKey = DB::table('cache_locks')->orderBy('id')->first()->key;
31+
$this->assertNotNull($lockKey);
32+
$this->runQueueWorkerCommand(['--once' => true]);
33+
$this->assertFalse(UniqueTestJobThatDoesNotRelease::$released);
34+
$lockKey = DB::table('cache_locks')->first()->key ?? null;
35+
$this->assertNull($lockKey);
36+
$this->assertDatabaseCount('jobs', 0);
37+
38+
// Job that releases and does not get processed
39+
UniqueUntilProcessingJobThatReleases::dispatch();
40+
$lockKey = DB::table('cache_locks')->first()->key;
41+
$this->assertNotNull($lockKey);
42+
$this->runQueueWorkerCommand(['--once' => true]);
43+
$this->assertFalse(UniqueUntilProcessingJobThatReleases::$handled);
44+
$this->assertTrue(UniqueUntilProcessingJobThatReleases::$released);
45+
$lockKey = DB::table('cache_locks')->orderBy('id')->first()->key ?? null;
46+
$this->assertNotNull($lockKey);
47+
48+
UniqueUntilProcessingJobThatReleases::dispatch();
49+
$this->assertDatabaseCount('jobs', 1);
50+
}
51+
}
52+
53+
class UniqueTestJobThatDoesNotRelease implements ShouldQueue, ShouldBeUniqueUntilProcessing
54+
{
55+
use InteractsWithQueue, Queueable, Dispatchable;
56+
57+
public static $handled = false;
58+
public static $released = false;
59+
60+
public function __construct()
61+
{
62+
static::$handled = false;
63+
static::$released = false;
64+
}
65+
66+
public function handle()
67+
{
68+
static::$handled = true;
69+
}
70+
}
71+
72+
class UniqueUntilProcessingJobThatReleases extends UniqueTestJobThatDoesNotRelease
73+
{
74+
public function middleware()
75+
{
76+
return [
77+
function ($job) {
78+
static::$released = true;
79+
80+
return $job->release(30);
81+
},
82+
];
83+
}
84+
85+
public function uniqueId()
86+
{
87+
return 100;
88+
}
89+
}

0 commit comments

Comments
 (0)