Skip to content

Commit e9a3727

Browse files
[12.x] Add ability to ignore queuePaused \ queueShouldRestart cache checks (#57975)
* add static properties and tests * mockery to m alias keep it inline * rename for more semantic sense * comment * adjust test * assert the job actually ran too * move check inside method to keep consistent * rename so more readable * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 748adea commit e9a3727

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/Illuminate/Queue/Worker.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ class Worker
106106
*/
107107
public static $memoryExceededExitCode;
108108

109+
/**
110+
* Indicates if the worker should check for the restart signal in the cache.
111+
*
112+
* @var bool
113+
*/
114+
public static $restartable = true;
115+
116+
/**
117+
* Indicates if the worker should check for the paused signal in the cache.
118+
*
119+
* @var bool
120+
*/
121+
public static $pausable = true;
122+
109123
/**
110124
* Create a new queue worker.
111125
*
@@ -400,6 +414,10 @@ protected function getNextJob($connection, $queue)
400414
*/
401415
protected function queuePaused($connectionName, $queue)
402416
{
417+
if (! static::$pausable) {
418+
return false;
419+
}
420+
403421
return $this->cache && (bool) $this->cache->get(
404422
"illuminate:queue:paused:{$connectionName}:{$queue}", false
405423
);
@@ -740,6 +758,10 @@ protected function raiseExceptionOccurredJobEvent($connectionName, $job, Throwab
740758
*/
741759
protected function queueShouldRestart($lastRestart)
742760
{
761+
if (! static::$restartable) {
762+
return false;
763+
}
764+
743765
return $this->getTimestampOfLastQueueRestart() != $lastRestart;
744766
}
745767

@@ -750,6 +772,10 @@ protected function queueShouldRestart($lastRestart)
750772
*/
751773
protected function getTimestampOfLastQueueRestart()
752774
{
775+
if (! static::$restartable) {
776+
return null;
777+
}
778+
753779
if ($this->cache) {
754780
return $this->cache->get('illuminate:queue:restart');
755781
}

tests/Integration/Queue/WorkCommandTest.php

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

55
use Illuminate\Bus\Queueable;
6+
use Illuminate\Cache\CacheManager;
7+
use Illuminate\Cache\Repository;
68
use Illuminate\Contracts\Queue\ShouldQueue;
79
use Illuminate\Database\UniqueConstraintViolationException;
810
use Illuminate\Foundation\Bus\Dispatchable;
@@ -12,6 +14,7 @@
1214
use Illuminate\Support\Facades\Artisan;
1315
use Illuminate\Support\Facades\Exceptions;
1416
use Illuminate\Support\Facades\Queue;
17+
use Mockery as m;
1518
use Orchestra\Testbench\Attributes\WithMigration;
1619
use RuntimeException;
1720

@@ -184,6 +187,65 @@ public function testMemoryExitCode()
184187
Worker::$memoryExceededExitCode = null;
185188
}
186189

190+
public function testDisableLastRestartCheck()
191+
{
192+
$this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']);
193+
194+
Worker::$restartable = false;
195+
196+
$cache = m::mock(Repository::class);
197+
$cache->shouldNotReceive('get')->with('illuminate:queue:restart');
198+
$cache->shouldReceive('get')->with(m::pattern('/^illuminate:queue:paused:/'), false);
199+
200+
$cacheManager = m::mock(CacheManager::class);
201+
$cacheManager->shouldReceive('driver')->andReturn($cache);
202+
$cacheManager->shouldReceive('store')->andReturn($cache);
203+
204+
$this->app->instance('cache', $cacheManager);
205+
206+
Queue::push(new FirstJob);
207+
208+
$this->artisan('queue:work', [
209+
'--max-jobs' => 1,
210+
'--stop-when-empty' => true,
211+
]);
212+
213+
$this->assertSame(0, Queue::size());
214+
$this->assertTrue(FirstJob::$ran);
215+
216+
Worker::$restartable = true;
217+
}
218+
219+
public function testDisablePauseQueueCheck()
220+
{
221+
$this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']);
222+
223+
Worker::$pausable = false;
224+
225+
$cache = m::mock(Repository::class);
226+
227+
$cache->shouldReceive('get')->with('illuminate:queue:restart')->andReturn(null);
228+
$cache->shouldNotReceive('get')->with(m::pattern('/^illuminate:queue:paused:/'), false);
229+
230+
$cacheManager = m::mock(CacheManager::class);
231+
$cacheManager->shouldReceive('driver')->andReturn($cache);
232+
$cacheManager->shouldReceive('store')->andReturn($cache);
233+
234+
$this->app->instance('cache', $cacheManager);
235+
236+
Queue::push(new FirstJob);
237+
238+
$this->artisan('queue:work', [
239+
'--max-jobs' => 1,
240+
'--stop-when-empty' => true,
241+
]);
242+
243+
$this->assertSame(0, Queue::size());
244+
$this->assertTrue(FirstJob::$ran);
245+
246+
Worker::$pausable = true;
247+
}
248+
187249
public function testFailedJobListenerOnlyRunsOnce()
188250
{
189251
$this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']);

0 commit comments

Comments
 (0)