Skip to content

Commit 153bb6a

Browse files
author
Admin
committed
Fix memory not limited in work and listen commands laravel/framework#56077
Call it like: php artisan queue:work || echo "error..."
1 parent 8b081e8 commit 153bb6a

File tree

7 files changed

+49
-52
lines changed

7 files changed

+49
-52
lines changed

illuminate/Queue/Console/ListenCommand.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ class ListenCommand extends Command
1616
* @var string
1717
*/
1818
protected $signature = 'queue:listen
19-
{connection? : The name of connection}
20-
{--name=default : The name of the worker}
21-
{--delay=0 : The number of seconds to delay failed jobs (Deprecated)}
22-
{--backoff=0 : The number of seconds to wait before retrying a job that encountered an uncaught exception}
23-
{--force : Force the worker to run even in maintenance mode}
24-
{--memory=128 : The memory limit in megabytes}
25-
{--queue= : The queue to listen on}
26-
{--sleep=3 : Number of seconds to sleep when no job is available}
27-
{--rest=0 : Number of seconds to rest between jobs}
28-
{--timeout=60 : The number of seconds a child process can run}
29-
{--tries=1 : Number of times to attempt a job before logging it failed}';
19+
{connection? : The name of connection}
20+
{--name=default : The name of the worker}
21+
{--delay=0 : The number of seconds to delay failed jobs (Deprecated)}
22+
{--backoff=0 : The number of seconds to wait before retrying a job that encountered an uncaught exception}
23+
{--force : Force the worker to run even in maintenance mode}
24+
{--memory=128 : The memory limit in megabytes}
25+
{--queue= : The queue to listen on}
26+
{--sleep=3 : Number of seconds to sleep when no job is available}
27+
{--rest=0 : Number of seconds to rest between jobs}
28+
{--timeout=60 : The number of seconds a child process can run}
29+
{--tries=1 : Number of times to attempt a job before logging it failed}';
3030

3131
/**
3232
* The console command description.
@@ -73,11 +73,10 @@ public function handle()
7373
sprintf('Processing jobs from the [%s] %s.', $queue, str('queue')->plural(explode(',', $queue)))
7474
);
7575

76-
$this->listener->listen(
77-
$connection,
78-
$queue,
79-
$this->gatherOptions()
80-
);
76+
$options = $this->gatherOptions();
77+
\ini_set('memory_limit', $options->memory . 'M');
78+
79+
$this->listener->listen($connection, $queue, $options);
8180
}
8281

8382
/**
@@ -114,9 +113,9 @@ protected function gatherOptions()
114113
memory: $this->option('memory'),
115114
timeout: $this->option('timeout'),
116115
sleep: $this->option('sleep'),
117-
rest: $this->option('rest'),
118116
maxTries: $this->option('tries'),
119-
force: $this->option('force')
117+
force: $this->option('force'),
118+
rest: $this->option('rest')
120119
);
121120
}
122121

illuminate/Queue/Console/MonitorCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class MonitorCommand extends Command
1818
* @var string
1919
*/
2020
protected $signature = 'queue:monitor
21-
{queues : The names of the queues to monitor}
22-
{--max=1000 : The maximum number of jobs that can be on the queue before an event is dispatched}';
21+
{queues : The names of the queues to monitor}
22+
{--max=1000 : The maximum number of jobs that can be on the queue before an event is dispatched}';
2323

2424
/**
2525
* The console command description.

illuminate/Queue/Console/PruneBatchesCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class PruneBatchesCommand extends Command
1818
* @var string
1919
*/
2020
protected $signature = 'queue:prune-batches
21-
{--hours=24 : The number of hours to retain batch data}
22-
{--unfinished= : The number of hours to retain unfinished batch data }
23-
{--cancelled= : The number of hours to retain cancelled batch data }';
21+
{--hours=24 : The number of hours to retain batch data}
22+
{--unfinished= : The number of hours to retain unfinished batch data }
23+
{--cancelled= : The number of hours to retain cancelled batch data }';
2424

2525
/**
2626
* The console command description.

illuminate/Queue/Console/PruneFailedJobsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class PruneFailedJobsCommand extends Command
1616
* @var string
1717
*/
1818
protected $signature = 'queue:prune-failed
19-
{--hours=24 : The number of hours to retain failed jobs data}';
19+
{--hours=24 : The number of hours to retain failed jobs data}';
2020

