argv[0])) { $this->file = $this->argv[0]; } else { $this->file = TASKS_REGISTRY_FILE; } if (!file_exists($this->file) || !is_readable($this->file)) { printf("Tasks file \"%s\" does not exist or is not readable!\n", $this->file); exit(1); } $this->taskDao = DAORegistry::getDAO('ScheduledTaskDAO'); } /** * Print command usage information. */ function usage() { echo "Script to run a set of scheduled tasks\n" . "Usage: {$this->scriptName} [tasks_file]\n"; } /** * Parse and execute the scheduled tasks. */ function execute() { $this->parseTasks($this->file); } /** * 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(); } /** * Execute the specified task. * @param $className string the class name to execute * @param $args array the array of arguments to pass to the class constructors */ function executeTask($className, $args) { // Load and execute the task if (!is_object($task = instantiate($className, null, null, 'execute', $args))) { fatalError('Cannot instantiate task class.'); } $this->taskDao->updateLastRunTime($className); $task->execute(); } } ?>