From fc332b275c4a0b216f9918d108e8e16d054815be Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 17 Jun 2015 21:53:09 +0200 Subject: [PATCH] Add BackgroundQueueHandler::enqueueIn() and rename enqueue() to enqueueAt() This also disallows scheduling jobs for execution in the past to prevent starving of jobs with a non-immediate execution (previous default parameter of zero). --- .../BackgroundQueueHandler.class.php | 21 +++++++++++++++++-- .../BackgroundQueueCleanUpCronjob.class.php | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php b/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php index a1beb8adb7..00519189dc 100644 --- a/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php +++ b/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php @@ -2,6 +2,7 @@ namespace wcf\system\background; use wcf\system\background\job\AbstractBackgroundJob; use wcf\system\exception\LoggedException; +use wcf\system\exception\SystemException; use wcf\system\SingletonFactory; use wcf\system\WCF; @@ -16,6 +17,18 @@ use wcf\system\WCF; * @category Community Framework */ class BackgroundQueueHandler extends SingletonFactory { + /** + * Enqueues the given job for execution in the specified number of + * seconds. Defaults to "as soon as possible" (0 seconds). + * + * @param \wcf\system\background\job\AbstractBackgroundJob $job The job to enqueue. + * @param int $time Minimum number of seconds to wait before performing the job. + * @see \wcf\system\background\BackgroundQueueHandler::enqueueAt() + */ + public function enqueueIn(AbstractBackgroundJob $job, $time = 0) { + return self::enqueueAt($job, TIME_NOW + $time); + } + /** * Enqueues the given job for execution at the given time. * Note: The time is a minimum time. Depending on the size of @@ -24,7 +37,11 @@ class BackgroundQueueHandler extends SingletonFactory { * @param \wcf\system\background\job\AbstractBackgroundJob $job The job to enqueue. * @param int $time Earliest time to consider the job for execution. */ - public function enqueue(AbstractBackgroundJob $job, $time = 0) { + public function enqueueAt(AbstractBackgroundJob $job, $time) { + if ($time < TIME_NOW) { + throw new SystemException("You may not schedule a job in the past (".$time." is smaller than the current timestamp ".TIME_NOW.")."); + } + $sql = "INSERT INTO wcf".WCF_N."_background_job (job, time) VALUES (?, ?)"; @@ -55,7 +72,7 @@ class BackgroundQueueHandler extends SingletonFactory { $job->fail(); if ($job->getFailures() <= $job::MAX_FAILURES) { - $this->enqueue($job, TIME_NOW + $job->retryAfter()); + $this->enqueueIn($job, $job->retryAfter()); } else { // job failed too often: log diff --git a/wcfsetup/install/files/lib/system/cronjob/BackgroundQueueCleanUpCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/BackgroundQueueCleanUpCronjob.class.php index 657ff2befc..d19dfd34af 100644 --- a/wcfsetup/install/files/lib/system/cronjob/BackgroundQueueCleanUpCronjob.class.php +++ b/wcfsetup/install/files/lib/system/cronjob/BackgroundQueueCleanUpCronjob.class.php @@ -48,7 +48,7 @@ class BackgroundQueueCleanUpCronjob extends AbstractCronjob { $job->fail(); if ($job->getFailures() <= $job::MAX_FAILURES) { - BackgroundQueueHandler::getInstance()->enqueue($job, TIME_NOW + $job->retryAfter()); + BackgroundQueueHandler::getInstance()->enqueueIn($job, $job->retryAfter()); } } } -- 2.20.1