WIP: 3-features #5

Closed
abelhooge wants to merge 9 commits from 3-features into master
23 changed files with 1703 additions and 175 deletions

9
.drone.yml Normal file
View File

@ -0,0 +1,9 @@
kind: pipeline
type: docker
name: test
steps:
- name: composer
image: composer:latest
commands:
- composer install

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ composer.lock
.idea/
log/
vendor/
build/
test/temp/

View File

@ -1,7 +1,12 @@
FROM php:7.3-cli-buster
RUN apt-get update &&\
apt-get install --no-install-recommends --assume-yes --quiet procps ca-certificates curl git &&\
apt-get install --no-install-recommends --assume-yes --quiet procps ca-certificates curl git unzip &&\
rm -rf /var/lib/apt/lists/*
# PDO
RUN docker-php-ext-install pdo_mysql
# Install Redis
RUN pecl install redis-5.1.1 && docker-php-ext-enable redis
RUN pecl install xdebug && docker-php-ext-enable xdebug
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

View File

@ -40,7 +40,9 @@ use FuzeWorks\Async\Tasks;
use FuzeWorks\Async\TasksException;
use FuzeWorks\Exception\InvalidArgumentException;
use FuzeWorks\Exception\LibraryException;
use FuzeWorks\Factory;
// First perform a PHP version check
if (version_compare('7.1.0', PHP_VERSION, '>')) {
fwrite(
STDERR,
@ -63,35 +65,56 @@ $autoloaders = [
];
foreach ($autoloaders as $file)
if (file_exists($file))
require($file);
require_once($file);
// If a bootstrap is provided, use that one
$arguments = getopt('', ['bootstrap:']);
if (!isset($arguments['bootstrap']) || empty($arguments['bootstrap']))
{
fwrite(STDERR, "Could not load supervisor. No bootstrap provided.");
die(1);
}
// Load the file. If it doesn't exist, fail.
$bootstrap = $arguments['bootstrap'];
if (!file_exists($bootstrap))
{
fwrite(STDERR, "Could not load supervisor. Provided bootstrap doesn't exist.");
die(1);
}
// Load the bootstrap
/** @var Factory $container */
$container = require($bootstrap);
// Check if container is a Factory
if (!$container instanceof Factory)
{
fwrite(STDERR, "Could not load supervisor. Provided bootstrap is not a valid bootstrap.");
die(1);
}
// Check if the Async library is already loaded. If not, load it.
// @todo: Better check in libraries for existing library
try {
// Open configurator
$configurator = new FuzeWorks\Configurator();
// Set up basic settings
$configurator->setTimeZone('Europe/Amsterdam');
$configurator->setTempDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'temp');
$configurator->setLogDirectory(dirname(__FILE__). DIRECTORY_SEPARATOR . 'log');
// Add Async library
$configurator->deferComponentClassMethod('libraries', 'addLibraryClass', null, 'async', '\FuzeWorks\Async\Tasks');
// Debug
$configurator->enableDebugMode()->setDebugAddress('ALL');
// Create container
$container = $configurator->createContainer();
// RUN THE APP
/** @var Tasks $lib */
$lib = $container->libraries->get('async');
} catch (LibraryException $e) {
$container->libraries->addLibraryClass('async', '\FuzeWorks\Async\Tasks');
/** @var Tasks $lib */
$lib = $container->libraries->get('async');
}
$supervisor = $lib->getSuperVisor();
// And finally, run the supervisor
try {
$supervisor = $lib->getSuperVisor($bootstrap);
while ($supervisor->cycle() === SuperVisor::RUNNING) {
usleep(250000);
}
// Write results
fwrite(STDOUT, "SuperVisor finished scheduled tasks.");
} catch (InvalidArgumentException | TasksException | LibraryException $e) {
fwrite(STDERR, sprintf('FuzeWorks Async could not load.' . PHP_EOL .
'Exception: ' . $e->getMessage() . PHP_EOL)

125
bin/worker Normal file
View File

@ -0,0 +1,125 @@
#!/usr/bin/env 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
*/
use FuzeWorks\Async\Tasks;
use FuzeWorks\Async\TasksException;
use FuzeWorks\Exception\InvalidArgumentException;
use FuzeWorks\Exception\LibraryException;
use FuzeWorks\Factory;
// First perform a PHP version check
if (version_compare('7.1.0', PHP_VERSION, '>')) {
fwrite(
STDERR,
sprintf(
'FuzeWorks Async requires PHP 7.1 or higher.' . PHP_EOL .
'You are using PHP %s (%s).' . PHP_EOL,
PHP_VERSION,
PHP_BINARY
)
);
die(1);
}
// First load composer
$autoloaders = [
__DIR__ . '/../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/vendor/autoload.php'
];
foreach ($autoloaders as $file)
if (file_exists($file))
require_once($file);
// If a bootstrap is provided, use that one
$arguments = getopt('', ['bootstrap:']);
if (!isset($arguments['bootstrap']) || empty($arguments['bootstrap']))
{
fwrite(STDERR, "Could not load worker. No bootstrap provided.");
die(1);
}
// Load the file. If it doesn't exist, fail.
$file = $arguments['bootstrap'];
if (!file_exists($file))
{
fwrite(STDERR, "Could not load worker. Provided bootstrap doesn't exist.");
die(1);
}
// Load the bootstrap
/** @var Factory $container */
$container = require($file);
// Check if container is a Factory
if (!$container instanceof Factory)
{
fwrite(STDERR, "Could not load worker. Provided bootstrap is not a valid bootstrap.");
die(1);
}
// Check if the Async library is already loaded. If not, load it.
// @todo: Better check in libraries for existing library
try {
/** @var Tasks $lib */
$lib = $container->libraries->get('async');
} catch (LibraryException $e) {
$container->libraries->addLibraryClass('async', '\FuzeWorks\Async\Tasks');
/** @var Tasks $lib */
$lib = $container->libraries->get('async');
}
// Fetch arguments for the worker
$arguments = getopt("t:p::");
if (!isset($arguments['t']))
{
fwrite(STDERR, "Could not load worker. No taskID provided.");
die(1);
}
// Prepare arguments
$taskID = base64_decode($arguments['t']);
$post = isset($arguments['p']);
// RUN THE APP
$worker = $lib->getWorker();
$worker->run($taskID, $post);
fwrite(STDOUT,'Finished task \'' . $taskID . "'");
?>

View File

@ -15,12 +15,22 @@
"require": {
"php": ">=7.2.0",
"fuzeworks/core": "~1.2.0",
"ext-json": "*"
"ext-json": "*",
"ext-redis": "*"
},
"require-dev": {
"phpunit/phpunit": "^9",
"fuzeworks/mvcr": "~1.2.0"
},
"config": {
"platform": {
"ext-redis": "1"
}
},
"autoload": {
"psr-4": {
"FuzeWorks\\Async\\": "src/FuzeWorks/Async"
}
},
"bin": ["bin/supervisor"]
"bin": ["bin/supervisor", "bin/worker"]
}

View File

@ -42,12 +42,30 @@ return array(
'type' => 'ArrayTaskStorage',
// For ArrayTaskStorage, first parameter is the file location of the array storage
'parameters' => [dirname(__FILE__) . DS . 'storage.php']
'parameters' => [
'filename' => dirname(__FILE__) . DS . 'storage.php'
],
// For RedisTaskStorage, parameters are connection properties
#'parameters' => [
# // Type can be 'tcp' or 'unix'
# 'socket_type' => 'tcp',
# // If socket_type == 'unix', set the socket here
# 'socket' => null,
# // If socket_type == 'tcp', set the host here
# 'host' => 'localhost',
#
# 'password' => null,
# 'port' => 6379,
# 'timeout' => 0
#]
],
'Executor' => [
'type' => 'ShellExecutor',
// For ShellExecutor, first parameter is the file location of the worker script
'parameters' => [dirname(__FILE__) . DS . 'worker.php']
'parameters' => [
'workerFile' => dirname(__FILE__) . DS . 'bin' . DS . 'worker'
]
]
);

