Skip to content

Commit 9afb669

Browse files
author
Philo Hamel
committed
Honor task specific cleanupTimeout
1 parent b7972da commit 9afb669

2 files changed

Lines changed: 50 additions & 12 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() - $task['cleanupTimeout'])
468+
];
469+
}
470+
471+
$this->deleteAll($conditions, false);
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, false);
466494
}
467495

468496
/**

src/Shell/QueueShell.php

Lines changed: 16 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,18 @@ 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+
public function clean_failed() {
289+
$this->out(__d('queue', 'Deleting failed jobs, that have had maximum worker retries.'));
290+
$this->QueuedTasks->cleanFailedJobs($this->_getTaskConf());
285291
}
286292

287293
/**
@@ -363,6 +369,10 @@ public function getOptionParser(): ConsoleOptionParser
363369
'help' => 'Remove old jobs (cleanup)',
364370
'parser' => $subcommandParser
365371
])
372+
->addSubcommand('clean_failed', [
373+
'help' => 'Remove old failed jobs (cleanup)',
374+
'parser' => $subcommandParser
375+
])
366376
->addSubcommand('add', [
367377
'help' => 'Add Job',
368378
'parser' => $subcommandParser

0 commit comments

Comments
 (0)