169 lines
5.2 KiB
PHP
169 lines
5.2 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;
|
|
use Application\Library\Tasks\Executor;
|
|
use Application\Library\Tasks\SuperVisor;
|
|
use Application\Library\Tasks\Task;
|
|
use Application\Library\Tasks\TasksException;
|
|
use Application\Library\Tasks\TaskStorage;
|
|
use Application\Library\Tasks\Worker;
|
|
use FuzeWorks\Config;
|
|
use FuzeWorks\ConfigORM\ConfigORM;
|
|
use FuzeWorks\Events;
|
|
use FuzeWorks\Exception\Exception;
|
|
use FuzeWorks\Factory;
|
|
use FuzeWorks\iLibrary;
|
|
use FuzeWorks\Priority;
|
|
|
|
class Tasks implements iLibrary
|
|
{
|
|
|
|
/**
|
|
* @var ConfigORM
|
|
*/
|
|
private $cfg;
|
|
|
|
/**
|
|
* Tasks constructor.
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public function __construct()
|
|
{
|
|
/** @var Config $config */
|
|
$config = Factory::getInstance('config');
|
|
$config->addComponentPath(dirname(__FILE__), Priority::LOW);
|
|
$this->cfg = $config->getConfig('tasks');
|
|
}
|
|
|
|
/**
|
|
* @param Task $task
|
|
* @return bool
|
|
* @throws TasksException
|
|
*/
|
|
public function queueTask(Task $task): bool
|
|
{
|
|
$taskStorage = $this->getTaskStorage();
|
|
return $taskStorage->addTask($task);
|
|
}
|
|
|
|
/**
|
|
* @throws TasksException
|
|
*/
|
|
public function getSuperVisor(): SuperVisor
|
|
{
|
|
$cfg = $this->cfg->get('SuperVisor');
|
|
$class = 'Application\Library\Tasks\Supervisors\\' . $cfg['type'];
|
|
$parameters = isset($cfg['parameters']) && is_array($cfg['parameters']) ? $cfg['parameters'] : [];
|
|
array_unshift($parameters, $this->getTaskStorage(), $this->getExecutor());
|
|
if (!class_exists($class, true))
|
|
throw new TasksException("Could not get SuperVisor. Type of '$class' not found.");
|
|
|
|
$object = new $class(...$parameters);
|
|
if (!$object instanceof SuperVisor)
|
|
throw new TasksException("Could not get SuperVisor. Type of '$class' is not instanceof TaskStorage.");
|
|
|
|
return $object;
|
|
}
|
|
|
|
/**
|
|
* @return Worker
|
|
* @throws TasksException
|
|
*/
|
|
public function getWorker(): Worker
|
|
{
|
|
return new Worker($this->getTaskStorage());
|
|
}
|
|
|
|
/**
|
|
* Fetch the TaskStorage based on the configured type
|
|
*
|
|
* @return TaskStorage
|
|
* @throws TasksException
|
|
*/
|
|
public function getTaskStorage(): TaskStorage
|
|
{
|
|
$cfg = $this->cfg->get('TaskStorage');
|
|
$class = 'Application\Library\Tasks\TaskStorage\\' . $cfg['type'];
|
|
$parameters = isset($cfg['parameters']) && is_array($cfg['parameters']) ? $cfg['parameters'] : [];
|
|
if (!class_exists($class, true))
|
|
throw new TasksException("Could not get TaskStorage. Type of '$class' not found.");
|
|
|
|
$object = new $class(...$parameters);
|
|
if (!$object instanceof TaskStorage)
|
|
throw new TasksException("Could not get TaskStorage. Type '$class' is not instanceof TaskStorage.");
|
|
|
|
return $object;
|
|
}
|
|
|
|
/**
|
|
* Fetch the Executor based on the configured type
|
|
*
|
|
* @return Executor
|
|
* @throws TasksException
|
|
*/
|
|
protected function getExecutor(): Executor
|
|
{
|
|
$cfg = $this->cfg->get('Executor');
|
|
$class = 'Application\Library\Tasks\Executors\\' . $cfg['type'];
|
|
$parameters = isset($cfg['parameters']) && is_array($cfg['parameters']) ? $cfg['parameters'] : [];
|
|
if (!class_exists($class, true))
|
|
throw new TasksException("Could not get Executor. Type of '$class' not found.");
|
|
|
|
$object = new $class(...$parameters);
|
|
if (!$object instanceof Executor)
|
|
throw new TasksException("Could not get Executor. Type '$class' is not instanceof Executor.");
|
|
|
|
return $object;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getClassesPrefix(): ?string
|
|
{
|
|
return '\Application\Library\Tasks';
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getSourceDirectory(): ?string
|
|
{
|
|
return 'src';
|
|
}
|
|
} |