addComponentPath(dirname(__FILE__), Priority::LOW); $this->cfg = $config->getConfig('tasks'); } /** * @param Task $task * @return bool * @throws TasksException */ public function queueTask(Task $task): bool { $taskStorage = $this->getTaskStorage(); return $taskStorage->addTask($task); } /** * @throws TasksException */ public function getSuperVisor(): SuperVisor { $cfg = $this->cfg->get('SuperVisor'); $class = 'Application\Library\Tasks\Supervisors\\' . $cfg['type']; $parameters = isset($cfg['parameters']) && is_array($cfg['parameters']) ? $cfg['parameters'] : []; array_unshift($parameters, $this->getTaskStorage(), $this->getExecutor()); if (!class_exists($class, true)) throw new TasksException("Could not get SuperVisor. Type of '$class' not found."); $object = new $class(...$parameters); if (!$object instanceof SuperVisor) throw new TasksException("Could not get SuperVisor. Type of '$class' is not instanceof TaskStorage."); return $object; } /** * @return Worker * @throws TasksException */ public function getWorker(): Worker { return new Worker($this->getTaskStorage()); } /** * Fetch the TaskStorage based on the configured type * * @return TaskStorage * @throws TasksException */ public function getTaskStorage(): TaskStorage { $cfg = $this->cfg->get('TaskStorage'); $class = 'Application\Library\Tasks\TaskStorage\\' . $cfg['type']; $parameters = isset($cfg['parameters']) && is_array($cfg['parameters']) ? $cfg['parameters'] : []; if (!class_exists($class, true)) throw new TasksException("Could not get TaskStorage. Type of '$class' not found."); $object = new $class(...$parameters); if (!$object instanceof TaskStorage) throw new TasksException("Could not get TaskStorage. Type '$class' is not instanceof TaskStorage."); return $object; } /** * Fetch the Executor based on the configured type * * @return Executor * @throws TasksException */ protected function getExecutor(): Executor { $cfg = $this->cfg->get('Executor'); $class = 'Application\Library\Tasks\Executors\\' . $cfg['type']; $parameters = isset($cfg['parameters']) && is_array($cfg['parameters']) ? $cfg['parameters'] : []; if (!class_exists($class, true)) throw new TasksException("Could not get Executor. Type of '$class' not found."); $object = new $class(...$parameters); if (!$object instanceof Executor) throw new TasksException("Could not get Executor. Type '$class' is not instanceof Executor."); return $object; } /** * @inheritDoc */ public function getClassesPrefix(): ?string { return '\Application\Library\Tasks'; } /** * @inheritDoc */ public function getSourceDirectory(): ?string { return 'src'; } }