View File

@ -45,22 +45,28 @@ class ShellExecutor implements Executor
private $binary;
private $worker;
private $bootstrapFile;
private $stdout = "> /dev/null";
private $stderr = "2> /dev/null";
/**
* ShellExecutor constructor.
*
* @param string $workerFile The worker script that shall run individual tasks
* @param string $bootstrapFile
* @param array $parameters
* @throws TasksException
*/
public function __construct(string $workerFile)
public function __construct(string $bootstrapFile, array $parameters)
{
// Fetch workerFile
$workerFile = $parameters['workerFile'];
// First determine the PHP binary
$this->binary = PHP_BINDIR . DS . 'php';
$this->bootstrapFile = $bootstrapFile;
if (!file_exists($workerFile))
throw new TasksException("Could not construct ShellExecutor. Worker script does not exist.");
throw new TasksException("Could not construct ShellExecutor. ShellWorker script does not exist.");
$this->worker = $workerFile;
}
@ -77,7 +83,7 @@ class ShellExecutor implements Executor
public function startTask(Task $task, bool $post = false): Task
{
// First prepare the command used to spawn workers
$commandString = "$this->binary $this->worker %s ".($post ? 'post' : 'run')." $this->stdout $this->stderr & echo $!";
$commandString = "$this->binary $this->worker --bootstrap=".$this->bootstrapFile." -t %s ".($post ? '-p' : '')." $this->stdout $this->stderr & echo $!";
// Then execute the command using the base64_encoded string of the taskID
$output = $this->shellExec($commandString, [base64_encode($task->getId())]);

View File

@ -0,0 +1,175 @@
<?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\Handler;
use FuzeWorks\Async\Task;
use FuzeWorks\Async\TasksException;
use FuzeWorks\Controller;
use FuzeWorks\Controllers;
use FuzeWorks\Exception\ControllerException;
use FuzeWorks\Exception\FactoryException;
use FuzeWorks\Exception\NotFoundException;
use FuzeWorks\Factory;
class ControllerHandler implements Handler
{
/**
* @var string Name of the controller used to handle the task
*/
protected $controllerName;
/**
* @var string The specific method to handle the task
*/
protected $controllerMethod;
/**
* @var string|null The method used to handle the post phase; if requested
*/
protected $postMethod = null;
protected $output;
protected $postOutput;
/**
* @inheritDoc
* @throws TasksException
*/
public function primaryHandler(Task $task): bool
{
// Set the arguments
$args = $this->setArguments($task);
// First we fetch the controller
$controller = $this->getController($this->controllerName);
// Check if method exists
if (!method_exists($controller, $this->controllerMethod))
throw new TasksException("Could not handle task. Method '$this->controllerMethod' not found on controller.");
// Call method and collect output
$this->output = call_user_func_array([$controller, $this->controllerMethod], $args);
return true;
}
/**
* @inheritDoc
*/
public function getOutput()
{
return $this->output;
}
/**
* @inheritDoc
* @throws TasksException
*/
public function postHandler(Task $task)
{
// Set the arguments
$this->setArguments($task);
// Abort if no postMethod exists
if (is_null($this->postMethod))
throw new TasksException("Could not handle task. No post method provided.");
// First we fetch the controller
$controller = $this->getController($this->controllerName);
// Check if method exists
if (!method_exists($controller, $this->postMethod))
throw new TasksException("Could not handle task. Post method '$this->postMethod' not found on controller.");
// Call method and collect output
$this->postOutput = call_user_func_array([$controller, $this->postMethod], [$task]);
return true;
}
/**
* @inheritDoc
*/
public function getPostOutput()
{
return $this->postOutput;
}
/**
* Set the arguments of this handler using the provided task
*
* @param Task $task
* @return array
* @throws TasksException
*/
public function setArguments(Task $task): array
{
// Direct arguments
$args = $task->getArguments();
if (count($args) < 2)
throw new TasksException("Could not handle task. Not enough arguments provided.");
// First argument: controllerName
$this->controllerName = $args[0];
$this->controllerMethod = $args[1];
$this->postMethod = isset($args[2]) ? $args[2] : null;
return !array_key_exists(2, $args) ? [] : array_slice($args, 3);
}
/**
* @param string $controllerName
* @return Controller
* @throws TasksException
*/
private function getController(string $controllerName): Controller
{
// First load the controllers component
try {
/** @var Controllers $controllers */
$controllers = Factory::getInstance('controllers');
// Load the requested controller
return $controllers->get($controllerName);
} catch (FactoryException $e) {
throw new TasksException("Could not get controller. FuzeWorks\MVCR is not installed!");
} catch (ControllerException $e) {
throw new TasksException("Could not get controller. Controller threw exception: '" . $e->getMessage() . "'");
} catch (NotFoundException $e) {
throw new TasksException("Could not get controller. Controller was not found.");
}
}
}

View File

@ -42,7 +42,7 @@ use FuzeWorks\Exception\EventException;
use FuzeWorks\Logger;
use FuzeWorks\Priority;
class Worker
class ShellWorker
{
/**
@ -75,7 +75,11 @@ class Worker
public function run(string $taskID, bool $post = false)
{
// First fetch the task
$task = $this->taskStorage->getTaskById($taskID);
try {
$task = $this->taskStorage->getTaskById($taskID);
} catch (TasksException $e) {
throw new TasksException("Could not run worker. Task not found.");
}
// Fire a taskHandleEvent
/** @var TaskHandleEvent $event */
@ -159,6 +163,10 @@ class Worker
$errors = $this->getErrors();
$this->output('', $errors);
// If no task is set yet, abort error logging to task
if (is_null($this->task))
return;
try {
// Write to TaskStorage
if (!$this->post)

View File

@ -80,7 +80,7 @@ class ParallelSuperVisor implements SuperVisor
for ($i=0;$i<count($this->tasks);$i++)
{
$task = $this->tasks[$i];
// PENDING: should start if not constrained
if ($task->getStatus() === Task::PENDING)
{
@ -97,6 +97,7 @@ class ParallelSuperVisor implements SuperVisor
// Modify the task in TaskStorage
$this->taskStorage->modifyTask($task);
fwrite(STDOUT, "\nChanged status of task '".$task->getId()."' to status " . Task::getStatusType($task->getStatus()));
}
// DELAYED: If task is delayed, and enough time has passed, change the status back to pending
@ -104,6 +105,7 @@ class ParallelSuperVisor implements SuperVisor
{
$task->setStatus(Task::PENDING);
$this->taskStorage->modifyTask($task);
fwrite(STDOUT, "\nChanged status of task '".$task->getId()."' to status " . Task::getStatusType($task->getStatus()));
}
// CANCELLED/COMPLETED: remove the task if requested to do so
@ -139,6 +141,7 @@ class ParallelSuperVisor implements SuperVisor
// If any changes have been made, they should be written to TaskStorage
$this->taskStorage->modifyTask($task);
fwrite(STDOUT, "\nChanged status of task '".$task->getId()."' to status " . Task::getStatusType($task->getStatus()));
}
// FAILED: if a process has failed, attempt to rety if requested to do so
@ -161,6 +164,7 @@ class ParallelSuperVisor implements SuperVisor
$task = $this->executor->startTask($task);
$task->setStatus(Task::RUNNING);
$this->taskStorage->modifyTask($task);
fwrite(STDOUT, "\nChanged status of task '".$task->getId()."' to status " . Task::getStatusType($task->getStatus()));
continue;
}
}
@ -176,6 +180,7 @@ class ParallelSuperVisor implements SuperVisor
$task->setStatus(Task::CANCELLED);
$this->taskStorage->modifyTask($task);
fwrite(STDOUT, "\nChanged status of task '".$task->getId()."' to status " . Task::getStatusType($task->getStatus()));
}
// SUCCESS: if a task has succeeded, see if it needs a postHandler
@ -191,6 +196,7 @@ class ParallelSuperVisor implements SuperVisor
$task->setStatus(Task::COMPLETED);
$this->taskStorage->modifyTask($task);
fwrite(STDOUT, "\nChanged status of task '".$task->getId()."' to status " . Task::getStatusType($task->getStatus()));
}
// POST: when a task is currently running in it's postHandler
@ -226,6 +232,7 @@ class ParallelSuperVisor implements SuperVisor
// If any changes have been made, they should be written to TaskStorage
$this->taskStorage->modifyTask($task);
fwrite(STDOUT, "\nChanged status of task '".$task->getId()."' to status " . Task::getStatusType($task->getStatus()));
}
}
@ -263,6 +270,7 @@ class ParallelSuperVisor implements SuperVisor
// Save changes to TaskStorage
$this->taskStorage->modifyTask($task);
fwrite(STDOUT, "\nChanged status of task '".$task->getId()."' to status " . Task::getStatusType($task->getStatus()));
}
}

