Skip to content

Commit 44c9d81

Browse files
committed
Fix manual maxruntime config to be used again.
1 parent 3ff4164 commit 44c9d81

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

docs/sections/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ You may create a file called `app_queue.php` inside your `config` folder (NOT th
3434
$config['Queue']['workermaxruntime'] = 120;
3535
```
3636

37-
*Warning:* Do not use 0 if you are using a cronjob to permanantly start a new worker once in a while and if you do not exit on idle.
37+
*Warning:* Do not use 0 if you are using a cronjob to permanently start a new worker once in a while and if you do not exit on idle.
3838

3939
- Seconds of running time after which the PHP process of the worker will terminate (0 = unlimited):
4040

src/Queue/Processor.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function run(array $args): int {
134134
$startTime = time();
135135

136136
while (!$this->exit) {
137-
$this->setPhpTimeout();
137+
$this->setPhpTimeout($config['maxruntime']);
138138

139139
try {
140140
$this->updatePid($pid);
@@ -167,10 +167,11 @@ public function run(array $args): int {
167167
sleep(Config::sleeptime());
168168
}
169169

170+
$maxRuntime = $config['maxruntime'] ?: Configure::readOrFail('Queue.workermaxruntime');
170171
// check if we are over the maximum runtime and end processing if so.
171-
if (Configure::readOrFail('Queue.workermaxruntime') && (time() - $startTime) >= Configure::readOrFail('Queue.workermaxruntime')) {
172+
if ($maxRuntime && (time() - $startTime) >= $maxRuntime) {
172173
$this->exit = true;
173-
$this->io->out('Reached runtime of ' . (time() - $startTime) . ' Seconds (Max ' . Configure::readOrFail('Queue.workermaxruntime') . '), terminating.');
174+
$this->io->out('Reached runtime of ' . (time() - $startTime) . ' Seconds (Max ' . $maxRuntime . '), terminating.');
174175
}
175176
if ($this->exit || mt_rand(0, 100) > 100 - (int)Config::gcprob()) {
176177
$this->io->out('Performing Old job cleanup.');
@@ -426,13 +427,21 @@ protected function stringToArray(string $param): array {
426427
}
427428

428429
/**
429-
* Makes sure accidental overriding isn't possible, uses workermaxruntime times 100 by default.
430+
* Makes sure accidental overriding isn't possible, uses workermaxruntime times 2 by default.
430431
* If available, uses workertimeout config directly.
431432
*
433+
* @param int|null $maxruntime Max runtime in seconds if set via CLI option.
434+
*
432435
* @return void
433436
*/
434-
protected function setPhpTimeout(): void {
435-
$timeLimit = (int)Configure::readOrFail('Queue.workermaxruntime') * 100;
437+
protected function setPhpTimeout(?int $maxruntime): void {
438+
if ($maxruntime) {
439+
set_time_limit($maxruntime * 2);
440+
441+
return;
442+
}
443+
444+
$timeLimit = (int)Configure::readOrFail('Queue.workermaxruntime') * 2;
436445
if (Configure::read('Queue.workertimeout') !== null) {
437446
$timeLimit = (int)Configure::read('Queue.workertimeout');
438447
}
@@ -450,6 +459,7 @@ protected function getConfig(array $args): array {
450459
'groups' => [],
451460
'types' => [],
452461
'verbose' => false,
462+
'maxruntime' => null,
453463
];
454464
if (!empty($args['verbose'])) {
455465
$config['verbose'] = true;
@@ -460,6 +470,9 @@ protected function getConfig(array $args): array {
460470
if (!empty($args['type'])) {
461471
$config['types'] = $this->stringToArray($args['type']);
462472
}
473+
if (!empty($args['max-runtime'])) {
474+
$config['maxruntime'] = (int)$args['max-runtime'];
475+
}
463476

464477
return $config;
465478
}

0 commit comments

Comments
 (0)