From 3968d7c197a2bb112753161849fdfe84f8180d92 Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Mon, 17 Aug 2020 15:46:23 +0200 Subject: [PATCH] Make GC on exit configurable --- README.md | 10 ++++++++-- src/Queue/Config.php | 9 +++++++++ src/Shell/QueueShell.php | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6ee81c3..ad247b4 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Enable the plugin within your src/Application.php bootstrap function: $this->addPlugin('Queue'); ``` -It is also advised to have the `posix` PHP extension enabled. +It is also advised to have the `posix` PHP extension enabled. ## Configuration @@ -79,6 +79,12 @@ You may create a file called `app_queue.php` inside your `config` folder (NOT th $config['Queue']['cleanupTimeout'] = 2592000; // 30 days ``` +- Whether or not to cleanup on exit: + + ```php + $config['Queue']['gcOnExit'] = true; + ``` + Don't forget to load that config file with `Configure::load('app_queue');` in your bootstrap. You can also use `Plugin::load('Queue', ['bootstrap' => true]);` which will load your `app_queue.php` config file automatically. @@ -106,7 +112,7 @@ You can set two main things on each task as property: timeout and retries. * @var int */ public $timeout = 120; - + /** * Number of times a failed instance of this task should be restarted before giving up. * diff --git a/src/Queue/Config.php b/src/Queue/Config.php index 592336a..5d3bfdc 100644 --- a/src/Queue/Config.php +++ b/src/Queue/Config.php @@ -59,4 +59,13 @@ public static function defaultWorkerRetries(): int { return Configure::read('Queue.defaultWorkerRetries', 4); } + + /** + * + * @return int + */ + public static function gcOnExit(): bool + { + return Configure::read('Queue.gcOnExit', true); + } } diff --git a/src/Shell/QueueShell.php b/src/Shell/QueueShell.php index 1e9155a..da2353c 100644 --- a/src/Shell/QueueShell.php +++ b/src/Shell/QueueShell.php @@ -206,7 +206,7 @@ public function runworker(): void $this->_exit = true; $this->out(__d('queue', 'Reached runtime of ' . (time() - $startTime) . ' Seconds (Max ' . Configure::readOrFail('Queue.workerMaxRuntime') . '), terminating.')); } - if (mt_rand(0, 100) > (100 - Config::gcprob())) { + if (($this->_exit && Config::gcOnExit()) || mt_rand(0, 100) > (100 - Config::gcprob())) { $this->out(__d('queue', 'Performing old job cleanup.')); $this->QueuedTasks->cleanOldJobs($this->_getTaskConf()); }