/** * Registers a new executable task. * * @param mixed $task Either path of the task class filename (relative * to Stud.IP root) or an instance of CronJob * @param bool $active Indicates whether the task should be set active * or not * @return String Id of the created task * @throws InvalidArgumentException when the task class file does not * exist * @throws RuntimeException when task has already been registered */ public function registerTask($task, $active = true) { if (is_object($task)) { $reflection = new ReflectionClass($task); $class = $reflection->getName(); $class_filename = str_replace($GLOBALS['STUDIP_BASE_PATH'] . '/', '', $reflection->getFileName()); } else { $filename = $GLOBALS['STUDIP_BASE_PATH'] . '/' . $task; if (!file_exists($filename)) { $message = sprintf('Task class file "%s" does not exist.', $task); throw new InvalidArgumentException($message); } $class_filename = $task; $classes = get_declared_classes(); require_once $filename; $class = end(array_diff(get_declared_classes(), $classes)); if (empty($class)) { throw new RuntimeException('No class was defined in file.'); } $reflection = new ReflectionClass($class); } if (!$reflection->isSubclassOf('CronJob')) { $message = sprintf('Job class "%s" (defined in %s) does not extend the abstract CronJob class.', $class, $filename); throw new RuntimeException($message); } if ($task = CronjobTask::findOneByClass($class)) { return $task->task_id; } $task = new CronjobTask(); $task->filename = $class_filename; $task->class = $class; $task->active = (int) $active; $task->store(); return $task->task_id; }
/** * Unregisters a previously registered task. * * @param String $task_id Id of the task to be unregistered * @return CronjobScheduler to allow chaining * @throws InvalidArgumentException when no task with the given id exists */ public static function unregister() { $class_name = get_called_class(); $task = CronjobTask::findOneByClass($class_name); if ($task !== null) { CronjobScheduler::getInstance()->unregisterTask($task->id); } }