View File

@ -170,11 +170,6 @@ class Task
*/
protected $attributes = [];
/**
* @var Process
*/
protected $process;
/* -------- Some settings ------------ */
protected $retryOnFail = false;
@ -375,9 +370,9 @@ class Task
/**
* @todo Handle output from multiple attempts
* @param string $output
* @param string $errors
* @todo Handle output from multiple attempts
*/
public function setOutput(string $output, string $errors)
{
@ -386,9 +381,9 @@ class Task
}
/**
* @todo Handle output from multiple attempts
* @param string $output
* @param string $errors
* @todo Handle output from multiple attempts
*/
public function setPostOutput(string $output, string $errors)
{
@ -396,39 +391,6 @@ class Task
$this->postErrors = $errors;
}
/**
* Sets the initial process for this task.
*
* To be set by Executor
*
* @param Process $process
*/
public function setProcess(Process $process)
{
$this->process = $process;
}
/**
* Returns the initial process for this task
*
* @return Process|null
*/
public function getProcess(): ?Process
{
return $this->process;
}
public function removeProcess(): bool
{
if ($this->process instanceof Process)
{
$this->process = null;
return true;
}
return false;
}
/**
* Set whether this task should retry after a failure, and how many times
*
@ -495,7 +457,8 @@ class Task
* @param $value
* @return bool
*/
private function isSerializable ($value) {
private function isSerializable($value)
{
$return = true;
$arr = array($value);

View File

@ -39,6 +39,14 @@ namespace FuzeWorks\Async;
interface TaskStorage
{
/**
* TaskStorage constructor.
*
* @throws TasksException
* @param array $parameters from config file
*/
public function __construct(array $parameters);
/**
* Add a task to the TaskStorage.
*
@ -124,9 +132,12 @@ interface TaskStorage
*
* $attempt refers to $task->getRetries(). If 0, it is the initial attempt. If > 0, it seeks a retry output.
*
* Returns null because that is a very valid response. Oftentimes output will need to be checked and its undesirable
* to always throw an exception for expected behaviour.
*
* @param Task $task
* @param int $attempt
* @return array
* @return array|null
*/
public function readTaskOutput(Task $task, int $attempt = 0): ?array;
@ -139,9 +150,21 @@ interface TaskStorage
*
* $attempt refers to $task->getRetries(). If 0, it is the initial attempt. If > 0, it seeks a retry output.
*
* Returns null because that is a very valid response. Oftentimes output will need to be checked and its undesirable
* to always throw an exception for expected behaviour.
*
* @param Task $task
* @param int $attempt
* @return array|null
*/
public function readPostOutput(Task $task, int $attempt = 0): ?array;
/**
* Reset the TaskStorage.
*
* Remove all tasks and their output from the storage so the TaskStorage begins anew.
*
* @return bool
*/
public function reset(): bool;
}

View File

@ -60,13 +60,13 @@ class ArrayTaskStorage implements TaskStorage
protected $tasks = [];
/**
* ArrayTaskStorage constructor.
*
* @param string $fileName Name of the Storage file
* @throws TasksException
* @inheritDoc
*/
public function __construct(string $fileName)
public function __construct(array $parameters)
{
// Load the filename for this taskStorage
$fileName = $parameters['filename'];
if (!file_exists($fileName))
throw new TasksException("Could not construct ArrayTaskStorage. Storage file '$fileName' doesn't exist.");
@ -262,6 +262,15 @@ class ArrayTaskStorage implements TaskStorage
return ['output' => $data['output'], 'errors' => $data['errors'], 'statusCode' => $data['statusCode']];
}
/**
* @inheritDoc
*/
public function reset(): bool
{
// @todo Implement
return false;
}
private function commit()
{
$this->data = ['tasks' => []];

View File

@ -0,0 +1,227 @@
<?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\TaskStorage;
use FuzeWorks\Async\Task;
use FuzeWorks\Async\TasksException;
use FuzeWorks\Async\TaskStorage;
/**
* Class DummyTaskStorage
*
* DummyTaskStorage is used when you don't want persistent task storage. Data is simply stored in an array and
* not saved anywhere. When the program terminates all contents are disposed off. Particularly useful for testing
* and development.
*
* @package FuzeWorks\Async\TaskStorage
*/
class DummyTaskStorage implements TaskStorage
{
/**
* Contains the entire task storage
*
* @var Task[]
*/
protected $tasks;
/**
* An array containing all task outputs
*
* @var array
*/
protected $taskOutput;
/**
* @inheritDoc
*/
public function __construct(array $parameters)
{
$this->tasks = array();
$this->taskOutput = array();
}
/**
* @inheritDoc
* @throws TasksException
*/
public function addTask(Task $task): bool
{
// Check if the already exists
$taskId = $task->getId();
foreach ($this->tasks as $t)
{
if ($t->getId() === $taskId)
throw new TasksException("Could not add Task to TaskStorage. Task '$taskId' already exists.");
}
$this->tasks[] = $task;
return true;
}
/**
* @inheritDoc
*/
public function readTasks(): array
{
return $this->tasks;
}
/**
* @inheritDoc
*/
public function refreshTasks()
{// Ignore
}
/**
* @inheritDoc
*/
public function getTaskById(string $identifier): Task
{
foreach ($this->tasks as $t)
if ($t->getId() === $identifier)
return $t;
throw new TasksException("Could not get task by id. Task not found.");
}
/**
* @inheritDoc
*/
public function modifyTask(Task $task): bool
{
$taskId = $task->getId();
for ($i=0;$i<count($this->tasks);$i++)
{
if ($this->tasks[$i]->getId() === $taskId)
{
$this->tasks[$i] = $task;
return true;
}
}
throw new TasksException("Could not modify task. Task '$taskId' doesn't exist.");
}
/**
* @inheritDoc
* @throws TasksException
*/
public function deleteTask(Task $task): bool
{
$taskId = $task->getId();
for ($i=0;$i<count($this->tasks);$i++)
{
if ($this->tasks[$i]->getId() === $taskId)
{
// Remove the task from the main storage
unset($this->tasks[$i]);
return true;
}
}
throw new TasksException("Could not delete task. Task '$taskId' doesn't exist.");
}
/**
* @inheritDoc
*/
public function writeTaskOutput(Task $task, string $output, string $errors, int $statusCode, int $attempt = 0): bool
{
if (isset($this->taskOutput[$task->getId()]['task'][$attempt]))
throw new TasksException("Could not write task output. Output already written.");
$this->taskOutput[$task->getId()]['task'][$attempt] = [
'output' => $output,
'errors' => $errors,
'statusCode' => $statusCode
];
return true;
}
/**
* @inheritDoc
*/
public function writePostOutput(Task $task, string $output, string $errors, int $statusCode, int $attempt = 0): bool
{
if (isset($this->taskOutput[$task->getId()]['post'][$attempt]))
throw new TasksException("Could not write task post output. Output already written.");
$this->taskOutput[$task->getId()]['post'][$attempt] = [
'output' => $output,
'errors' => $errors,
'statusCode' => $statusCode
];
return true;
}
/**
* @inheritDoc
*/
public function readTaskOutput(Task $task, int $attempt = 0): ?array
{
if (isset($this->taskOutput[$task->getId()]['task'][$attempt]))
return $this->taskOutput[$task->getId()]['task'][$attempt];
return null;
}
/**
* @inheritDoc
*/
public function readPostOutput(Task $task, int $attempt = 0): ?array
{
if (isset($this->taskOutput[$task->getId()]['post'][$attempt]))
return $this->taskOutput[$task->getId()]['post'][$attempt];
return null;
}
/**
* @inheritDoc
*/
public function reset(): bool
{
$this->tasks = [];
$this->taskOutput = [];
return true;
}
}

View File

@ -0,0 +1,291 @@
<?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\TaskStorage;
use FuzeWorks\Async\Task;
use FuzeWorks\Async\TasksException;
use FuzeWorks\Async\TaskStorage;
use Redis;
use RedisException;
class RedisTaskStorage implements TaskStorage
{
/**
* @var Redis
*/
protected $conn;
protected $indexSet = 'async_index';
protected $key_prefix = 'async_task_';
/**
* @inheritDoc
*/
public function __construct(array $parameters)
{
// Attempt to connect to Redis
try {
$this->conn = new Redis();
// Afterwards connect to server
$socketType = $parameters['socket_type'];
if ($socketType == 'unix')
$success = $this->conn->connect($parameters['socket']);
elseif ($socketType == 'tcp')
$success = $this->conn->connect($parameters['host'], $parameters['port'], $parameters['timeout']);
else
$success = false;
// If unsuccessful, return false
if (!$success)
throw new TasksException("Could not construct RedisTaskStorage. Failed to connect.");
// Otherwise attempt authentication, if needed
if (isset($parameters['password']) && !$this->conn->auth($parameters['password']))
throw new TasksException("Could not construct RedisTaskStorage. Authentication failure.");
} catch (RedisException $e) {
throw new TasksException("Could not construct RedisTaskStorage. RedisException thrown: '" . $e->getMessage() . "'");
}
}
/**
* @inheritDoc
* @throws TasksException
*/
public function addTask(Task $task): bool
{
// Check if the task doesn't exist yet
$taskId = $task->getId();
// Query the index
$isMember = $this->conn->sIsMember($this->indexSet, $taskId);
if ($isMember)
throw new TasksException("Could not add Task to TaskStorage. Task '$taskId' already exists.");
// Serialize the task and save it
$taskData = serialize($task);
$this->conn->set($this->key_prefix . $taskId, $taskData);
$this->conn->sAdd($this->indexSet, $taskId);
return true;
}
/**
* @inheritDoc
*/
public function readTasks(): array
{
return $this->refreshTasks();
}
/**
* @inheritDoc
*/
public function refreshTasks()
{
// First fetch an array of all tasks in the set
$taskList = $this->conn->sMembers($this->indexSet);
// Go over each taskId and fetch the specific task
$tasks = [];
foreach ($taskList as $taskId)
$tasks[] = unserialize($this->conn->get($this->key_prefix . $taskId));
return $tasks;
}
/**
* @inheritDoc
*/
public function getTaskById(string $identifier): Task
{
// Query the index
$isMember = $this->conn->sIsMember($this->indexSet, $identifier);
if (!$isMember)
throw new TasksException("Could not get task by ID. Task not found.");
// Fetch the task
/** @var Task $task */
$task = unserialize($this->conn->get($this->key_prefix . $identifier));
// Return the task
return $task;
}
/**
* @inheritDoc
*/
public function modifyTask(Task $task): bool
{
// First get the task ID
$taskId = $task->getId();
// Check if it exists
$isMember = $this->conn->sIsMember($this->indexSet, $taskId);
if (!$isMember)
throw new TasksException("Could not modify task. Task '$taskId' already exists.");
// And write the data
$taskData = serialize($task);
return $this->conn->set($this->key_prefix . $taskId, $taskData);
}
/**
* @inheritDoc
* @throws TasksException
*/
public function deleteTask(Task $task): bool
{
// First get the task ID
$taskId = $task->getId();
// Check if it exists
$isMember = $this->conn->sIsMember($this->indexSet, $taskId);
if (!$isMember)
throw new TasksException("Could not modify task. Task '$taskId' already exists.");
// Delete the key
$this->conn->del($this->key_prefix . $taskId);
$this->conn->sRem($this->indexSet, $taskId);
// Remove all task output and post output
$settings = $task->getRetrySettings();
$maxRetries = $settings['maxRetries'];
for ($i=0;$i<=$maxRetries;$i++)
{
// First remove all possible task output
if ($this->conn->exists($this->key_prefix . $taskId . '_output_' . $i))
$this->conn->del($this->key_prefix . $taskId . '_output_' . $i);
if ($this->conn->exists($this->key_prefix . $taskId . '_post_' . $i))
$this->conn->del($this->key_prefix . $taskId . '_post_' . $i);
}
return true;
}
/**
* @inheritDoc
*/
public function writeTaskOutput(Task $task, string $output, string $errors, int $statusCode, int $attempt = 0): bool
{
// First get the task ID
$taskId = $task->getId();
// Check if the key already exists
if ($this->conn->exists($this->key_prefix . $taskId . '_output_' . $attempt))
throw new TasksException("Could not write task output. Output already written.");
// Prepare contents
$contents = ['taskId' => $task->getId(), 'output' => $output, 'errors' => $errors, 'statusCode' => $statusCode];
$data = serialize($contents);
// Write contents
return $this->conn->set($this->key_prefix . $taskId . '_output_' . $attempt, $data);
}
/**
* @inheritDoc
*/
public function writePostOutput(Task $task, string $output, string $errors, int $statusCode, int $attempt = 0): bool
{
// First get the task ID
$taskId = $task->getId();
// Check if the key already exists
if ($this->conn->exists($this->key_prefix . $taskId . '_post_' . $attempt))
throw new TasksException("Could not write post output. Output already written.");
// Prepare contents
$contents = ['taskId' => $task->getId(), 'output' => $output, 'errors' => $errors, 'statusCode' => $statusCode];
$data = serialize($contents);
// Write contents
return $this->conn->set($this->key_prefix . $taskId . '_post_' . $attempt, $data);
}
/**
* @inheritDoc
*/
public function readTaskOutput(Task $task, int $attempt = 0): ?array
{
// First get the task ID
$taskId = $task->getId();
// Check if the key already exists
if (!$this->conn->exists($this->key_prefix . $taskId . '_output_' . $attempt))
return null;
// Load and convert the data
$data = $this->conn->get($this->key_prefix . $taskId . '_output_' . $attempt);
$data = unserialize($data);
return ['output' => $data['output'], 'errors' => $data['errors'], 'statusCode' => $data['statusCode']];
}
/**
* @inheritDoc
*/
public function readPostOutput(Task $task, int $attempt = 0): ?array
{
// First get the task ID
$taskId = $task->getId();
// Check if the key already exists
if (!$this->conn->exists($this->key_prefix . $taskId . '_post_' . $attempt))
return null;
// Load and convert the data
$data = $this->conn->get($this->key_prefix . $taskId . '_post_' . $attempt);
$data = unserialize($data);
return ['output' => $data['output'], 'errors' => $data['errors'], 'statusCode' => $data['statusCode']];
}
/**
* @inheritDoc
* @throws TasksException
*/
public function reset(): bool
{
// First get a list of all tasks
foreach ($this->readTasks() as $task)
$this->deleteTask($task);
return true;
}
}

View File

@ -68,21 +68,23 @@ class Tasks implements iLibrary
* @return bool
* @throws TasksException
*/
public function queueTask(Task $task): bool
public function addTask(Task $task): bool
{
$taskStorage = $this->getTaskStorage();
return $taskStorage->addTask($task);
}
/**
* @param string $bootstrapFile
* @return SuperVisor
* @throws TasksException
*/
public function getSuperVisor(): SuperVisor
public function getSuperVisor(string $bootstrapFile): SuperVisor
{
$cfg = $this->cfg->get('SuperVisor');
$class = 'FuzeWorks\Async\Supervisors\\' . $cfg['type'];
$parameters = isset($cfg['parameters']) && is_array($cfg['parameters']) ? $cfg['parameters'] : [];
array_unshift($parameters, $this->getTaskStorage(), $this->getExecutor());
array_unshift($parameters, $this->getTaskStorage(), $this->getExecutor($bootstrapFile));
if (!class_exists($class, true))
throw new TasksException("Could not get SuperVisor. Type of '$class' not found.");
@ -94,12 +96,12 @@ class Tasks implements iLibrary
}
/**
* @return Worker
* @return ShellWorker
* @throws TasksException
*/
public function getWorker(): Worker
public function getWorker(): ShellWorker
{
return new Worker($this->getTaskStorage());
return new ShellWorker($this->getTaskStorage());
}
/**
@ -116,7 +118,7 @@ class Tasks implements iLibrary
if (!class_exists($class, true))
throw new TasksException("Could not get TaskStorage. Type of '$class' not found.");
$object = new $class(...$parameters);
$object = new $class($parameters);
if (!$object instanceof TaskStorage)
throw new TasksException("Could not get TaskStorage. Type '$class' is not instanceof TaskStorage.");
@ -126,10 +128,11 @@ class Tasks implements iLibrary
/**
* Fetch the Executor based on the configured type
*
* @param string $bootstrapFile
* @return Executor
* @throws TasksException
*/
protected function getExecutor(): Executor
protected function getExecutor(string $bootstrapFile): Executor
{
$cfg = $this->cfg->get('Executor');
$class = 'FuzeWorks\Async\Executors\\' . $cfg['type'];
@ -137,7 +140,7 @@ class Tasks implements iLibrary
if (!class_exists($class, true))
throw new TasksException("Could not get Executor. Type of '$class' not found.");
$object = new $class(...$parameters);
$object = new $class($bootstrapFile, $parameters);
if (!$object instanceof Executor)
throw new TasksException("Could not get Executor. Type '$class' is not instanceof Executor.");

View File

@ -0,0 +1,427 @@
<?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
*/
use FuzeWorks\Async\Task;
use FuzeWorks\Async\TasksException;
use FuzeWorks\Async\TaskStorage;
use FuzeWorks\Async\TaskStorage\DummyTaskStorage;
use PHPUnit\Framework\TestCase;
class TaskStorageTest extends TestCase
{
/**
* @var TaskStorage
*/
private $taskStorage;
public function setUp(): void
{
$this->taskStorage = new DummyTaskStorage([]);
}
public function testDummyTaskStorageClass()
{
$this->assertInstanceOf('FuzeWorks\Async\TaskStorage\DummyTaskStorage', $this->taskStorage);
}
/* ---------------------------------- Writing and reading tasks ----------------------- */
/**
* @depends testDummyTaskStorageClass
*/
public function testAddAndReadTasks()
{
// Prepare a dummy task
$dummyTask = new Task('testAddTask', 'none');
// Nothing is written yet so it should be empty
$this->assertEmpty($this->taskStorage->readTasks());
// Write task to storage and test properties of readTasks
$this->assertTrue($this->taskStorage->addTask($dummyTask));
$output = $this->taskStorage->readTasks();
$this->assertContains($dummyTask, $output);
// Test if the properties match
$this->assertEquals('testAddTask', $output[0]->getId());
$this->assertEquals('none', $output[0]->getHandlerClass());
}
/**
* @depends testAddAndReadTasks
*/
public function testAddExistingTask()
{
// Prepare a dummy task
$dummyTask = new Task('testAddExistingTask', 'none');
// First check that the task storage starts empty
$this->assertEmpty($this->taskStorage->readTasks());
// Then add the first task
$this->assertTrue($this->taskStorage->addTask($dummyTask));
// But then add another task, which should raise an exception
$this->expectException(TasksException::class);
$this->taskStorage->addTask($dummyTask);
}
/**
* @depends testAddAndReadTasks
*/
public function testGetTaskById()
{
// Prepare a dummy task
$dummyTask1 = new Task('testGetTaskById1', 'none');
$dummyTask2 = new Task('testGetTaskById2', 'none');
// First we add both tasks
$this->assertEmpty($this->taskStorage->readTasks());
$this->assertTrue($this->taskStorage->addTask($dummyTask1));
$this->assertTrue($this->taskStorage->addTask($dummyTask2));
// Afterwards, we attempt to get the separate tasks
$retrievedTask1 = $this->taskStorage->getTaskById('testGetTaskById1');
$retrievedTask2 = $this->taskStorage->getTaskById('testGetTaskById2');
$this->assertInstanceOf('FuzeWorks\Async\Task', $retrievedTask1);
$this->assertInstanceOf('FuzeWorks\Async\Task', $retrievedTask2);
// Assert they have the values we seek
$this->assertEquals('testGetTaskById1', $retrievedTask1->getId());
$this->assertEquals('testGetTaskById2', $retrievedTask2->getId());
// Test they are not the same
$this->assertNotSame($retrievedTask1, $retrievedTask2);
// And test they are the initial dummy tasks
$this->assertSame($dummyTask1, $retrievedTask1);
$this->assertSame($dummyTask2, $retrievedTask2);
}
/**
* @depends testGetTaskById
*/
public function testGetTaskByIdNotFound()
{
// Prepare a dummy task
$dummyTask = new Task('testGetTaskByIdNotFound', 'none');
// First we add the task
$this->assertEmpty($this->taskStorage->readTasks());
$this->assertTrue($this->taskStorage->addTask($dummyTask));
// Afterwards we check if we can get this task
$this->assertInstanceOf('FuzeWorks\Async\Task', $this->taskStorage->getTaskById('testGetTaskByIdNotFound'));
// And afterwards we check if an exception is raised if none exist
$this->expectException(TasksException::class);
$this->taskStorage->getTaskById('DoesNotExist');
}
/**
* @depends testGetTaskById
*/
public function testModifyTask()
{
// Prepare a dummy task
$dummyTask = new Task('testModifyTask', 'none');
$dummyTask->setStatus(Task::RUNNING);
// First we add the task
$this->assertEmpty($this->taskStorage->readTasks());
$this->assertTrue($this->taskStorage->addTask($dummyTask));
// Afterwards we check if this task has the known details
$this->assertEquals(Task::RUNNING, $this->taskStorage->getTaskById('testModifyTask')->getStatus());
// Then we change the task
$dummyTask->setStatus(Task::FAILED);
$this->assertTrue($this->taskStorage->modifyTask($dummyTask));
// And check if the details have been changed
$this->assertEquals(Task::FAILED, $this->taskStorage->getTaskById('testModifyTask')->getStatus());
}
/**
* @depends testModifyTask
*/
public function testModifyTaskNotFound()
{
// Prepare a dummy task
$dummyTask = new Task('testModifyTaskNotFound', 'none');
// Attempt to change this task, which does not exist.
$this->expectException(TasksException::class);
$this->taskStorage->modifyTask($dummyTask);
}
/**
* @depends testGetTaskById
*/
public function testDeleteTask()
{
// Prepare a dummy task
$dummyTask = new Task('testDeleteTask', 'none');
// Add the task to the storage
$this->assertEmpty($this->taskStorage->readTasks());
$this->assertTrue($this->taskStorage->addTask($dummyTask));
// Test that it exists
$this->assertSame($dummyTask, $this->taskStorage->getTaskById('testDeleteTask'));
// Then remove the task
$this->assertTrue($this->taskStorage->deleteTask($dummyTask));
// And test that it can't be found
$this->expectException(TasksException::class);
$this->taskStorage->getTaskById('testDeleteTask');
}
/**
* @depends testDeleteTask
*/
public function testDeleteTaskNotFound()
{
// Prepare a dummy task
$dummyTask = new Task('testDeleteTaskNotFound', 'none');
// Attempt to delete this task, which does not exist.
$this->expectException(TasksException::class);
$this->taskStorage->deleteTask($dummyTask);
}
/* ---------------------------------- Writing and reading task output ----------------- */
/**
* @depends testDummyTaskStorageClass
*/
public function testWriteAndReadTaskOutput()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteTaskOutput', 'none');
// First write the task output
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output', 'errors', 0, 0));
// Then try to read the output
$output = $this->taskStorage->readTaskOutput($dummyTask, 0);
$this->assertEquals('output', $output['output']);
$this->assertEquals('errors', $output['errors']);
$this->assertEquals(0, $output['statusCode']);
}
/**
* @depends testWriteAndReadTaskOutput
*/
public function testWriteAndReadTaskOutputAttempts()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskOutputAttempts', 'none');
// Write the different outputs. Done in a weird order to make sure the default is inserted not first or last
// to make sure the default is not selected by accident by the TaskStorage
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output2', 'errors2', 102, 2));
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output0', 'errors0', 100, 0));
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output1', 'errors1', 101, 1));
// Attempt to load the first output
$output0 = $this->taskStorage->readTaskOutput($dummyTask, 0);
$this->assertEquals('output0', $output0['output']);
$this->assertEquals('errors0', $output0['errors']);
$this->assertEquals(100, $output0['statusCode']);
// Attempt to load the second output
$output1 = $this->taskStorage->readTaskOutput($dummyTask, 1);
$this->assertEquals('output1', $output1['output']);
$this->assertEquals('errors1', $output1['errors']);
$this->assertEquals(101, $output1['statusCode']);
// Attempt to load the third output
$output2 = $this->taskStorage->readTaskOutput($dummyTask, 2);
$this->assertEquals('output2', $output2['output']);
$this->assertEquals('errors2', $output2['errors']);
$this->assertEquals(102, $output2['statusCode']);
// Attempt to load the default output
$output = $this->taskStorage->readTaskOutput($dummyTask);
$this->assertEquals('output0', $output['output']);
$this->assertEquals('errors0', $output['errors']);
$this->assertEquals(100, $output['statusCode']);
}
/**
* @depends testWriteAndReadTaskOutput
*/
public function testWriteAndReadTaskOutputAlreadyExists()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskOutputAlreadyExists', 'none');
// Write a first time
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output', 'errors', 100, 0));
// And write it a second time
$this->expectException(TasksException::class);
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output', 'errors', 100, 0));
}
/**
* @depends testWriteAndReadTaskOutput
*/
public function testWriteAndReadTaskOutputNotExist()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskOutputNotExist', 'none');
$this->assertNull($this->taskStorage->readTaskOutput($dummyTask));
}
/* ---------------------------------- Writing and reading task post output ------------ */
/**
* @depends testDummyTaskStorageClass
*/
public function testWriteAndReadTaskPostOutput()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskPostOutput', 'none');
// First write the task output
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'postOutput', 'errors', 0, 0));
// Then try to read the output
$output = $this->taskStorage->readPostOutput($dummyTask, 0);
$this->assertEquals('postOutput', $output['output']);
$this->assertEquals('errors', $output['errors']);
$this->assertEquals(0, $output['statusCode']);
}
/**
* @depends testWriteAndReadTaskPostOutput
*/
public function testWriteAndReadTaskPostOutputAttempts()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskPostOutputAttempts', 'none');
// Write the different outputs. Done in a weird order to make sure the default is inserted not first or last
// to make sure the default is not selected by accident by the TaskStorage
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'output2', 'errors2', 102, 2));
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'output0', 'errors0', 100, 0));
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'output1', 'errors1', 101, 1));
// Attempt to load the first output
$output0 = $this->taskStorage->readPostOutput($dummyTask, 0);
$this->assertEquals('output0', $output0['output']);
$this->assertEquals('errors0', $output0['errors']);
$this->assertEquals(100, $output0['statusCode']);
// Attempt to load the second output
$output1 = $this->taskStorage->readPostOutput($dummyTask, 1);
$this->assertEquals('output1', $output1['output']);
$this->assertEquals('errors1', $output1['errors']);
$this->assertEquals(101, $output1['statusCode']);
// Attempt to load the third output
$output2 = $this->taskStorage->readPostOutput($dummyTask, 2);
$this->assertEquals('output2', $output2['output']);
$this->assertEquals('errors2', $output2['errors']);
$this->assertEquals(102, $output2['statusCode']);
// Attempt to load the default output
$output = $this->taskStorage->readPostOutput($dummyTask);
$this->assertEquals('output0', $output['output']);
$this->assertEquals('errors0', $output['errors']);
$this->assertEquals(100, $output['statusCode']);
}
/**
* @depends testWriteAndReadTaskPostOutput
*/
public function testWriteAndReadTaskPostOutputAlreadyExists()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskPostOutputAlreadyExists', 'none');
// Write a first time
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'output', 'errors', 100, 0));
// And write it a second time
$this->expectException(TasksException::class);
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'output', 'errors', 100, 0));
}
/**
* @depends testWriteAndReadTaskPostOutput
*/
public function testWriteAndReadTaskPostOutputNotExist()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskPostOutputNotExist', 'none');
$this->assertNull($this->taskStorage->readPostOutput($dummyTask));
}
/* ---------------------------------- Data persistence and resets --------- ------------ */
/**
* @depends testAddAndReadTasks
* @depends testWriteAndReadTaskOutput
* @depends testWriteAndReadTaskPostOutput
*/
public function testReset()
{
// Prepare a dummy task
$dummyTask = new Task('testReset', 'none');
// Add the task and some output
$this->assertTrue($this->taskStorage->addTask($dummyTask));
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output', 'errors', 100));
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'postOutput', 'errors', 100));
// Then reset the data
$this->assertTrue($this->taskStorage->reset());
// And test if the data is actually gone
$this->assertNull($this->taskStorage->readTaskOutput($dummyTask));
$this->assertNull($this->taskStorage->readPostOutput($dummyTask));
$this->expectException(TasksException::class);
$this->taskStorage->getTaskById('testReset');
}
}

