Async/Tasks/src/Task.php

358 lines
8.5 KiB
PHP

<?php
/**
* FuzeWorks CLIComponent.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2019 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 - 2019, TechFuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 1.2.0
*
* @version Version 1.2.0
*/
namespace Application\Library\Tasks;
class Task
{
const PENDING = 1;
const RUNNING = 2;
const FAILED = 3;
const SUCCESS = 4;
const POST = 5;
const COMPLETED = 6;
const DELAYED = 7;
const CANCELLED = 8;
/**
* @var string
*/
protected $taskId;
/**
* @var string
*/
protected $handlerClass;
/**
* @var array
*/
protected $arguments;
/**
* @var int
*/
protected $status = Task::PENDING;
/**
* @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 $data = [];
/**
* Whether the post handler should be called after every task execution
*
* @var bool
*/
protected $callPostHandler = false;
/**
* Whether the post handler should be called after execution has failed
*
* @var bool
*/
protected $callPostHandlerWhenFailed = false;
/**
* 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 string $handlerClass The class that shall handle this task
* @param mixed $parameters,... The arguments provided to the method that shall handle this class
* @throws TasksException
*/
public function __construct(string $identifier, string $handlerClass)
{
$this->taskId = $identifier;
$this->handlerClass = $handlerClass;
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;
}
/**
* Returns the unique ID of this task
*
* @return string
*/
public function getId(): string
{
return $this->taskId;
}
/**
* Gets the name of the class that shall process this task
*
* @return string
*/
public function getHandlerClass(): string
{
return $this->handlerClass;
}
/**
* 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.
*
* @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;
}
/**
* Fetch an attribute of this task
*
* @param string $key
* @return mixed
*/
public function attribute(string $key)
{
if (!isset($this->data[$key]))
return null;
return $this->data[$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->data[$key] = $value;
}
/**
* Return the output of this task execution
*
* @return string|null
*/
public function getOutput(): ?string
{
return $this->output;
}
public function getPostOutput(): ?string
{
return $this->postOutput;
}
/**
* Sets the conditions for when the Handler::postHandler should be called.
*
* If you want the postHandler to only get called on errors, set onEvery to false and onFail to true.
*
* @param bool $onEvery Call the post handler on every result
* @param bool $onFail Call the post handler only when failed
*/
public function setPostHandler(bool $onEvery = false, bool $onFail = false)
{
$this->callPostHandler = $onEvery;
$this->callPostHandlerWhenFailed = $onFail;
}
public function getCallPostHandler(): bool
{
return $this->callPostHandler;
}
public function getCallPostHandlerWhenFailed(): bool
{
return $this->callPostHandlerWhenFailed;
}
/**
* Return the errors of this task execution
*
* @return string|null
*/
public function getErrors(): ?string
{
return $this->errors;
}
public function getPostErrors(): ?string
{
return $this->postErrors;
}
public function setOutput(string $output, string $errors)
{
if (!is_null($this->output) || !is_null($this->errors))
throw new TasksException("Could not set output. Output already set.");
$this->output = $output;
$this->errors = $errors;
}
public function setPostOutput(string $output, string $errors)
{
if (!is_null($this->postOutput) || !is_null($this->postErrors))
throw new TasksException("Could not set post output. Output already set.");
$this->postOutput = $output;
$this->postErrors = $errors;
}
/**
* Checks whether an object can be serialized
*
* @param $value
* @return bool
*/
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;
}
}