Skip to content

Commit b53bebc

Browse files
authored
Merge pull request #38 from notpron/honor-correct-config
Honor task specific config
2 parents 19592e0 + 9d2e784 commit b53bebc

3 files changed

Lines changed: 52 additions & 26 deletions

File tree

src/Model/Table/QueuedTasksTable.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,17 +452,45 @@ public function rerun($taskName): int
452452
/**
453453
* Cleanup/Delete Completed Tasks.
454454
*
455+
* @param array $capabilities Available QueueWorkerTasks.
455456
* @return void
456457
*/
457-
public function cleanOldJobs(): void
458+
public function cleanOldJobs(array $capabilities): void
458459
{
459-
if (!Configure::read('Queue.cleanuptimeout')) {
460-
return;
460+
$conditions = [];
461+
462+
// Generate the job specific conditions
463+
foreach ($capabilities as $task) {
464+
list ($plugin, $name) = pluginSplit($task['name']);
465+
$conditions['OR'][] = [
466+
'task' => $name,
467+
'completed <' => date('Y-m-d H:i:s', time() - (int)$task['cleanupTimeout'])
468+
];
469+
}
470+
471+
$this->deleteAll($conditions);
472+
}
473+
474+
/**
475+
* Cleanups / delete failed jobs with given capabilities after maximum retries.
476+
*
477+
* @param array $capabilities Available QueueWorkerTasks.
478+
* @return void
479+
*/
480+
public function cleanFailedJobs(array $capabilities): void
481+
{
482+
$conditions = [];
483+
484+
// Generate the job specific conditions.
485+
foreach ($capabilities as $task) {
486+
list ($plugin, $name) = pluginSplit($task['name']);
487+
$conditions['OR'][] = [
488+
'task' => $name,
489+
'failed_count >' => $task['retries']
490+
];
461491
}
462492

463-
$this->deleteAll([
464-
'completed <' => time() - (int)Configure::read('Queue.cleanuptimeout')
465-
]);
493+
$this->deleteAll($conditions);
466494
}
467495

468496
/**

src/Shell/QueueShell.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public function runworker(): void
208208
}
209209
if ($this->_exit || mt_rand(0, 100) > (100 - Config::gcprob())) {
210210
$this->out(__d('queue', 'Performing old job cleanup.'));
211-
$this->QueuedTasks->cleanOldJobs();
211+
$this->QueuedTasks->cleanOldJobs($this->_getTaskConf());
212212
}
213213
$this->hr();
214214
}
@@ -276,12 +276,20 @@ protected function runJob(QueuedTask $queuedTask): void
276276
*/
277277
public function clean(): void
278278
{
279-
if (!Configure::read('Queue.cleanupTimeout')) {
280-
$this->abort('You disabled cleanuptimout in config. Aborting.');
281-
}
279+
$this->out(__d('queue', 'Deleting old completed jobs, that have had cleanup timeout.'));
280+
$this->QueuedTasks->cleanOldJobs($this->_getTaskConf());
281+
}
282282

283-
$this->out('Deleting old jobs, that have finished before ' . date('Y-m-d H:i:s', time() - (int)Configure::read('Queue.cleanupTimeout')));
284-
$this->QueuedTasks->cleanOldJobs();
283+
/**
284+
* Manually trigger a Failed job cleanup.
285+
*
286+
* @return void
287+
*/
288+
//@codingStandardsIgnoreLine
289+
public function clean_failed(): void
290+
{
291+
$this->out(__d('queue', 'Deleting failed jobs, that have had maximum worker retries.'));
292+
$this->QueuedTasks->cleanFailedJobs($this->_getTaskConf());
285293
}
286294

287295
/**
@@ -363,6 +371,10 @@ public function getOptionParser(): ConsoleOptionParser
363371
'help' => 'Remove old jobs (cleanup)',
364372
'parser' => $subcommandParser
365373
])
374+
->addSubcommand('clean_failed', [
375+
'help' => 'Remove old failed jobs (cleanup)',
376+
'parser' => $subcommandParser
377+
])
366378
->addSubcommand('add', [
367379
'help' => 'Add Job',
368380
'parser' => $subcommandParser

src/Shell/Task/QueueTask.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,6 @@ abstract class QueueTask extends Shell implements QueueTaskInterface
3030
*/
3131
public $QueuedTasks;
3232

33-
/**
34-
* Timeout for run, after which the Task is reassigned to a new worker.
35-
*
36-
* @var int
37-
*/
38-
public $timeout = 120;
39-
40-
/**
41-
* Number of times a failed instance of this task should be restarted before giving up.
42-
*
43-
* @var int
44-
*/
45-
public $retries = 1;
46-
4733
/**
4834
*
4935
* @param \Cake\Console\ConsoleIo|null $io IO

0 commit comments

Comments
 (0)