use wcf\data\cronjob\Cronjob AS CronjobObj;
use wcf\data\cronjob\CronjobEditor;
use wcf\system\cache\CacheHandler;
-use wcf\system\database\condition\PreparedStatementConditionBuilder;
+use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\SystemException;
use wcf\system\package\PackageDependencyHandler;
+use wcf\system\SingletonFactory;
use wcf\system\WCF;
use wcf\util\ClassUtil;
* @subpackage system.cronjob
* @category Community Framework
*/
-abstract class CronjobScheduler {
+class CronjobScheduler extends SingletonFactory {
/**
- * list of outstanding cronjobs
- *
- * @var array<CronjobEditor>
+ * cached times of the next and after next cronjob execution
+ * @var array<integer>
+ */
+ protected $cache = array();
+
+ /**
+ * list of editors for outstanding cronjobs
+ * @var array<wcf\data\cronjob\CronjobEditor>
*/
- protected static $cronjobs = array();
+ protected $cronjobEditors = array();
+
+ /**
+ * @see wcf\system\SingletonFactory::init()
+ */
+ protected function init() {
+ $this->loadCache();
+ }
/**
* Executes outstanding cronjobs.
*/
- public static function execute() {
- $cache = self::getCache();
-
+ public function executeCronjobs() {
// break if there are no outstanding cronjobs
- if ($cache['nextExec'] > TIME_NOW && $cache['afterNextExec'] > TIME_NOW) return;
+ if ($this->cache['nextExec'] > TIME_NOW && $this->cache['afterNextExec'] > TIME_NOW) {
+ return;
+ }
// get outstanding cronjobs
- self::loadCronjobs();
+ $this->loadCronjobs();
// clear cache
self::clearCache();
- foreach (self::$cronjobs as $cronjob) {
+ foreach ($this->cronjobEditors as $cronjobEditor) {
// mark cronjob as being executed
$cronjobEditor->update(array(
'state' => CronjobObj::EXECUTING
$logEditor = new CronjobLogEditor($log);
try {
- self::executeCronjob($cronjob, $logEditor);
+ $this->executeCronjob($cronjobEditor, $logEditor);
}
catch (SystemException $e) {
- self::logResult($logEditor, $e);
+ $this->logResult($logEditor, $e);
}
// get time of next execution
// mark cronjob as done
$cronjobEditor->update(array(
+ 'lastExec' => TIME_NOW,
'afterNextExec' => $afterNextExec,
'failCount' => 0,
'nextExec' => $nextExec,
}
/**
- * Loads and executes outstanding cronjobs.
+ * Loads outstanding cronjobs.
*/
- protected static function loadCronjobs() {
+ protected function loadCronjobs() {
$conditions = new PreparedStatementConditionBuilder();
$conditions->add("cronjob.packageID IN (?)", array(PackageDependencyHandler::getDependencies()));
$conditions->add("(cronjob.nextExec <= ? OR cronjob.afterNextExec <= ?)", array(TIME_NOW, TIME_NOW));
$conditions->add("cronjob.failCount < ?", array(3));
$conditions->add("cronjob.state = ?", array(CronjobObj::READY));
- $sql = "SELECT cronjob.*, package.packageDir
+ $sql = "SELECT cronjob.*
FROM wcf".WCF_N."_cronjob cronjob
- LEFT JOIN wcf".WCF_N."_package package
- ON (package.packageID = cronjob.packageID)
".$conditions;
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute($conditions->getParameters());
$cronjobEditor->update($data);
if ($executeCronjob) {
- self::$cronjobs[] = $cronjobEditor;
+ $this->cronjobEditors[] = $cronjobEditor;
}
}
}
/**
* Executes a cronjob.
*
- * @param CronjobEditor $cronjobEditor
- * @param CronjobLogEditor $logEditor
+ * @param wcf\data\cronjob\CronjobEditor $cronjobEditor
+ * @param wcf\data\cronjob\log\CronjobLogEditor $logEditor
*/
- protected static function executeCronjob(CronjobEditor $cronjobEditor, CronjobLogEditor $logEditor) {
+ protected function executeCronjob(CronjobEditor $cronjobEditor, CronjobLogEditor $logEditor) {
$className = $cronjobEditor->className;
if (!class_exists($className)) {
throw new SystemException("unable to find class '".$className."'");
// execute cronjob
$cronjob = new $className();
- $cronjob->execute();
+ $cronjob->execute($cronjobEditor->getDecoratedObject());
- self::logResult($logEditor);
+ $this->logResult($logEditor);
}
/**
* Logs cronjob exec success or failure.
*
- * @param CronjobLogEditor $log
- * @param SystemException $e
+ * @param wcf\data\cronjob\CronjobEditor $logEditor
+ * @param wcf\system\exception\SystemException $exception
*/
- protected static function logResult(CronjobLogEditor $log, SystemException $e = null) {
+ protected function logResult(CronjobLogEditor $logEditor, SystemException $exception = null) {
if ($exception !== null) {
$errString = implode("\n", array(
- $e->getMessage(),
- $e->getCode(),
- $e->getFile(),
- $e->getLine(),
- $e->getTraceAsString()
+ $exception->getMessage(),
+ $exception->getCode(),
+ $exception->getFile(),
+ $exception->getLine(),
+ $exception->getTraceAsString()
));
$logEditor->update(array(
}
/**
- * Returns cached cronjob data.
- *
- * @return array
+ * Loads the cached data for cronjob execution.
*/
- protected static function getCache() {
+ protected function loadCache() {
$cacheName = 'cronjobs-'.PACKAGE_ID;
CacheHandler::getInstance()->addResource($cacheName, WCF_DIR.'cache/cache.'.$cacheName.'.php', 'wcf\system\cache\builder\CacheBuilderCronjob');
-
- return CacheHandler::getInstance()->get($cacheName);
+ $this->cache = CacheHandler::getInstance()->get($cacheName);
}
/**
- * Clears cronjob cache.
+ * Clears the cronjob data cache.
*/
public static function clearCache() {
- // clear cache
- CacheHandler::getInstance()->clear(WCF_DIR.'cache/', 'cache.cronjobs-'.PACKAGE_ID.'php');
+ CacheHandler::getInstance()->clear(WCF_DIR.'cache/', 'cache.cronjobs-'.PACKAGE_ID.'.php');
}
}