addComponentPath(dirname(__FILE__, 4), Priority::LOW); $this->cfg = $config->getConfig('tasks'); } /** * @param Task $task * @return bool * @throws TasksException */ public function addTask(Task $task): bool { $taskStorage = $this->getTaskStorage(); return $taskStorage->addTask($task); } /** * @param string $bootstrapFile * @return SuperVisor * @throws TasksException */ public function getSuperVisor(string $bootstrapFile): SuperVisor { $cfg = $this->cfg->get('SuperVisor'); $class = 'FuzeWorks\Async\Supervisors\\' . $cfg['type']; $parameters = isset($cfg['parameters']) && is_array($cfg['parameters']) ? $cfg['parameters'] : []; array_unshift($parameters, $this->getTaskStorage(), $this->getExecutor($bootstrapFile)); 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 ShellWorker * @throws TasksException */ public function getWorker(): ShellWorker { return new ShellWorker($this->getTaskStorage()); } /** * Fetch the TaskStorage based on the configured type * * @return TaskStorage * @throws TasksException */ public function getTaskStorage(): TaskStorage { $cfg = $this->cfg->get('TaskStorage'); $class = 'FuzeWorks\Async\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 * * @param string $bootstrapFile * @return Executor * @throws TasksException */ protected function getExecutor(string $bootstrapFile): Executor { $cfg = $this->cfg->get('Executor'); $class = 'FuzeWorks\Async\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($bootstrapFile, $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 null; } /** * @inheritDoc */ public function getSourceDirectory(): ?string { return null; } }