From ec28c3d9faae9d31e2928e78fa9fc0939525c173 Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 30 Nov 2025 00:12:44 +0000 Subject: [PATCH 1/9] add static properties and tests --- src/Illuminate/Queue/Worker.php | 22 ++++++++ tests/Integration/Queue/WorkCommandTest.php | 56 +++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/src/Illuminate/Queue/Worker.php b/src/Illuminate/Queue/Worker.php index fe9bb0c46262..67bac0bcbe43 100644 --- a/src/Illuminate/Queue/Worker.php +++ b/src/Illuminate/Queue/Worker.php @@ -106,6 +106,20 @@ class Worker */ public static $memoryExceededExitCode; + /** + * Indicates if the worker should check for restart. + * + * @var bool + */ + public static $checkRestart = true; + + /** + * Indicates if the worker should check for paused queues. + * + * @var bool + */ + public static $checkPaused = true; + /** * Create a new queue worker. * @@ -400,6 +414,10 @@ protected function getNextJob($connection, $queue) */ protected function queuePaused($connectionName, $queue) { + if (! static::$checkPaused) { + return false; + } + return $this->cache && (bool) $this->cache->get( "illuminate:queue:paused:{$connectionName}:{$queue}", false ); @@ -750,6 +768,10 @@ protected function queueShouldRestart($lastRestart) */ protected function getTimestampOfLastQueueRestart() { + if (! static::$checkRestart) { + return null; + } + if ($this->cache) { return $this->cache->get('illuminate:queue:restart'); } diff --git a/tests/Integration/Queue/WorkCommandTest.php b/tests/Integration/Queue/WorkCommandTest.php index 33a3916e1746..17b6f2d5a37b 100644 --- a/tests/Integration/Queue/WorkCommandTest.php +++ b/tests/Integration/Queue/WorkCommandTest.php @@ -3,6 +3,8 @@ namespace Illuminate\Tests\Integration\Queue; use Illuminate\Bus\Queueable; +use Illuminate\Cache\CacheManager; +use Illuminate\Cache\Repository; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Database\UniqueConstraintViolationException; use Illuminate\Foundation\Bus\Dispatchable; @@ -12,6 +14,7 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Exceptions; use Illuminate\Support\Facades\Queue; +use Mockery; use Orchestra\Testbench\Attributes\WithMigration; use RuntimeException; @@ -184,6 +187,59 @@ public function testMemoryExitCode() Worker::$memoryExceededExitCode = null; } + public function testDisableRestartCheck() + { + $this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']); + + Worker::$checkRestart = false; + + $cache = Mockery::mock(Repository::class); + $cache->shouldNotReceive('get')->with('illuminate:queue:restart'); + + $cacheManager = Mockery::mock(CacheManager::class); + $cacheManager->shouldReceive('driver')->andReturn($cache); + $cacheManager->shouldReceive('store')->andReturn($cache); + + $this->app->instance('cache', $cacheManager); + + Queue::push(new FirstJob); + + $this->artisan('queue:work', [ + '--max-jobs' => 1, + '--stop-when-empty' => true, + ]); + + Worker::$checkRestart = true; + } + + public function testDisablePauseCheck() + { + $this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']); + + Worker::$checkPaused = false; + + $cache = Mockery::mock(Repository::class); + + $cache->shouldReceive('get')->with('illuminate:queue:restart')->andReturn(null); + + $cache->shouldNotReceive('get')->with(Mockery::pattern('/^illuminate:queue:paused:/'), false); + + $cacheManager = Mockery::mock(CacheManager::class); + $cacheManager->shouldReceive('driver')->andReturn($cache); + $cacheManager->shouldReceive('store')->andReturn($cache); + + $this->app->instance('cache', $cacheManager); + + Queue::push(new FirstJob); + + $this->artisan('queue:work', [ + '--max-jobs' => 1, + '--stop-when-empty' => true, + ]); + + Worker::$checkPaused = true; + } + public function testFailedJobListenerOnlyRunsOnce() { $this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']); From 64ac42e01dd9c7285d0956d21c958c5248f33151 Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 30 Nov 2025 01:58:46 +0000 Subject: [PATCH 2/9] mockery to m alias keep it inline --- tests/Integration/Queue/WorkCommandTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Integration/Queue/WorkCommandTest.php b/tests/Integration/Queue/WorkCommandTest.php index 17b6f2d5a37b..55018289470d 100644 --- a/tests/Integration/Queue/WorkCommandTest.php +++ b/tests/Integration/Queue/WorkCommandTest.php @@ -14,7 +14,7 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Exceptions; use Illuminate\Support\Facades\Queue; -use Mockery; +use Mockery as m; use Orchestra\Testbench\Attributes\WithMigration; use RuntimeException; @@ -193,10 +193,10 @@ public function testDisableRestartCheck() Worker::$checkRestart = false; - $cache = Mockery::mock(Repository::class); + $cache = m::mock(Repository::class); $cache->shouldNotReceive('get')->with('illuminate:queue:restart'); - $cacheManager = Mockery::mock(CacheManager::class); + $cacheManager = m::mock(CacheManager::class); $cacheManager->shouldReceive('driver')->andReturn($cache); $cacheManager->shouldReceive('store')->andReturn($cache); @@ -218,13 +218,13 @@ public function testDisablePauseCheck() Worker::$checkPaused = false; - $cache = Mockery::mock(Repository::class); + $cache = m::mock(Repository::class); $cache->shouldReceive('get')->with('illuminate:queue:restart')->andReturn(null); - $cache->shouldNotReceive('get')->with(Mockery::pattern('/^illuminate:queue:paused:/'), false); + $cache->shouldNotReceive('m')->with(m::pattern('/^illuminate:queue:paused:/'), false); - $cacheManager = Mockery::mock(CacheManager::class); + $cacheManager = m::mock(CacheManager::class); $cacheManager->shouldReceive('driver')->andReturn($cache); $cacheManager->shouldReceive('store')->andReturn($cache); From 0204b5b08eaa396539d8d11cd3b1f3df190dc3e0 Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 30 Nov 2025 02:29:15 +0000 Subject: [PATCH 3/9] rename for more semantic sense --- src/Illuminate/Queue/Worker.php | 16 ++++++++-------- tests/Integration/Queue/WorkCommandTest.php | 10 +++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Illuminate/Queue/Worker.php b/src/Illuminate/Queue/Worker.php index 67bac0bcbe43..cbde64cdb22c 100644 --- a/src/Illuminate/Queue/Worker.php +++ b/src/Illuminate/Queue/Worker.php @@ -111,14 +111,14 @@ class Worker * * @var bool */ - public static $checkRestart = true; + public static $checkLastRestart = true; /** * Indicates if the worker should check for paused queues. * * @var bool */ - public static $checkPaused = true; + public static $checkPausedQueues = true; /** * Create a new queue worker. @@ -386,7 +386,7 @@ protected function getNextJob($connection, $queue) } foreach (explode(',', $queue) as $index => $queue) { - if ($this->queuePaused($connection->getConnectionName(), $queue)) { + if (static::$checkPausedQueues && $this->queuePaused($connection->getConnectionName(), $queue)) { continue; } @@ -414,10 +414,6 @@ protected function getNextJob($connection, $queue) */ protected function queuePaused($connectionName, $queue) { - if (! static::$checkPaused) { - return false; - } - return $this->cache && (bool) $this->cache->get( "illuminate:queue:paused:{$connectionName}:{$queue}", false ); @@ -758,6 +754,10 @@ protected function raiseExceptionOccurredJobEvent($connectionName, $job, Throwab */ protected function queueShouldRestart($lastRestart) { + if (! static::$checkLastRestart) { + return false; + } + return $this->getTimestampOfLastQueueRestart() != $lastRestart; } @@ -768,7 +768,7 @@ protected function queueShouldRestart($lastRestart) */ protected function getTimestampOfLastQueueRestart() { - if (! static::$checkRestart) { + if (! static::$checkLastRestart) { return null; } diff --git a/tests/Integration/Queue/WorkCommandTest.php b/tests/Integration/Queue/WorkCommandTest.php index 55018289470d..2b333e432c71 100644 --- a/tests/Integration/Queue/WorkCommandTest.php +++ b/tests/Integration/Queue/WorkCommandTest.php @@ -187,11 +187,11 @@ public function testMemoryExitCode() Worker::$memoryExceededExitCode = null; } - public function testDisableRestartCheck() + public function testDisableLastRestartCheck() { $this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']); - Worker::$checkRestart = false; + Worker::$checkLastRestart = false; $cache = m::mock(Repository::class); $cache->shouldNotReceive('get')->with('illuminate:queue:restart'); @@ -209,14 +209,14 @@ public function testDisableRestartCheck() '--stop-when-empty' => true, ]); - Worker::$checkRestart = true; + Worker::$checkLastRestart = true; } public function testDisablePauseCheck() { $this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']); - Worker::$checkPaused = false; + Worker::$checkPausedQueues = false; $cache = m::mock(Repository::class); @@ -237,7 +237,7 @@ public function testDisablePauseCheck() '--stop-when-empty' => true, ]); - Worker::$checkPaused = true; + Worker::$checkPausedQueues = true; } public function testFailedJobListenerOnlyRunsOnce() From a55b5a868b91864172258337868bae8f84afc863 Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 30 Nov 2025 02:35:04 +0000 Subject: [PATCH 4/9] comment --- src/Illuminate/Queue/Worker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Queue/Worker.php b/src/Illuminate/Queue/Worker.php index cbde64cdb22c..eae6f34445b6 100644 --- a/src/Illuminate/Queue/Worker.php +++ b/src/Illuminate/Queue/Worker.php @@ -107,7 +107,7 @@ class Worker public static $memoryExceededExitCode; /** - * Indicates if the worker should check for restart. + * Indicates if the worker should check for the last restart. * * @var bool */ From fe4c19f167f9f66c79f61574eed26e0fe4131404 Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 30 Nov 2025 22:07:18 +0000 Subject: [PATCH 5/9] adjust test --- tests/Integration/Queue/WorkCommandTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Integration/Queue/WorkCommandTest.php b/tests/Integration/Queue/WorkCommandTest.php index 2b333e432c71..5790506d5a4b 100644 --- a/tests/Integration/Queue/WorkCommandTest.php +++ b/tests/Integration/Queue/WorkCommandTest.php @@ -195,6 +195,7 @@ public function testDisableLastRestartCheck() $cache = m::mock(Repository::class); $cache->shouldNotReceive('get')->with('illuminate:queue:restart'); + $cache->shouldReceive('get')->with(m::pattern('/^illuminate:queue:paused:/'), false); $cacheManager = m::mock(CacheManager::class); $cacheManager->shouldReceive('driver')->andReturn($cache); @@ -221,8 +222,7 @@ public function testDisablePauseCheck() $cache = m::mock(Repository::class); $cache->shouldReceive('get')->with('illuminate:queue:restart')->andReturn(null); - - $cache->shouldNotReceive('m')->with(m::pattern('/^illuminate:queue:paused:/'), false); + $cache->shouldNotReceive('get')->with(m::pattern('/^illuminate:queue:paused:/'), false); $cacheManager = m::mock(CacheManager::class); $cacheManager->shouldReceive('driver')->andReturn($cache); From f6015aaebe07b8157bdf0263692afe7bae33bb3f Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 30 Nov 2025 23:35:48 +0000 Subject: [PATCH 6/9] assert the job actually ran too --- tests/Integration/Queue/WorkCommandTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Integration/Queue/WorkCommandTest.php b/tests/Integration/Queue/WorkCommandTest.php index 5790506d5a4b..b100abcf7c8d 100644 --- a/tests/Integration/Queue/WorkCommandTest.php +++ b/tests/Integration/Queue/WorkCommandTest.php @@ -210,6 +210,9 @@ public function testDisableLastRestartCheck() '--stop-when-empty' => true, ]); + $this->assertSame(0, Queue::size()); + $this->assertTrue(FirstJob::$ran); + Worker::$checkLastRestart = true; } @@ -237,6 +240,9 @@ public function testDisablePauseCheck() '--stop-when-empty' => true, ]); + $this->assertSame(0, Queue::size()); + $this->assertTrue(FirstJob::$ran); + Worker::$checkPausedQueues = true; } From 46f349f9c8c84c283c80b316c3f8b07fe16fedb2 Mon Sep 17 00:00:00 2001 From: Jack Date: Mon, 1 Dec 2025 00:50:31 +0000 Subject: [PATCH 7/9] move check inside method to keep consistent --- src/Illuminate/Queue/Worker.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Queue/Worker.php b/src/Illuminate/Queue/Worker.php index eae6f34445b6..f3ceb67f7268 100644 --- a/src/Illuminate/Queue/Worker.php +++ b/src/Illuminate/Queue/Worker.php @@ -386,7 +386,7 @@ protected function getNextJob($connection, $queue) } foreach (explode(',', $queue) as $index => $queue) { - if (static::$checkPausedQueues && $this->queuePaused($connection->getConnectionName(), $queue)) { + if ($this->queuePaused($connection->getConnectionName(), $queue)) { continue; } @@ -414,6 +414,10 @@ protected function getNextJob($connection, $queue) */ protected function queuePaused($connectionName, $queue) { + if (! static::$checkPausedQueues) { + return false; + } + return $this->cache && (bool) $this->cache->get( "illuminate:queue:paused:{$connectionName}:{$queue}", false ); From 28de1d8f99a20e605aabcd1c14ec9b3d6cbe827c Mon Sep 17 00:00:00 2001 From: Jack Bayliss Date: Mon, 1 Dec 2025 09:28:49 +0000 Subject: [PATCH 8/9] rename so more readable --- src/Illuminate/Queue/Worker.php | 14 +++++++------- tests/Integration/Queue/WorkCommandTest.php | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Illuminate/Queue/Worker.php b/src/Illuminate/Queue/Worker.php index f3ceb67f7268..6e0e6eb93e48 100644 --- a/src/Illuminate/Queue/Worker.php +++ b/src/Illuminate/Queue/Worker.php @@ -107,18 +107,18 @@ class Worker public static $memoryExceededExitCode; /** - * Indicates if the worker should check for the last restart. + * Indicates if the worker should disable the check for the last restart. * * @var bool */ - public static $checkLastRestart = true; + public static $disableLastRestartCheck = false; /** - * Indicates if the worker should check for paused queues. + * Indicates if the worker should disable the check for paused queues. * * @var bool */ - public static $checkPausedQueues = true; + public static $disablePausedQueueCheck = false; /** * Create a new queue worker. @@ -414,7 +414,7 @@ protected function getNextJob($connection, $queue) */ protected function queuePaused($connectionName, $queue) { - if (! static::$checkPausedQueues) { + if (static::$disablePausedQueueCheck) { return false; } @@ -758,7 +758,7 @@ protected function raiseExceptionOccurredJobEvent($connectionName, $job, Throwab */ protected function queueShouldRestart($lastRestart) { - if (! static::$checkLastRestart) { + if (static::$disableLastRestartCheck) { return false; } @@ -772,7 +772,7 @@ protected function queueShouldRestart($lastRestart) */ protected function getTimestampOfLastQueueRestart() { - if (! static::$checkLastRestart) { + if (static::$disableLastRestartCheck) { return null; } diff --git a/tests/Integration/Queue/WorkCommandTest.php b/tests/Integration/Queue/WorkCommandTest.php index b100abcf7c8d..85c6b40b2aa5 100644 --- a/tests/Integration/Queue/WorkCommandTest.php +++ b/tests/Integration/Queue/WorkCommandTest.php @@ -191,7 +191,7 @@ public function testDisableLastRestartCheck() { $this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']); - Worker::$checkLastRestart = false; + Worker::$disableLastRestartCheck = true; $cache = m::mock(Repository::class); $cache->shouldNotReceive('get')->with('illuminate:queue:restart'); @@ -213,14 +213,14 @@ public function testDisableLastRestartCheck() $this->assertSame(0, Queue::size()); $this->assertTrue(FirstJob::$ran); - Worker::$checkLastRestart = true; + Worker::$disableLastRestartCheck = false; } - public function testDisablePauseCheck() + public function testDisablePauseQueueCheck() { $this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']); - Worker::$checkPausedQueues = false; + Worker::$disablePausedQueueCheck = true; $cache = m::mock(Repository::class); @@ -243,7 +243,7 @@ public function testDisablePauseCheck() $this->assertSame(0, Queue::size()); $this->assertTrue(FirstJob::$ran); - Worker::$checkPausedQueues = true; + Worker::$disablePausedQueueCheck = false; } public function testFailedJobListenerOnlyRunsOnce() From d477ae073976c8fe272b2b75759b3b4bcc865435 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 1 Dec 2025 09:37:50 -0800 Subject: [PATCH 9/9] formatting --- src/Illuminate/Queue/Worker.php | 14 +++++++------- tests/Integration/Queue/WorkCommandTest.php | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Illuminate/Queue/Worker.php b/src/Illuminate/Queue/Worker.php index 6e0e6eb93e48..c08d7a277507 100644 --- a/src/Illuminate/Queue/Worker.php +++ b/src/Illuminate/Queue/Worker.php @@ -107,18 +107,18 @@ class Worker public static $memoryExceededExitCode; /** - * Indicates if the worker should disable the check for the last restart. + * Indicates if the worker should check for the restart signal in the cache. * * @var bool */ - public static $disableLastRestartCheck = false; + public static $restartable = true; /** - * Indicates if the worker should disable the check for paused queues. + * Indicates if the worker should check for the paused signal in the cache. * * @var bool */ - public static $disablePausedQueueCheck = false; + public static $pausable = true; /** * Create a new queue worker. @@ -414,7 +414,7 @@ protected function getNextJob($connection, $queue) */ protected function queuePaused($connectionName, $queue) { - if (static::$disablePausedQueueCheck) { + if (! static::$pausable) { return false; } @@ -758,7 +758,7 @@ protected function raiseExceptionOccurredJobEvent($connectionName, $job, Throwab */ protected function queueShouldRestart($lastRestart) { - if (static::$disableLastRestartCheck) { + if (! static::$restartable) { return false; } @@ -772,7 +772,7 @@ protected function queueShouldRestart($lastRestart) */ protected function getTimestampOfLastQueueRestart() { - if (static::$disableLastRestartCheck) { + if (! static::$restartable) { return null; } diff --git a/tests/Integration/Queue/WorkCommandTest.php b/tests/Integration/Queue/WorkCommandTest.php index 85c6b40b2aa5..f1846550daf4 100644 --- a/tests/Integration/Queue/WorkCommandTest.php +++ b/tests/Integration/Queue/WorkCommandTest.php @@ -191,7 +191,7 @@ public function testDisableLastRestartCheck() { $this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']); - Worker::$disableLastRestartCheck = true; + Worker::$restartable = false; $cache = m::mock(Repository::class); $cache->shouldNotReceive('get')->with('illuminate:queue:restart'); @@ -213,14 +213,14 @@ public function testDisableLastRestartCheck() $this->assertSame(0, Queue::size()); $this->assertTrue(FirstJob::$ran); - Worker::$disableLastRestartCheck = false; + Worker::$restartable = true; } public function testDisablePauseQueueCheck() { $this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']); - Worker::$disablePausedQueueCheck = true; + Worker::$pausable = false; $cache = m::mock(Repository::class); @@ -243,7 +243,7 @@ public function testDisablePauseQueueCheck() $this->assertSame(0, Queue::size()); $this->assertTrue(FirstJob::$ran); - Worker::$disablePausedQueueCheck = false; + Worker::$pausable = true; } public function testFailedJobListenerOnlyRunsOnce()