559 lines
14 KiB
PHP
559 lines
14 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;
|
|
class Task
|
|
{
|
|
|
|
/**
|
|
* If a task is waiting to be executed, its status will be PENDING
|
|
*/
|
|
const PENDING = 1;
|
|
|
|
/**
|
|
* If a task is currently in process, its status will be RUNNING
|
|
*/
|
|
const RUNNING = 2;
|
|
|
|
/**
|
|
* If a task does not reach the goal it sets out to, its status should be set to FAILED.
|
|
*
|
|
* Not to be confused with Process::FAILED. Process::FAILED will be the Process status when a Process running a Task unexpectedly stops running.
|
|
* This will set the Task to Task::PFAILED
|
|
*/
|
|
const FAILED = 3;
|
|
const PFAILED = 4;
|
|
|
|
/**
|
|
* If a task has reached its goal, its status should be set to SUCCESS
|
|
*/
|
|
const SUCCESS = 5;
|
|
|
|
/**
|
|
* If a task is in process of its postHandler, its status will be POST
|
|
*/
|
|
const POST = 6;
|
|
|
|
/**
|
|
* If a task has completed all its goals, its status will be COMPLETED
|
|
*/
|
|
const COMPLETED = 7;
|
|
|
|
/**
|
|
* If a task has a timed based constraint, its status will be DELAYED
|
|
*
|
|
* Task is delayed by the time in $delayTime
|
|
*/
|
|
const DELAYED = 8;
|
|
|
|
/**
|
|
* If a task has a constraint preventing it from running at any point, its status will be CANCELLED
|
|
*/
|
|
const CANCELLED = 9;
|
|
|
|
public static function getStatusType($statusId)
|
|
{
|
|
switch ($statusId) {
|
|
case 1:
|
|
return 'Task::PENDING';
|
|
case 2:
|
|
return 'Task::RUNNING';
|
|
case 3:
|
|
return 'Task::FAILED';
|
|
case 4:
|
|
return 'Task::PFAILED';
|
|
case 5:
|
|
return 'Task::SUCCESS';
|
|
case 6:
|
|
return 'Task::POST';
|
|
case 7:
|
|
return 'Task::COMPLETED';
|
|
case 8:
|
|
return 'Task::DELAYED';
|
|
case 9:
|
|
return 'Task::CANCELLED';
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $taskId;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
protected $status = Task::PENDING;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $arguments;
|
|
|
|
/**
|
|
* @var Handler
|
|
*/
|
|
protected $handler;
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
protected $usePostHandler = false;
|
|
|
|
/**
|
|
* @var Constraint[]
|
|
*/
|
|
protected $constraints = [];
|
|
|
|
/**
|
|
* When the status of this task is Task::DELAYED, this is the amount of time in seconds before it should be tried again.
|
|
*
|
|
* @var int Delay time in seconds
|
|
*/
|
|
protected $delayTime = 0;
|
|
|
|
/**
|
|
* The output of the execution of this task
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $output;
|
|
protected $postOutput;
|
|
|
|
/**
|
|
* The errors thrown by the execution of this task
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $errors;
|
|
protected $postErrors;
|
|
|
|
/**
|
|
* Contains a key, value array with properties of this Task
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $attributes = [];
|
|
|
|
/* -------- Some settings ------------ */
|
|
|
|
protected $retryOnFail = false;
|
|
protected $maxRetries = 2;
|
|
protected $retryPFailures = true;
|
|
protected $retryRFailures = true;
|
|
protected $retryPostFailures = true;
|
|
protected $retries = 0;
|
|
protected $maxTime = 30;
|
|
|
|
/**
|
|
* Task constructor.
|
|
*
|
|
* Creates a Task object, which can be added to the TaskQueue.
|
|
*
|
|
* @param string $identifier The unique identifier of this task. Make sure it is always unique!
|
|
* @param Handler $handler The Handler object which will run the Task in the Worker
|
|
* @param bool $usePostHandler Whether the postHandler on Handler should also be used
|
|
* @param mixed $parameters,... The arguments provided to the method that shall handle this class
|
|
* @throws TasksException
|
|
*/
|
|
public function __construct(string $identifier, Handler $handler, bool $usePostHandler = false)
|
|
{
|
|
// Check if the provided Handler is serializable
|
|
if (!$this->isSerializable($handler))
|
|
throw new TasksException("Could not create Task. Provided Handler is not serializable.");
|
|
|
|
$this->taskId = $identifier;
|
|
$this->handler = $handler;
|
|
$this->usePostHandler = $usePostHandler;
|
|
if (func_num_args() > 3)
|
|
$args = array_slice(func_get_args(), 3);
|
|
else
|
|
$args = [];
|
|
|
|
foreach ($args as $arg)
|
|
if (!$this->isSerializable($arg))
|
|
throw new TasksException("Could not create Task. Provided arguments are not serializable.");
|
|
|
|
$this->arguments = $args;
|
|
|
|
// Init the handler
|
|
$this->handler->init($this);
|
|
}
|
|
|
|
/**
|
|
* Returns the unique ID of this task
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getId(): string
|
|
{
|
|
return $this->taskId;
|
|
}
|
|
|
|
/**
|
|
* Gets the Handler that shall process this task
|
|
*
|
|
* @return Handler
|
|
*/
|
|
public function getHandler(): Handler
|
|
{
|
|
return $this->handler;
|
|
}
|
|
|
|
/**
|
|
* Whether the postHandler on the Handler should be invoked after processing the initial task.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function getUsePostHandler(): bool
|
|
{
|
|
return $this->usePostHandler;
|
|
}
|
|
|
|
/**
|
|
* Gets the arguments to be provided to the method of the class that shall process this task
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getArguments(): array
|
|
{
|
|
return $this->arguments;
|
|
}
|
|
|
|
/**
|
|
* Add a constraint to this Task; in order to make the execution conditional.
|
|
*
|
|
* @param Constraint $constraint
|
|
*/
|
|
public function addConstraint(Constraint $constraint)
|
|
{
|
|
$this->constraints[] = $constraint;
|
|
}
|
|
|
|
/**
|
|
* Gets the constraints to this job.
|
|
*
|
|
* @return Constraint[]
|
|
*/
|
|
public function getConstraints(): array
|
|
{
|
|
return $this->constraints;
|
|
}
|
|
|
|
/**
|
|
* Gets the current status of this Task
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getStatus(): int
|
|
{
|
|
return $this->status;
|
|
}
|
|
|
|
/**
|
|
* Sets the status of this Task.
|
|
*
|
|
* Must be one of the constants of this Task class
|
|
*
|
|
* @param int $status
|
|
*/
|
|
public function setStatus(int $status)
|
|
{
|
|
$this->status = $status;
|
|
}
|
|
|
|
/**
|
|
* Set the delay time to the Task
|
|
*
|
|
* @param int $delayTime
|
|
*/
|
|
public function setDelayTime(int $delayTime)
|
|
{
|
|
$this->delayTime = $delayTime;
|
|
}
|
|
|
|
/**
|
|
* Receive the delay time for this task
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getDelayTime(): int
|
|
{
|
|
return $this->delayTime;
|
|
}
|
|
|
|
/* ---------------------------------- Attributes setters and getters ------------------ */
|
|
|
|
/**
|
|
* Fetch an attribute of this task
|
|
*
|
|
* @param string $key
|
|
* @return mixed
|
|
*/
|
|
public function attribute(string $key)
|
|
{
|
|
if (!isset($this->attributes[$key]))
|
|
return null;
|
|
|
|
return $this->attributes[$key];
|
|
}
|
|
|
|
/**
|
|
* Adds an attribute to this task.
|
|
*
|
|
* @param string $key
|
|
* @param $value
|
|
* @throws TasksException
|
|
*/
|
|
public function addAttribute(string $key, $value)
|
|
{
|
|
if (!$this->isSerializable($value))
|
|
throw new TasksException("Could not set Task '$this->taskId' attribute '$key'. Value not serializable.");
|
|
|
|
$this->attributes[$key] = $value;
|
|
}
|
|
|
|
/**
|
|
* Remove an attribute from a Task
|
|
*
|
|
* @param string $key
|
|
* @throws TasksException
|
|
*/
|
|
public function removeAttribute(string $key)
|
|
{
|
|
if (!isset($this->attributes[$key]))
|
|
throw new TasksException("Could not remove Task '$this->taskId' attribute '$key'. Not found.");
|
|
|
|
unset($this->attributes[$key]);
|
|
}
|
|
|
|
/* ---------------------------------- Output setters and getters ---------------------- */
|
|
|
|
/**
|
|
* Return the output of this task execution
|
|
*
|
|
* @return string|null
|
|
*/
|
|
public function getOutput(): ?string
|
|
{
|
|
return $this->output;
|
|
}
|
|
|
|
public function getPostOutput(): ?string
|
|
{
|
|
return $this->postOutput;
|
|
}
|
|
|
|
/**
|
|
* Return the errors of this task execution
|
|
*
|
|
* @return string|null
|
|
*/
|
|
public function getErrors(): ?string
|
|
{
|
|
return $this->errors;
|
|
}
|
|
|
|
public function getPostErrors(): ?string
|
|
{
|
|
return $this->postErrors;
|
|
}
|
|
|
|
/**
|
|
* @param string $output
|
|
* @param string $errors
|
|
* @todo Handle output from multiple attempts
|
|
*/
|
|
public function setOutput(string $output, string $errors)
|
|
{
|
|
$this->output = $output;
|
|
$this->errors = $errors;
|
|
}
|
|
|
|
/**
|
|
* @param string $output
|
|
* @param string $errors
|
|
* @todo Handle output from multiple attempts
|
|
*/
|
|
public function setPostOutput(string $output, string $errors)
|
|
{
|
|
$this->postOutput = $output;
|
|
$this->postErrors = $errors;
|
|
}
|
|
|
|
/* ---------------------------------- Failure settings and criteria ------------------- */
|
|
|
|
/**
|
|
* Set whether this task should retry after a failure, and how many times
|
|
*
|
|
* @param bool $retryOnFail Whether this task should be retried if failing
|
|
* @param int $maxRetries How many times the task should be retried
|
|
* @param int $maxTime How long a task may run before it shall be forcefully shut down
|
|
* @param bool $retryRegularFailures Whether regular Task::FAILED should be retried
|
|
* @param bool $retryProcessFailures Whether process based Task::PFAILED should be retried
|
|
* @param bool $retryPostFailures Whether failures during the Task::POST phase should be retried.
|
|
*/
|
|
public function setSettings(bool $retryOnFail, int $maxRetries = 2, int $maxTime = 30, bool $retryRegularFailures = true, bool $retryProcessFailures = true, bool $retryPostFailures = true)
|
|
{
|
|
$this->retryOnFail = $retryOnFail;
|
|
$this->maxRetries = $maxRetries;
|
|
$this->retryPFailures = $retryProcessFailures;
|
|
$this->retryRFailures = $retryRegularFailures;
|
|
$this->retryPostFailures = $retryPostFailures;
|
|
$this->maxTime = $maxTime;
|
|
}
|
|
|
|
/**
|
|
* Returns the failure retry settings
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getSettings(): array
|
|
{
|
|
return [
|
|
'retryOnFail' => $this->retryOnFail,
|
|
'maxRetries' => $this->maxRetries,
|
|
'retryPFailures' => $this->retryPFailures,
|
|
'retryRFailures' => $this->retryRFailures,
|
|
'retryPostFailures' => $this->retryPostFailures,
|
|
'maxTime' => $this->maxTime
|
|
];
|
|
}
|
|
|
|
/* ---------------------------------- Retries and attempts registers ------------------ */
|
|
|
|
/**
|
|
* Add a retry to the retry counter
|
|
*/
|
|
public function addRetry()
|
|
{
|
|
$this->retries++;
|
|
}
|
|
|
|
/**
|
|
* Reset the retry counter back to 0
|
|
*/
|
|
public function resetRetries()
|
|
{
|
|
$this->retries = 0;
|
|
}
|
|
|
|
/**
|
|
* Receive the amount of retries already attempted
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getRetries(): int
|
|
{
|
|
return $this->retries;
|
|
}
|
|
|
|
/* ---------------------------------- Runtime data getters and setters----------------- */
|
|
|
|
protected $taskStartTime;
|
|
protected $taskEndTime;
|
|
protected $postStartTime;
|
|
protected $postEndTime;
|
|
|
|
public function startTaskTime()
|
|
{
|
|
$this->taskEndTime = null;
|
|
$this->taskStartTime = time();
|
|
}
|
|
|
|
public function endTaskTime()
|
|
{
|
|
$this->taskEndTime = time();
|
|
}
|
|
|
|
public function getTaskTime(): ?int
|
|
{
|
|
if (is_null($this->taskStartTime))
|
|
return null;
|
|
|
|
if (is_null($this->taskEndTime))
|
|
return time() - $this->taskStartTime;
|
|
|
|
return $this->taskEndTime - $this->taskStartTime;
|
|
}
|
|
|
|
public function startPostTime()
|
|
{
|
|
$this->postEndTime = null;
|
|
$this->postStartTime = time();
|
|
}
|
|
|
|
public function endPostTime()
|
|
{
|
|
$this->postEndTime = time();
|
|
}
|
|
|
|
public function getPostTime(): ?int
|
|
{
|
|
if (is_null($this->postStartTime))
|
|
return null;
|
|
|
|
if (is_null($this->postEndTime))
|
|
return time() - $this->postStartTime;
|
|
|
|
return $this->postEndTime - $this->postStartTime;
|
|
}
|
|
|
|
/**
|
|
* Checks whether an object can be serialized
|
|
*
|
|
* @param $value
|
|
* @return bool
|
|
* @todo Improve so it is properly tested
|
|
*/
|
|
private function isSerializable($value)
|
|
{
|
|
$return = true;
|
|
$arr = array($value);
|
|
|
|
array_walk_recursive($arr, function ($element) use (&$return) {
|
|
if (is_object($element) && get_class($element) == 'Closure') {
|
|
$return = false;
|
|
}
|
|
});
|
|
|
|
return $return;
|
|
}
|
|
|
|
|
|
} |