150 lines
4.8 KiB
PHP
150 lines
4.8 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\Executors;
|
|
use Application\Library\Tasks\Executor;
|
|
use Application\Library\Tasks\Task;
|
|
use Application\Library\Tasks\TasksException;
|
|
|
|
class ShellExecutor implements Executor
|
|
{
|
|
|
|
private $binary;
|
|
private $worker;
|
|
private $stdout = "> /dev/null";
|
|
private $stderr = "2> /dev/null";
|
|
|
|
/**
|
|
* ShellExecutor constructor.
|
|
*
|
|
* @param string $workerFile The worker script that shall run individual tasks
|
|
* @throws TasksException
|
|
*/
|
|
public function __construct(string $workerFile)
|
|
{
|
|
// First determine the PHP binary
|
|
$this->binary = PHP_BINDIR . DS . 'php';
|
|
|
|
if (!file_exists($workerFile))
|
|
throw new TasksException("Could not construct ShellExecutor. Worker script does not exist.");
|
|
|
|
$this->worker = $workerFile;
|
|
}
|
|
|
|
|
|
private function shellExec($format, array $parameters = [])
|
|
{
|
|
$parameters = array_map("escapeshellarg", $parameters);
|
|
array_unshift($parameters, $format);
|
|
$command = call_user_func_array("sprintf", $parameters);
|
|
exec($command, $output);
|
|
return $output;
|
|
}
|
|
|
|
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 $!";
|
|
|
|
// Then execute the command using the base64_encoded string of the taskID
|
|
$output = $this->shellExec($commandString, [base64_encode($task->getId())]);
|
|
|
|
// Add the PID to the task and change its state to 'RUNNING'
|
|
if ($post)
|
|
{
|
|
$task->setStatus(Task::POST);
|
|
$task->addAttribute('post_pid', $output[0]);
|
|
}
|
|
else
|
|
{
|
|
$task->setStatus(Task::RUNNING);
|
|
$task->addAttribute('run_pid', $output[0]);
|
|
}
|
|
|
|
// And finally return the task
|
|
return $task;
|
|
}
|
|
|
|
public function stopTask(Task $task): Task
|
|
{
|
|
// TODO: Implement stopTask() method.
|
|
}
|
|
|
|
public function getTaskRunning(Task $task): bool
|
|
{
|
|
$stats = $this->getTaskStats($task);
|
|
return !is_null($stats);
|
|
}
|
|
|
|
public function getTaskStats(Task $task): ?array
|
|
{
|
|
// First prepare the command used to gather info on processes
|
|
$commandString = "ps -o pid,%%cpu,%%mem,state,start -p %s | sed 1d";
|
|
|
|
// Then fetch the PID
|
|
try {
|
|
$pid = is_null($task->attribute('post_pid')) ? $task->attribute('run_pid') : $task->attribute('post_pid');
|
|
$output = $this->shellExec($commandString, [$pid]);
|
|
|
|
if (count($output) < 1)
|
|
return null;
|
|
|
|
$last = $output[count($output) - 1];
|
|
if (trim($last) === "")
|
|
return null;
|
|
|
|
$parts = preg_split("/\s+/", trim($last));
|
|
$pid = intval($parts[0]);
|
|
$state = strtoupper(trim($parts[3]));
|
|
if ("{$pid}" !== $parts[0] || $state === 'Z')
|
|
return null;
|
|
|
|
return $parts;
|
|
} catch (TasksException $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function getTaskExitCode(Task $task): int
|
|
{
|
|
// TODO: Implement getTaskExitCode() method.
|
|
}
|
|
|
|
public function getRunningTasks(): array
|
|
{
|
|
// TODO: Implement getRunningTasks() method.
|
|
}
|
|
} |