2121
/**
2222
* The console command description.

illuminate/Queue/Console/RetryCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class RetryCommand extends Command
1919
* @var string
2020
*/
2121
protected $signature = 'queue:retry
22-
{id?* : The ID of the failed job or "all" to retry all jobs}
23-
{--queue= : Retry all of the failed jobs for the specified queue}
24-
{--range=* : Range of job IDs (numeric) to be retried (e.g. 1-5)}';
22+
{id?* : The ID of the failed job or "all" to retry all jobs}
23+
{--queue= : Retry all of the failed jobs for the specified queue}
24+
{--range=* : Range of job IDs (numeric) to be retried (e.g. 1-5)}';
2525

2626
/**
2727
* The console command description.

illuminate/Queue/Console/WorkCommand.php

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ class WorkCommand extends Command
2727
* @var string
2828
*/
2929
protected $signature = 'queue:work
30-
{connection? : The name of the queue connection to work}
31-
{--name=default : The name of the worker}
32-
{--queue= : The names of the queues to work}
33-
{--daemon : Run the worker in daemon mode (Deprecated)}
34-
{--once : Only process the next job on the queue}
35-
{--stop-when-empty : Stop when the queue is empty}
36-
{--delay=0 : The number of seconds to delay failed jobs (Deprecated)}
37-
{--backoff=0 : The number of seconds to wait before retrying a job that encountered an uncaught exception}
38-
{--max-jobs=0 : The number of jobs to process before stopping}
39-
{--max-time=0 : The maximum number of seconds the worker should run}
40-
{--force : Force the worker to run even in maintenance mode}
41-
{--memory=128 : The memory limit in megabytes}
42-
{--sleep=3 : Number of seconds to sleep when no job is available}
43-
{--rest=0 : Number of seconds to rest between jobs}
44-
{--timeout=60 : The number of seconds a child process can run}
45-
{--tries=1 : Number of times to attempt a job before logging it failed}';
30+
{connection? : The name of the queue connection to work}
31+
{--name=default : The name of the worker}
32+
{--queue= : The names of the queues to work}
33+
{--daemon : Run the worker in daemon mode (Deprecated)}
34+
{--once : Only process the next job on the queue}
35+
{--stop-when-empty : Stop when the queue is empty}
36+
{--delay=0 : The number of seconds to delay failed jobs (Deprecated)}
37+
{--backoff=0 : The number of seconds to wait before retrying a job that encountered an uncaught exception}
38+
{--max-jobs=0 : The number of jobs to process before stopping}
39+
{--max-time=0 : The maximum number of seconds the worker should run}
40+
{--force : Force the worker to run even in maintenance mode}
41+
{--memory=128 : The memory limit in megabytes}
42+
{--sleep=3 : Number of seconds to sleep when no job is available}
43+
{--rest=0 : Number of seconds to rest between jobs}
44+
{--timeout=60 : The number of seconds a child process can run}
45+
{--tries=1 : Number of times to attempt a job before logging it failed}';
4646

4747
/**
4848
* The console command description.
@@ -134,14 +134,13 @@ public function handle()
134134
*/
135135
protected function runWorker($connection, $queue)
136136
{
137+
$workerOptions = $this->gatherWorkerOptions();
138+
\ini_set('memory_limit', $workerOptions->memory . 'M');
139+
137140
return $this->worker
138-
->setName($this->option('name'))
141+
->setName($workerOptions->name)
139142
->setCache($this->cache)
140-
->{$this->option('once') ? 'runNextJob' : 'daemon'}(
141-
$connection,
142-
$queue,
143-
$this->gatherWorkerOptions()
144-
);
143+
->{$this->option('once') ? 'runNextJob' : 'daemon'}($connection, $queue, $workerOptions);
145144
}
146145

147146
/**

illuminate/Queue/Worker.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,15 @@ protected function pauseWorker(WorkerOptions $options, $lastRestart)
312312
* @param int $lastRestart
313313
* @param int $startTime
314314
* @param int $jobsProcessed
315-
* @param mixed $job
316-
* @return int|null
315+
* @param mixed|null $job
317316
*/
318317
protected function stopIfNecessary(
319318
WorkerOptions $options,
320319
$lastRestart,
321320
$startTime = 0,
322321
$jobsProcessed = 0,
323322
$job = null
324-
) {
323+
): ?int {
325324
return match (true) {
326325
$this->shouldQuit => static::EXIT_SUCCESS,
327326
$this->memoryExceeded($options->memory) => static::EXIT_MEMORY_LIMIT,

0 commit comments

Comments
 (0)