250 lines
7.5 KiB
PHP
250 lines
7.5 KiB
PHP
<?php
|
|
/**
|
|
* FuzeWorks Async Library
|
|
*
|
|
* The FuzeWorks PHP FrameWork
|
|
*
|
|
* Copyright (C) 2013-2020 TechFuze
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
* @author TechFuze
|
|
* @copyright Copyright (c) 2013 - 2020, TechFuze. (http://techfuze.net)
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
*
|
|
* @link http://techfuze.net/fuzeworks
|
|
* @since Version 1.0.0
|
|
*
|
|
* @version Version 1.0.0
|
|
*/
|
|
|
|
namespace FuzeWorks\Async\Handler;
|
|
use FuzeWorks\Async\Constraint;
|
|
use FuzeWorks\Async\Constraint\DependencyConstraint;
|
|
use FuzeWorks\Async\Handler;
|
|
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 DependentTaskHandler implements Handler
|
|
{
|
|
|
|
/**
|
|
* @var Handler
|
|
*/
|
|
protected $parentHandler;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $output;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $dependencyList;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private $delayTimes;
|
|
|
|
/**
|
|
* DependentTaskHandler constructor.
|
|
*
|
|
* To add dependencies, the following array should be supplied:
|
|
* $dependencyList: array(string 'taskId', string 'taskId', string 'taskId')
|
|
*
|
|
* @param array $dependencyList
|
|
* @param int $delayTimes Time that a task should be delayed before retrying
|
|
*/
|
|
public function __construct(array $dependencyList = [], int $delayTimes = 3)
|
|
{
|
|
$this->dependencyList = $dependencyList;
|
|
$this->delayTimes = $delayTimes;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function init(Task $task)
|
|
{
|
|
if (!empty($this->dependencyList))
|
|
$task->addConstraint(new DependencyConstraint($this->dependencyList, $this->delayTimes));
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function primaryHandler(Task $task): bool
|
|
{
|
|
// First find all the dependencies
|
|
try {
|
|
$dependencies = $this->fetchDependencies($task);
|
|
$this->output = json_encode($dependencies);
|
|
return true;
|
|
} catch (TasksException $e) {
|
|
$this->output = 'Failed to fetch dependencies. TasksException: ' . $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getOutput(): string
|
|
{
|
|
return $this->output;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function postHandler(Task $task)
|
|
{
|
|
// First find all the dependencies
|
|
try {
|
|
$dependencies = $this->fetchDependencies($task);
|
|
$this->output = json_encode($dependencies);
|
|
return true;
|
|
} catch (TasksException $e) {
|
|
$this->output = 'Failed to fetch dependencies. TasksException: ' . $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getPostOutput(): string
|
|
{
|
|
return $this->output;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getParentHandler(): ?Handler
|
|
{
|
|
return $this->parentHandler;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function setParentHandler(Handler $parentHandler): void
|
|
{
|
|
$this->parentHandler = $parentHandler;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function setParentInput(string $input): void
|
|
{
|
|
// Parent output gets set at this handler's output.
|
|
// Only if this class has something to intervene it will override the parent output
|
|
// Which should be always... but alas.
|
|
$this->output = $input;
|
|
}
|
|
|
|
/**
|
|
* @param Task $task
|
|
* @return array
|
|
* @throws TasksException
|
|
*/
|
|
protected function fetchDependencies(Task $task): array
|
|
{
|
|
// When it receives the task, all dependencies should already be handled
|
|
// the primary handler will therefore connect with the DependencyConstraint and fetch dependencies
|
|
$constraints = $task->getConstraints();
|
|
|
|
// First prepare a list of dependencies
|
|
$dependencies = [];
|
|
$dependencyConstraints = [];
|
|
foreach ($constraints as $constraint) {
|
|
if ($constraint instanceof Constraint)
|
|
$dependencyConstraints[] = $constraint;
|
|
}
|
|
|
|
// If no dependencies found, throw exception
|
|
if (empty($dependencyConstraints))
|
|
return $dependencies;
|
|
|
|
// Afterwards build a list of dependencies
|
|
/** @var DependencyConstraint $constraint */
|
|
foreach ($dependencyConstraints as $constraint) {
|
|
foreach ($constraint->getDependencies() as $dependency) {
|
|
if (!isset($dependencies[$dependency]))
|
|
$dependencies[$dependency] = [];
|
|
}
|
|
}
|
|
|
|
// Now that all dependencies are determined, fetch all the output
|
|
$tasks = $this->loadTasksLib();
|
|
$taskStorage = $tasks->getTaskStorage();
|
|
foreach ($dependencies as $dependency => $data)
|
|
{
|
|
// Fetch the task
|
|
try {
|
|
$dependencyTask = $taskStorage->getTaskById($dependency);
|
|
|
|
// Then fetch all output
|
|
$dependencies[$dependency]['status'] = $dependencyTask->getStatus();
|
|
$dependencies[$dependency]['output'] = $dependencyTask->getOutput();
|
|
$dependencies[$dependency]['errors'] = $dependencyTask->getErrors();
|
|
$dependencies[$dependency]['post'] = $dependencyTask->getPostOutput();
|
|
$dependencies[$dependency]['postErrors'] = $dependencyTask->getPostErrors();
|
|
} catch (TasksException $e) {
|
|
$dependencies[$dependency]['status'] = Task::FAILED;
|
|
$dependencies[$dependency]['output'] = null;
|
|
$dependencies[$dependency]['errors'] = 'Task not found.';
|
|
$dependencies[$dependency]['post'] = null;
|
|
$dependencies[$dependency]['postErrors'] = null;
|
|
}
|
|
}
|
|
|
|
return $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.");
|
|
}
|
|
}
|
|
} |