/** * Parse and execute the scheduled tasks in the specified file. * @param $file string */ function parseTasks($file) { $xmlParser = new XMLParser(); $tree = $xmlParser->parse($file); if (!$tree) { $xmlParser->destroy(); printf("Unable to parse file \"%s\"!\n", $file); exit(1); } foreach ($tree->getChildren() as $task) { $className = $task->getAttribute('class'); $frequency = $task->getChildByName('frequency'); if (isset($frequency)) { $canExecute = ScheduledTaskHelper::checkFrequency($className, $frequency); } else { // Always execute if no frequency is specified $canExecute = true; } if ($canExecute) { $this->executeTask($className, ScheduledTaskHelper::getTaskArgs($task)); } } $xmlParser->destroy(); }
/** * Get all scheduled tasks that needs to be executed. * @return array */ function _getTasksToRun() { $tasksToRun = array(); $isEnabled = $this->getSetting(0, 'enabled'); if ($isEnabled) { $taskDao =& DAORegistry::getDao('ScheduledTaskDAO'); // Grab the scheduled scheduled tree $scheduledTasks = $this->getSetting(0, 'crontab'); if (is_null($scheduledTasks)) { $this->_parseCrontab(); $scheduledTasks = $this->getSetting(0, 'crontab'); } foreach ($scheduledTasks as $task) { // We don't allow tasks without frequency, see _parseCronTab(). $frequency = new XMLNode(); $frequency->setAttribute(key($task['frequency']), current($task['frequency'])); $canExecute = ScheduledTaskHelper::checkFrequency($task['className'], $frequency); if ($canExecute) { $tasksToRun[] = $task; } } } return $tasksToRun; }
/** * @see PKPPageRouter::loadHandler() */ function callbackLoadHandler($hookName, $args) { $isEnabled = $this->getSetting(0, 'enabled'); if ($isEnabled) { $taskDao = DAORegistry::getDao('ScheduledTaskDAO'); // Grab the scheduled scheduled tree $scheduledTasks = $this->getSetting(0, 'crontab'); if (is_null($scheduledTasks)) { $this->_parseCrontab(); $scheduledTasks = $this->getSetting(0, 'crontab'); } foreach ($scheduledTasks as $task) { // We don't allow tasks without frequency, see _parseCronTab(). $frequency = new XMLNode(); $frequency->setAttribute(key($task['frequency']), current($task['frequency'])); $canExecute = ScheduledTaskHelper::checkFrequency($task['className'], $frequency); if ($canExecute) { // Strip off the package name(s) to get the base class name $className = $task['className']; $pos = strrpos($className, '.'); if ($pos === false) { $baseClassName = $className; } else { $baseClassName = substr($className, $pos + 1); } $taskArgs = array(); if (isset($task['args'])) { $taskArgs = $task['args']; } // There's a race here. Several requests may come in closely spaced. // Each may decide it's time to run scheduled tasks, and more than one // can happily go ahead and do it before the "last run" time is updated. // By updating the last run time as soon as feasible, we can minimize // the race window. TODO: there ought to be a safer way of doing this. $taskDao->updateLastRunTime($className, time()); // Load and execute the task import($className); $task = new $baseClassName($taskArgs); $task->execute(); } } } return false; }