244
test/base/TaskTest.php Normal file
View File

@ -0,0 +1,244 @@
<?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
*/
use FuzeWorks\Async\Constraint;
use FuzeWorks\Async\Task;
use FuzeWorks\Async\TasksException;
use PHPUnit\Framework\TestCase;
class TaskTest extends TestCase
{
public function testClass()
{
// Create dummy task
$dummyTask = new Task('testClass', 'none');
// And check the class. A pretty useless but standard test
$this->assertInstanceOf('FuzeWorks\Async\Task', $dummyTask);
}
/* ---------------------------------- Basic variables tests --------------------------- */
/**
* @depends testClass
*/
public function testBaseVariables()
{
// Create dummy task
$dummyTask = new Task('testBaseVariables', 'someThing', true);
// test the values
$this->assertEquals('testBaseVariables', $dummyTask->getId());
$this->assertEquals('someThing', $dummyTask->getHandlerClass());
$this->assertTrue($dummyTask->getUsePostHandler());
}
/**
* @depends testBaseVariables
*/
public function testArguments()
{
// Create task without arguments
$dummyTask1 = new Task('testArguments1', 'none', true);
$this->assertEmpty($dummyTask1->getArguments());
// Now create a task with some arguments
$dummyTask2 = new Task('testArguments2', 'none', true, 'some', 'arguments');
$this->assertEquals(['some', 'arguments'], $dummyTask2->getArguments());
}
/**
* @depends testBaseVariables
*/
public function testPostHandler()
{
// Create dummy tasks
$dummyTask1 = new Task('testPostHandler1', 'someThing', true);
$dummyTask2 = new Task('testPostHandler2', 'someThing', false);
$this->assertTrue($dummyTask1->getUsePostHandler());
$this->assertFalse($dummyTask2->getUsePostHandler());
}
/**
* @depends testBaseVariables
*/
public function testConstraints()
{
// First create a mock constraint
$stub = $this->createMock(Constraint::class);
// Then add it to the task
$dummyTask = new Task('testConstraints', 'someThing', false);
$dummyTask->addConstraint($stub);
// Assert it exists
$this->assertEquals([$stub], $dummyTask->getConstraints());
}
/**
* @depends testBaseVariables
*/
public function testStatusCodes()
{
// Create dummy task
$dummyTask = new Task('testStatusCodes', 'someThing', true);
for ($i = 1; $i <= 9; $i++) {
$dummyTask->setStatus($i);
$this->assertEquals($i, $dummyTask->getStatus());
}
}
/**
* @depends testBaseVariables
*/
public function testDelayTime()
{
// Create dummy task
$dummyTask = new Task('testDelayTime', 'someThing', true);
$this->assertEquals(0, $dummyTask->getDelayTime());
$dummyTask->setDelayTime(1000);
$this->assertEquals(1000, $dummyTask->getDelayTime());
}
/**
* @depends testBaseVariables
*/
public function testAttributes()
{
// Create dummy task
$dummyTask = new Task('testAttributes', 'someThing', true);
// First test a non-existing attribute
$this->assertNull($dummyTask->attribute('testKey'));
// Now add it and test if it is there
$dummyTask->addAttribute('testKey', 'SomeContent');
$this->assertEquals('SomeContent', $dummyTask->attribute('testKey'));
}
/**
* @depends testBaseVariables
*/
public function testOutputsAndErrors()
{
// Create dummy task
$dummyTask = new Task('testOutputsAndErrors', 'someThing', true);
// Check if non are filled
$this->assertNull($dummyTask->getOutput());
$this->assertNull($dummyTask->getPostOutput());
$this->assertNull($dummyTask->getErrors());
$this->assertNull($dummyTask->getPostErrors());
// Then write some data to the task
$dummyTask->setOutput('SomeOutput', 'SomeErrors');
$dummyTask->setPostOutput('SomePostOutput', 'SomePostErrors');
// And check again
$this->assertEquals('SomeOutput', $dummyTask->getOutput());
$this->assertEquals('SomePostOutput', $dummyTask->getPostOutput());
$this->assertEquals('SomeErrors', $dummyTask->getErrors());
$this->assertEquals('SomePostErrors', $dummyTask->getPostErrors());
}
/**
* @depends testBaseVariables
*/
public function testRetrySettings()
{
// Create dummy task
$dummyTask = new Task('testRetrySettings', 'someThing', true);
// Test starting position
$this->assertEquals([
'retryOnFail' => false,
'maxRetries' => 2,
'retryPFailures' => true,
'retryRFailures' => true,
'retryPostFailures' => true
], $dummyTask->getRetrySettings());
// Then change the settings
$dummyTask->setRetrySettings(true, 30, false, false, false);
// And test the new positions
$this->assertEquals([
'retryOnFail' => true,
'maxRetries' => 30,
'retryPFailures' => false,
'retryRFailures' => false,
'retryPostFailures' => false
], $dummyTask->getRetrySettings());
}
/**
* @depends testBaseVariables
*/
public function testRetries()
{
// Create dummy task
$dummyTask = new Task('testRetries', 'someThing', true);
// First test the starting position
$this->assertEquals(0, $dummyTask->getRetries());
// Then add one and test
$dummyTask->addRetry();
$this->assertEquals(1, $dummyTask->getRetries());
// Then reset it and test
$dummyTask->resetRetries();
$this->assertEquals(0, $dummyTask->getRetries());
}
public function testGetStatusType()
{
$this->assertEquals('Task::PENDING', Task::getStatusType(Task::PENDING));
$this->assertEquals('Task::RUNNING', Task::getStatusType(Task::RUNNING));
$this->assertEquals('Task::FAILED', Task::getStatusType(Task::FAILED));
$this->assertEquals('Task::PFAILED', Task::getStatusType(Task::PFAILED));
$this->assertEquals('Task::SUCCESS', Task::getStatusType(Task::SUCCESS));
$this->assertEquals('Task::POST', Task::getStatusType(Task::POST));
$this->assertEquals('Task::COMPLETED', Task::getStatusType(Task::COMPLETED));
$this->assertEquals('Task::DELAYED', Task::getStatusType(Task::DELAYED));
$this->assertEquals('Task::CANCELLED', Task::getStatusType(Task::CANCELLED));
$this->assertFalse(Task::getStatusType(10));
}
}

