dependencies = $dependencyList; $this->delayTimes = $delayTimes; // Test if async library can be loaded $this->loadTasksLib(); } /** * @inheritDoc */ public function intervene(Task $task): bool { // Fetch taskStorage try { $tasks = $this->loadTasksLib(); $taskStorage = $tasks->getTaskStorage(); // Is any dependency unresolved? $hasUnresolved = false; // Test if any dependency has not been resolved foreach ($this->dependencies as $dependency) { // Get dependency $dependencyTask = $taskStorage->getTaskById($dependency); // If the dependency task is completed, ignore it and continue to next dependency if ($dependencyTask->getStatus() === Task::COMPLETED) continue; elseif ($dependencyTask->getStatus() === Task::CANCELLED) { // Cancel current task $task->setOutput('', 'Task cancelled due to failed dependency.'); $this->returnStatus = Task::CANCELLED; return true; } else $hasUnresolved = true; } // If any unresolved tasks exist, delay task execution if ($hasUnresolved) { $this->returnStatus = Task::DELAYED; return true; } } catch (TasksException $e) { Logger::logError("Could not constraint task. '" . $e->getMessage() . "'"); $this->returnStatus = Task::FAILED; return true; } return false; } /** * @inheritDoc */ public function blockCode(): int { return $this->returnStatus; } /** * @inheritDoc */ public function delayTime(): int { return time() + $this->delayTimes; } /** * Return a list of dependencies * * @return array */ public function getDependencies() { return $this->dependencies; } /** * Load the tasks library, so that dependencies can get scanned later * * @return Tasks * @throws TasksException */ private function loadTasksLib(): Tasks { try { /** @var Libraries $libraries */ $libraries = Factory::getInstance('libraries'); /** @var Tasks $tasks */ $tasks = $libraries->get('async'); return $tasks; } catch (FactoryException | LibraryException $e) { throw new TasksException("Could not constrain task. Async library could not be loaded."); } } }