parent
9c08114008
commit
b6057dc730
|
@ -60,6 +60,8 @@ interface Constraint
|
|||
/**
|
||||
* In case blockCode() returns Task::DELAYED, this method shall return the actual delay time
|
||||
*
|
||||
* Must provide a unix timestamp when to retry execution.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function delayTime(): int;
|
||||
|
|
|
@ -0,0 +1,169 @@
|
|||
<?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\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;
|
||||
use FuzeWorks\Logger;
|
||||
|
||||
/**
|
||||
* Class DependencyConstraint
|
||||
*
|
||||
* Use this constraint to prevent a task from running if one or multiple tasks this task depends on hasn't completed yet.
|
||||
* This constraint also can be applied to queue multiple tasks. Make a chain of tasks that depend on one another.
|
||||
*
|
||||
* @package FuzeWorks\Async\Constraint
|
||||
*/
|
||||
class DependencyConstraint implements Constraint
|
||||
{
|
||||
|
||||
public $dependencies = [];
|
||||
|
||||
protected $delayTimes = 3;
|
||||
|
||||
protected $returnStatus = Task::DELAYED;
|
||||
|
||||
/**
|
||||
* DependencyConstraint 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
|
||||
* @throws TasksException
|
||||
*/
|
||||
public function __construct(array $dependencyList, int $delayTimes = 3)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue