Skip to content
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
26 changes: 26 additions & 0 deletions src/Illuminate/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ class Worker
*/
public static $memoryExceededExitCode;

/**
* Indicates if the worker should check for the restart signal in the cache.
*
* @var bool
*/
public static $restartable = true;

/**
* Indicates if the worker should check for the paused signal in the cache.
*
* @var bool
*/
public static $pausable = true;

/**
* Create a new queue worker.
*
Expand Down Expand Up @@ -400,6 +414,10 @@ protected function getNextJob($connection, $queue)
*/
protected function queuePaused($connectionName, $queue)
{
if (! static::$pausable) {
return false;
}

return $this->cache && (bool) $this->cache->get(
"illuminate:queue:paused:{$connectionName}:{$queue}", false
);
Expand Down Expand Up @@ -740,6 +758,10 @@ protected function raiseExceptionOccurredJobEvent($connectionName, $job, Throwab
*/
protected function queueShouldRestart($lastRestart)
{
if (! static::$restartable) {
return false;
}

return $this->getTimestampOfLastQueueRestart() != $lastRestart;
}

Expand All @@ -750,6 +772,10 @@ protected function queueShouldRestart($lastRestart)
*/
protected function getTimestampOfLastQueueRestart()
{
if (! static::$restartable) {
return null;
}

if ($this->cache) {
return $this->cache->get('illuminate:queue:restart');
}
Expand Down
62 changes: 62 additions & 0 deletions tests/Integration/Queue/WorkCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +14,7 @@
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Exceptions;
use Illuminate\Support\Facades\Queue;
use Mockery as m;
use Orchestra\Testbench\Attributes\WithMigration;
use RuntimeException;

Expand Down Expand Up @@ -184,6 +187,65 @@ public function testMemoryExitCode()
Worker::$memoryExceededExitCode = null;
}

public function testDisableLastRestartCheck()
{
$this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']);

Worker::$restartable = false;

$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);
$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,
]);

$this->assertSame(0, Queue::size());
$this->assertTrue(FirstJob::$ran);

Worker::$restartable = true;
}

public function testDisablePauseQueueCheck()
{
$this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']);

Worker::$pausable = false;

$cache = m::mock(Repository::class);

$cache->shouldReceive('get')->with('illuminate:queue:restart')->andReturn(null);
$cache->shouldNotReceive('get')->with(m::pattern('/^illuminate:queue:paused:/'), false);

$cacheManager = m::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,
]);

$this->assertSame(0, Queue::size());
$this->assertTrue(FirstJob::$ran);

Worker::$pausable = true;
}

public function testFailedJobListenerOnlyRunsOnce()
{
$this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']);
Expand Down