View File

@ -34,11 +34,10 @@
* @version Version 1.0.0
*/
use FuzeWorks\Async\SuperVisor;
use FuzeWorks\Async\Tasks;
use FuzeWorks\Logger;
require_once(dirname(__DIR__) . '/vendor/autoload.php');
require_once('vendor/autoload.php');
use FuzeWorks\Logger;
use FuzeWorks\Priority;
// Open configurator
$configurator = new FuzeWorks\Configurator();
@ -46,25 +45,17 @@ $configurator = new FuzeWorks\Configurator();
// Set up basic settings
$configurator->setTimeZone('Europe/Amsterdam');
$configurator->setTempDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'temp');
$configurator->setLogDirectory(dirname(__FILE__). DIRECTORY_SEPARATOR . 'log');
$configurator->setLogDirectory(dirname(__FILE__). DIRECTORY_SEPARATOR . 'temp');
$configurator->addComponent(new \FuzeWorks\MVCRComponent());
// Add Async library
$configurator->deferComponentClassMethod('libraries', 'addLibraryClass', null, 'async', '\FuzeWorks\Async\Tasks');
// Debug
$configurator->enableDebugMode()->setDebugAddress('ALL');
$configurator->addDirectory(dirname(__FILE__), 'controllers', Priority::HIGH);
// Create container
$container = $configurator->createContainer();
// Add lib
Logger::enableScreenLog();
// RUN THE APP
/** @var Tasks $lib */
$lib = $container->libraries->get('async');
$supervisor = $lib->getSuperVisor();
while ($supervisor->cycle() === SuperVisor::RUNNING) {
usleep(250000);
}
return $container;

