Async/src/FuzeWorks/Async/Tasks.php

233 lines
6.8 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;
use FuzeWorks\Config;
use FuzeWorks\ConfigORM\ConfigORM;
use FuzeWorks\Exception\Exception;
use FuzeWorks\Factory;
use FuzeWorks\iLibrary;
use FuzeWorks\Priority;
class Tasks implements iLibrary
{
/**
* @var ConfigORM
*/
private $cfg;
/**
* @var TaskStorage
*/
private $taskStorage;
/**
* @var Executor
*/
private $executor;
/**
* @var SuperVisor
*/
private $supervisor;
/**
* @var ShellWorker
*/
private $shellWorker;
/**
* Tasks constructor.
*
* @throws Exception
*/
public function __construct()
{
/** @var Config $config */
$config = Factory::getInstance('config');
$this->cfg = $config->getConfig('tasks');
}
/**
* @param Task $task
* @return bool
* @throws TasksException
*/
public function addTask(Task $task): bool
{
$taskStorage = $this->getTaskStorage();
return $taskStorage->addTask($task);
}
/**
* @return SuperVisor
* @throws TasksException
*/
public function getSuperVisor(): SuperVisor
{
if (isset($this->supervisor))
return $this->supervisor;
// First get the configuration for SuperVisors
$cfg = $this->cfg->get('SuperVisor');
// Select the SuperVisor type
$type = $cfg['type'];
// Load the class of the currently selected type
$class = 'FuzeWorks\Async\Supervisors\\' . $type;
// Fetch the parameters for the selected SuperVisor
$parameters = isset($cfg[$type]['parameters']) && is_array($cfg[$type]['parameters']) ? $cfg[$type]['parameters'] : [];
// Then add the TaskStorage and Executor to the parameters
array_unshift($parameters, $this->getTaskStorage(), $this->getExecutor());
// If the type does not exist, throw an exception
if (!class_exists($class, true))
throw new TasksException("Could not get SuperVisor. Type of '$class' not found.");
// And load the SuperVisor and test if everything is in order
$object = new $class(...$parameters);
if (!$object instanceof SuperVisor)
throw new TasksException("Could not get SuperVisor. Type of '$class' is not instanceof TaskStorage.");
$this->supervisor = $object;
return $object;
}
/**
* @return ShellWorker
* @throws TasksException
*/
public function getWorker(): ShellWorker
{
if (isset($this->shellWorker))
return $this->shellWorker;
$this->shellWorker = new ShellWorker($this->getTaskStorage());
return $this->shellWorker;
}
/**
* Fetch the TaskStorage based on the configured type
*
* @return TaskStorage
* @throws TasksException
*/
public function getTaskStorage(): TaskStorage
{
if (isset($this->taskStorage))
return $this->taskStorage;
// First get the configuration for TaskStorage
$cfg = $this->cfg->get('TaskStorage');
// Select the TaskStorage type
$type = $cfg['type'];
// Load the class of the currently selected type
$class = 'FuzeWorks\Async\TaskStorage\\' . $type;
// Fetch the parameters for the selected type
$parameters = isset($cfg[$type]['parameters']) && is_array($cfg[$type]['parameters']) ? $cfg[$type]['parameters'] : [];
// If the type does not exist, throw an exception
if (!class_exists($class, true))
throw new TasksException("Could not get TaskStorage. Type of '$class' not found.");
// And load the TaskStorage and test if everything is in order
$object = new $class($parameters);
if (!$object instanceof TaskStorage)
throw new TasksException("Could not get TaskStorage. Type '$class' is not instanceof TaskStorage.");
$this->taskStorage = $object;
return $this->taskStorage;
}
/**
* Fetch the Executor based on the configured type
*
* @return Executor
* @throws TasksException
*/
protected function getExecutor(): Executor
{
if (isset($this->executor))
return $this->executor;
// First get the configuration for Executor
$cfg = $this->cfg->get('Executor');
// Select the Executor type
$type = $cfg['type'];
// Load the class of the currently selected type
$class = 'FuzeWorks\Async\Executors\\' . $type;
// Fetch the parameters for the selected type
$parameters = isset($cfg[$type]['parameters']) && is_array($cfg[$type]['parameters']) ? $cfg[$type]['parameters'] : [];
// If the type does not exist, throw an exception
if (!class_exists($class, true))
throw new TasksException("Could not get Executor. Type of '$class' not found.");
// And load the Executor and test if everything is in order
$object = new $class($parameters);
if (!$object instanceof Executor)
throw new TasksException("Could not get Executor. Type '$class' is not instanceof Executor.");
$this->executor = $object;
return $this->executor;
}
/**
* @inheritDoc
*/
public function getClassesPrefix(): ?string
{
return null;
}
/**
* @inheritDoc
*/
public function getSourceDirectory(): ?string
{
return null;
}
}