From: Tim Düsterhus Date: Sun, 14 Jun 2015 16:25:23 +0000 (+0200) Subject: Add BackgroundQueueCleanUpCronjob X-Git-Tag: 3.0.0_Beta_1~2249^2~3^2~5 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=4da75834d4b468efaaa7d681212b9e2668af2c71;p=GitHub%2FWoltLab%2FWCF.git Add BackgroundQueueCleanUpCronjob --- diff --git a/com.woltlab.wcf/cronjob.xml b/com.woltlab.wcf/cronjob.xml index ef8c93d744..b8bab7a83e 100644 --- a/com.woltlab.wcf/cronjob.xml +++ b/com.woltlab.wcf/cronjob.xml @@ -79,6 +79,19 @@ 1 + + + + + */10 + * + * + * + * + 1 + 1 + + diff --git a/wcfsetup/install/files/lib/system/cronjob/BackgroundQueueCleanUpCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/BackgroundQueueCleanUpCronjob.class.php new file mode 100644 index 0000000000..657ff2befc --- /dev/null +++ b/wcfsetup/install/files/lib/system/cronjob/BackgroundQueueCleanUpCronjob.class.php @@ -0,0 +1,81 @@ + + * @package com.woltlab.wcf + * @subpackage system.cronjob + * @category Community Framework + */ +class BackgroundQueueCleanUpCronjob extends AbstractCronjob { + /** + * @see \wcf\system\cronjob\ICronjob::execute() + */ + public function execute(Cronjob $cronjob) { + parent::execute($cronjob); + + WCF::getDB()->beginTransaction(); + $commited = false; + try { + $sql = "SELECT jobID, job + FROM wcf".WCF_N."_background_job + WHERE status = ? + AND time <= ? + ORDER BY time ASC, jobID ASC + FOR UPDATE"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute([ + 'processing', + TIME_NOW - 600 // running longer than 10 minutes + ]); + + $jobIDs = [ ]; + while ($row = $statement->fetchArray()) { + $jobIDs[] = $row['jobID']; + + try { + // no shut up operator, exception will be caught + $job = unserialize($row['job']); + if ($job) { + $job->fail(); + + if ($job->getFailures() <= $job::MAX_FAILURES) { + BackgroundQueueHandler::getInstance()->enqueue($job, TIME_NOW + $job->retryAfter()); + } + } + } + catch (\Exception $e) { + // job is completely broken: log + if ($e instanceof LoggedException) $e->getExceptionID(); + } + } + + if (empty($jobIDs)) { + WCF::getDB()->commitTransaction(); + $commited = true; + return; + } + + // delete jobs + $condition = new PreparedStatementConditionBuilder(); + $condition->add('jobID IN (?)', [ $jobIDs ]); + $sql = "DELETE FROM wcf".WCF_N."_background_job ".$condition; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute($condition->getParameters()); + + WCF::getDB()->commitTransaction(); + $commited = true; + } + finally { + if (!$commited) WCF::getDB()->rollbackTransaction(); + } + } +}