Implemented other changes requested by Wettennet.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Abel Hoogeveen 2020-07-12 11:54:20 +02:00
parent 4758f4154b
commit be3d5d56e8
4 changed files with 42 additions and 22 deletions

View File

@ -109,7 +109,7 @@ try {
// And finally, run the supervisor // And finally, run the supervisor
try { try {
$supervisor = $lib->getSuperVisor($bootstrap); $supervisor = $lib->getSuperVisor($bootstrap);
while ($supervisor->cycle() !== SuperVisor::RUNNING) { while ($supervisor->cycle() !== SuperVisor::FINISHED) {
usleep(250000); usleep(250000);
} }

View File

@ -76,6 +76,11 @@ class GroupConstraint implements Constraint
$this->maxConcurrent = $maxConcurrent; $this->maxConcurrent = $maxConcurrent;
} }
public function init(Task $task)
{
$task->addAttribute('groupName', $this->groupName);
}
public function getGroupName(): string public function getGroupName(): string
{ {
return $this->groupName; return $this->groupName;

View File

@ -37,7 +37,12 @@
namespace FuzeWorks\Async\Constraint; namespace FuzeWorks\Async\Constraint;
use FuzeWorks\Async\Constraint; use FuzeWorks\Async\Constraint;
use FuzeWorks\Async\Task; 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\Factory;
use FuzeWorks\Libraries;
class GroupDependencyConstraint implements Constraint class GroupDependencyConstraint implements Constraint
{ {
@ -60,8 +65,6 @@ class GroupDependencyConstraint implements Constraint
public function intervene(Task $task, array $tasks): bool public function intervene(Task $task, array $tasks): bool
{ {
// Find whether any task is not completed // Find whether any task is not completed
$hasFailed = false;
$hasUnresolved = false;
foreach ($tasks as $t) foreach ($tasks as $t)
{ {
foreach ($t->getConstraints() as $constraint) foreach ($t->getConstraints() as $constraint)
@ -69,33 +72,24 @@ class GroupDependencyConstraint implements Constraint
// Check whether the constraint is a GroupConstraint // Check whether the constraint is a GroupConstraint
if ($constraint instanceof GroupConstraint && $this->groupName === $constraint->getGroupName()) if ($constraint instanceof GroupConstraint && $this->groupName === $constraint->getGroupName())
{ {
// Check whether GroupConstraint has failed $this->returnStatus = Task::DELAYED;
if ($t->getStatus() === Task::CANCELLED) return true;
$hasFailed = true; }
// If the task is not cancelled and not completed, there are unresolved tasks
elseif ($t->getStatus() !== Task::COMPLETED)
$hasUnresolved = true;
} }
} }
// If there are failed tasks, cancel this dependent // After all known tasks have run, check if all have ended without a CANCELLED status
if ($hasFailed) $taskStorage = $this->loadTasksLib()->getTaskStorage();
$tasks = $taskStorage->getTasksByAttribute('groupName', $this->groupName);
foreach ($tasks as $t)
{
if ($t->getStatus() === Task::CANCELLED)
{ {
$this->returnStatus = Task::CANCELLED; $this->returnStatus = Task::CANCELLED;
$task->setOutput('', 'Task cancelled due to failed group dependency.'); $task->setOutput('', 'Task cancelled due to failed group dependency.');
return true; 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; return false;
} }
@ -115,4 +109,25 @@ class GroupDependencyConstraint implements Constraint
{ {
return time() + 3; 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.");
}
}
} }

View File

@ -79,7 +79,7 @@ interface TaskStorage
* *
* @param string $attributeKey * @param string $attributeKey
* @param string $attributeValue * @param string $attributeValue
* @return array * @return Task[]
*/ */
public function getTasksByAttribute(string $attributeKey, string $attributeValue): array; public function getTasksByAttribute(string $attributeKey, string $attributeValue): array;