29
test/phpunit.xml Normal file
View File

@ -0,0 +1,29 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
bootstrap="bootstrap.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
colors="false">
<testsuites>
<testsuite name="Base Functionality">
<directory>./base/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">../</directory>
<exclude>
<directory suffix=".php">../vendor/</directory>
<directory suffix=".php">../test/</directory>
<directory suffix=".php">../src/Layout/</directory>
<directory suffix=".php">../src/Config/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

0
test/temp/placeholder Normal file
View File

View File

@ -1,68 +0,0 @@
<?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
*/
use FuzeWorks\Async\Tasks;
require_once('vendor/autoload.php');
// Open configurator
$configurator = new FuzeWorks\Configurator();
// Set up basic settings
$configurator->setTimeZone('Europe/Amsterdam');
$configurator->setTempDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'temp');
$configurator->setLogDirectory(dirname(__FILE__). DIRECTORY_SEPARATOR . 'log');
// Add Async library
$configurator->deferComponentClassMethod('libraries', 'addLibraryClass', null, 'async', '\FuzeWorks\Async\Tasks');
// Debug
$configurator->enableDebugMode()->setDebugAddress('ALL');
// Create container
$container = $configurator->createContainer();
// Prepare arguments
$script = array_shift($argv);
$taskID = array_shift($argv);
$taskID = base64_decode($taskID);
$mode = trim(array_shift($argv));
$post = ($mode === 'post' ? true : false);
// RUN THE APP
/** @var Tasks $lib */
$lib = $container->libraries->get('async');
$worker = $lib->getWorker();
$worker->run($taskID, $post);