Skip to content

Commit 0ff9e14

Browse files
jackbaylisstaylorotwell
authored andcommitted
[12.x] Add ability to control QueueWorker memory exceeded exit code (laravel#57044)
* init * Update WorkCommand.php * cs * fix up test * init * Update WorkCommandTest.php * try fix flakiness * cs * Update WorkCommandTest.php * Update WorkCommandTest.php * readd const to prevent breaking change :trollface: * use variable --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 1b4fd54 commit 0ff9e14

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/Illuminate/Queue/Console/WorkCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected function gatherWorkerOptions()
168168
$this->option('stop-when-empty'),
169169
$this->option('max-jobs'),
170170
$this->option('max-time'),
171-
$this->option('rest')
171+
$this->option('rest'),
172172
);
173173
}
174174

src/Illuminate/Queue/Worker.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ class Worker
9999
*/
100100
protected static $popCallbacks = [];
101101

102+
/**
103+
* The custom exit code to be used when memory is exceeded.
104+
*
105+
* @var int|null
106+
*/
107+
public static $memoryExceededExitCode;
108+
102109
/**
103110
* Create a new queue worker.
104111
*
@@ -307,7 +314,7 @@ protected function stopIfNecessary(WorkerOptions $options, $lastRestart, $startT
307314
{
308315
return match (true) {
309316
$this->shouldQuit => static::EXIT_SUCCESS,
310-
$this->memoryExceeded($options->memory) => static::EXIT_MEMORY_LIMIT,
317+
$this->memoryExceeded($options->memory) => static::$memoryExceededExitCode ?: static::EXIT_MEMORY_LIMIT,
311318
$this->queueShouldRestart($lastRestart) => static::EXIT_SUCCESS,
312319
$options->stopWhenEmpty && is_null($job) => static::EXIT_SUCCESS,
313320
$options->maxTime && hrtime(true) / 1e9 - $startTime >= $options->maxTime => static::EXIT_SUCCESS,

tests/Integration/Queue/WorkCommandTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\UniqueConstraintViolationException;
88
use Illuminate\Foundation\Bus\Dispatchable;
99
use Illuminate\Foundation\Testing\DatabaseMigrations;
10+
use Illuminate\Queue\Worker;
1011
use Illuminate\Support\Carbon;
1112
use Illuminate\Support\Facades\Artisan;
1213
use Illuminate\Support\Facades\Exceptions;
@@ -162,6 +163,27 @@ public function testMaxTimeExceeded()
162163
$this->assertFalse(SecondJob::$ran);
163164
}
164165

166+
public function testMemoryExitCode()
167+
{
168+
$this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']);
169+
170+
Worker::$memoryExceededExitCode = 25;
171+
172+
Queue::push(new FirstJob);
173+
Queue::push(new SecondJob);
174+
175+
$this->artisan('queue:work', [
176+
'--memory' => 0.1,
177+
])->assertExitCode(25);
178+
179+
// Memory limit isn't checked until after the first job is attempted.
180+
$this->assertSame(1, Queue::size());
181+
$this->assertTrue(FirstJob::$ran);
182+
$this->assertFalse(SecondJob::$ran);
183+
184+
Worker::$memoryExceededExitCode = null;
185+
}
186+
165187
public function testFailedJobListenerOnlyRunsOnce()
166188
{
167189
$this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']);

0 commit comments

Comments
 (0)