Skip to content

Commit 2a6713f

Browse files
authored
[9.x] Release lock for job implementing ShouldBeUnique that is dispatched afterResponse() (#46806)
* failing test * rely on PendingDispatch@afterResponse so that ShouldBeUnique is checked * modifications to failing test * move lock/middleware handling to CallQueuedHandler@handle() * use CallQueuedHandler@handle if job employs InteractsWithQueue * style * revert changes to CallQueuedHandler & PendingDispatch * switch Bus\Dispatcher@dispatchAfterResponse to rely on Dispatcher@dispatchSync() * add `dispatchAfterResponse` test
1 parent 6114330 commit 2a6713f

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/Illuminate/Bus/Dispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ protected function pushCommandToQueue($queue, $command)
263263
public function dispatchAfterResponse($command, $handler = null)
264264
{
265265
$this->container->terminating(function () use ($command, $handler) {
266-
$this->dispatchNow($command, $handler);
266+
$this->dispatchSync($command, $handler);
267267
});
268268
}
269269

src/Illuminate/Foundation/Bus/Dispatchable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static function dispatchNow(...$arguments)
9696
*/
9797
public static function dispatchAfterResponse(...$arguments)
9898
{
99-
return app(Dispatcher::class)->dispatchAfterResponse(new static(...$arguments));
99+
return self::dispatch(...$arguments)->afterResponse();
100100
}
101101

102102
/**

tests/Integration/Queue/JobDispatchingTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
namespace Illuminate\Tests\Integration\Queue;
44

55
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Cache\Repository;
7+
use Illuminate\Contracts\Queue\ShouldBeUnique;
68
use Illuminate\Contracts\Queue\ShouldQueue;
79
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\InteractsWithQueue;
811
use Orchestra\Testbench\TestCase;
912

1013
class JobDispatchingTest extends TestCase
@@ -70,6 +73,52 @@ public function testDoesNotDispatchConditionallyWithClosure()
7073

7174
$this->assertTrue(Job::$ran);
7275
}
76+
77+
public function testUniqueJobLockIsReleasedForJobDispatchedAfterResponse()
78+
{
79+
// get initial terminatingCallbacks
80+
$terminatingCallbacksReflectionProperty = (new \ReflectionObject($this->app))->getProperty('terminatingCallbacks');
81+
$terminatingCallbacksReflectionProperty->setAccessible(true);
82+
$startTerminatingCallbacks = $terminatingCallbacksReflectionProperty->getValue($this->app);
83+
84+
UniqueJob::dispatchAfterResponse('test');
85+
$this->assertFalse(
86+
$this->getJobLock(UniqueJob::class, 'test')
87+
);
88+
89+
$this->app->terminate();
90+
$this->assertTrue(UniqueJob::$ran);
91+
92+
$terminatingCallbacksReflectionProperty->setValue($this->app, $startTerminatingCallbacks);
93+
94+
UniqueJob::$ran = false;
95+
UniqueJob::dispatch('test')->afterResponse();
96+
$this->app->terminate();
97+
$this->assertTrue(UniqueJob::$ran);
98+
99+
// acquire job lock and confirm that job is not dispatched after response
100+
$this->assertTrue(
101+
$this->getJobLock(UniqueJob::class, 'test')
102+
);
103+
$terminatingCallbacksReflectionProperty->setValue($this->app, $startTerminatingCallbacks);
104+
UniqueJob::$ran = false;
105+
UniqueJob::dispatch('test')->afterResponse();
106+
$this->app->terminate();
107+
$this->assertFalse(UniqueJob::$ran);
108+
109+
// confirm that dispatchAfterResponse also does not run
110+
UniqueJob::dispatchAfterResponse('test');
111+
$this->app->terminate();
112+
$this->assertFalse(UniqueJob::$ran);
113+
}
114+
115+
/**
116+
* Helpers.
117+
*/
118+
private function getJobLock($job, $value = null)
119+
{
120+
return $this->app->get(Repository::class)->lock('laravel_unique_job:'.$job.$value, 10)->get();
121+
}
73122
}
74123

75124
class Job implements ShouldQueue
@@ -96,3 +145,13 @@ public function replaceValue($value)
96145
static::$value = $value;
97146
}
98147
}
148+
149+
class UniqueJob extends Job implements ShouldBeUnique
150+
{
151+
use InteractsWithQueue;
152+
153+
public function uniqueId()
154+
{
155+
return self::$value;
156+
}
157+
}

0 commit comments

Comments
 (0)