diff --git a/bin/supervisor b/bin/supervisor index 0542413..bc6f4c1 100644 --- a/bin/supervisor +++ b/bin/supervisor @@ -109,7 +109,7 @@ try { // And finally, run the supervisor try { $supervisor = $lib->getSuperVisor($bootstrap); - while ($supervisor->cycle() !== SuperVisor::RUNNING) { + while ($supervisor->cycle() !== SuperVisor::FINISHED) { usleep(250000); } diff --git a/src/FuzeWorks/Async/Constraint/GroupConstraint.php b/src/FuzeWorks/Async/Constraint/GroupConstraint.php index 86e781e..dc379ae 100644 --- a/src/FuzeWorks/Async/Constraint/GroupConstraint.php +++ b/src/FuzeWorks/Async/Constraint/GroupConstraint.php @@ -76,6 +76,11 @@ class GroupConstraint implements Constraint $this->maxConcurrent = $maxConcurrent; } + public function init(Task $task) + { + $task->addAttribute('groupName', $this->groupName); + } + public function getGroupName(): string { return $this->groupName; diff --git a/src/FuzeWorks/Async/Constraint/GroupDependencyConstraint.php b/src/FuzeWorks/Async/Constraint/GroupDependencyConstraint.php index fe8ce55..5a35723 100644 --- a/src/FuzeWorks/Async/Constraint/GroupDependencyConstraint.php +++ b/src/FuzeWorks/Async/Constraint/GroupDependencyConstraint.php @@ -37,7 +37,12 @@ namespace FuzeWorks\Async\Constraint; use FuzeWorks\Async\Constraint; use FuzeWorks\Async\Task; +use FuzeWorks\Async\Tasks; +use FuzeWorks\Async\TasksException; +use FuzeWorks\Exception\FactoryException; +use FuzeWorks\Exception\LibraryException; use FuzeWorks\Factory; +use FuzeWorks\Libraries; class GroupDependencyConstraint implements Constraint { @@ -60,8 +65,6 @@ class GroupDependencyConstraint implements Constraint public function intervene(Task $task, array $tasks): bool { // Find whether any task is not completed - $hasFailed = false; - $hasUnresolved = false; foreach ($tasks as $t) { foreach ($t->getConstraints() as $constraint) @@ -69,34 +72,25 @@ class GroupDependencyConstraint implements Constraint // Check whether the constraint is a GroupConstraint if ($constraint instanceof GroupConstraint && $this->groupName === $constraint->getGroupName()) { - // Check whether GroupConstraint has failed - if ($t->getStatus() === Task::CANCELLED) - $hasFailed = true; - // If the task is not cancelled and not completed, there are unresolved tasks - elseif ($t->getStatus() !== Task::COMPLETED) - $hasUnresolved = true; + $this->returnStatus = Task::DELAYED; + return true; } } + } - // If there are failed tasks, cancel this dependent - if ($hasFailed) + // After all known tasks have run, check if all have ended without a CANCELLED status + $taskStorage = $this->loadTasksLib()->getTaskStorage(); + $tasks = $taskStorage->getTasksByAttribute('groupName', $this->groupName); + foreach ($tasks as $t) + { + if ($t->getStatus() === Task::CANCELLED) { $this->returnStatus = Task::CANCELLED; $task->setOutput('', 'Task cancelled due to failed group dependency.'); return true; } - elseif ($hasUnresolved) - { - $this->returnStatus = Task::DELAYED; - return true; - } } - // Return the taskList - //$task->setOutput(serialize($tasks), ''); - //$this->returnStatus = Task::CANCELLED; - //return true; - return false; } @@ -115,4 +109,25 @@ class GroupDependencyConstraint implements Constraint { return time() + 3; } + + /** + * 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."); + } + } } \ No newline at end of file diff --git a/src/FuzeWorks/Async/TaskStorage.php b/src/FuzeWorks/Async/TaskStorage.php index ddf5ef8..7709481 100644 --- a/src/FuzeWorks/Async/TaskStorage.php +++ b/src/FuzeWorks/Async/TaskStorage.php @@ -79,7 +79,7 @@ interface TaskStorage * * @param string $attributeKey * @param string $attributeValue - * @return array + * @return Task[] */ public function getTasksByAttribute(string $attributeKey, string $attributeValue): array;