Release of RC1 #7
|
@ -36,7 +36,6 @@
|
|||
|
||||
namespace FuzeWorks\Async\Executors;
|
||||
use FuzeWorks\Async\Executor;
|
||||
use FuzeWorks\Async\Process;
|
||||
use FuzeWorks\Async\Task;
|
||||
use FuzeWorks\Async\TasksException;
|
||||
|
||||
|
@ -52,7 +51,6 @@ class ShellExecutor implements Executor
|
|||
/**
|
||||
* ShellExecutor constructor.
|
||||
*
|
||||
* @param string $bootstrapFile
|
||||
* @param array $parameters
|
||||
* @throws TasksException
|
||||
*/
|
||||
|
@ -70,15 +68,6 @@ class ShellExecutor implements Executor
|
|||
throw new TasksException("Could not construct ShellExecutor. No bootstrap file found.");
|
||||
}
|
||||
|
||||
protected 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
|
||||
|
@ -152,4 +141,13 @@ class ShellExecutor implements Executor
|
|||
// Finally, return the Task information
|
||||
return ['pid' => (int) $parts[0], 'cpu' => (float) $parts[1], 'mem' => (float) $parts[2], 'state' => $parts[3], 'start' => $parts[4]];
|
||||
}
|
||||
|
||||
protected 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;
|
||||
}
|
||||
}
|
|
@ -142,6 +142,9 @@ class ShellExecutorTest extends TestCase
|
|||
// Then we fire the task
|
||||
$task = $this->executor->startTask($dummyTask);
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// Assert that the output is the same
|
||||
$this->assertSame($dummyTask, $task);
|
||||
|
||||
|
@ -166,6 +169,9 @@ class ShellExecutorTest extends TestCase
|
|||
// Then we start the task
|
||||
$dummyTask = $this->executor->startTask($dummyTask);
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// And we fetch some task statistics
|
||||
$stats = $this->executor->getTaskStats($dummyTask);
|
||||
|
||||
|
@ -206,11 +212,19 @@ class ShellExecutorTest extends TestCase
|
|||
|
||||
// First we start the task and confirm its running
|
||||
$dummyTask = $this->executor->startTask($dummyTask);
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// Check if the task is running
|
||||
$this->assertTrue($this->executor->getTaskRunning($dummyTask));
|
||||
|
||||
// But then we try and stop it
|
||||
$output = $this->executor->stopTask($dummyTask);
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// We check that the output actually is the task
|
||||
$this->assertSame($dummyTask, $output);
|
||||
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
<?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\ShellWorker;
|
||||
use FuzeWorks\Async\Task;
|
||||
use FuzeWorks\Async\Tasks;
|
||||
use FuzeWorks\Async\TaskStorage;
|
||||
use FuzeWorks\Events;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ShellWorkerTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ShellWorker
|
||||
*/
|
||||
protected $shellWorker;
|
||||
|
||||
/**
|
||||
* @var TaskStorage
|
||||
*/
|
||||
protected $taskStorage;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
// Load the TaskStorage so temporary tasks can be stored
|
||||
$tasks = new Tasks();
|
||||
$this->taskStorage = $tasks->getTaskStorage();
|
||||
$this->taskStorage->reset();
|
||||
|
||||
// Clear events
|
||||
Events::$listeners = [];
|
||||
$this->shellWorker = $tasks->getWorker();
|
||||
}
|
||||
|
||||
public function testClass()
|
||||
{
|
||||
$this->assertInstanceOf(ShellWorker::class, $this->shellWorker);
|
||||
}
|
||||
|
||||
/* ---------------------------------- Writing and reading tasks ----------------------- */
|
||||
|
||||
// @todo Add lots of tests and amend ShellWorker to return results
|
||||
|
||||
}
|
|
@ -76,7 +76,9 @@ return array(
|
|||
|
||||
// For ShellExecutor, first parameter is the file location of the worker script
|
||||
'parameters' => [
|
||||
'workerFile' => dirname(__FILE__) . DS . 'bin' . DS . 'worker'
|
||||
'workerFile' => Core::getEnv('EXECUTOR_SHELL_WORKER',
|
||||
dirname(__FILE__) . DS . 'bin' . DS . 'worker'),
|
||||
'bootstrapFile' => Core::getEnv('EXECUTOR_SHELL_BOOTSTRAP', 'unknown')
|
||||
]
|
||||
]
|
||||
);
|
|
@ -71,6 +71,10 @@ class ArgumentedHandler implements Handler
|
|||
*/
|
||||
public function postHandler(Task $task)
|
||||
{
|
||||
$arguments = $task->getArguments();
|
||||
$this->sleeptime = $arguments[0];
|
||||
$this->output = $arguments[1];
|
||||
|
||||
sleep($this->sleeptime);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ class ParallelSuperVisorTest extends TestCase
|
|||
|
||||
// And load the ShellExecutor using the execution settings
|
||||
$this->executor = new ShellExecutor([
|
||||
'bootstrapFile' => dirname(__DIR__) . DIRECTORY_SEPARATOR,
|
||||
'bootstrapFile' => dirname(__DIR__, 1) . DIRECTORY_SEPARATOR . 'bootstrap.php',
|
||||
'workerFile' => dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'worker'
|
||||
]);
|
||||
|
||||
|
@ -111,6 +111,9 @@ class ParallelSuperVisorTest extends TestCase
|
|||
// Then cycle the SuperVisor
|
||||
$this->superVisor->cycle();
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// Then re-fetch the Task
|
||||
$dummyTask = $this->taskStorage->getTaskById($dummyTask->getId());
|
||||
|
||||
|
@ -140,6 +143,9 @@ class ParallelSuperVisorTest extends TestCase
|
|||
// Then cycle the SuperVisor
|
||||
$this->superVisor->cycle();
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// Then re-fetch the Task
|
||||
$dummyTask = $this->taskStorage->getTaskById($dummyTask->getId());
|
||||
|
||||
|
@ -170,6 +176,9 @@ class ParallelSuperVisorTest extends TestCase
|
|||
// Then cycle the SuperVisor
|
||||
$this->superVisor->cycle();
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// Then re-fetch the Task
|
||||
$dummyTask = $this->taskStorage->getTaskById($dummyTask->getId());
|
||||
|
||||
|
@ -200,6 +209,9 @@ class ParallelSuperVisorTest extends TestCase
|
|||
// Then cycle the SuperVisor
|
||||
$this->superVisor->cycle();
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// Then re-fetch the Task
|
||||
$dummyTask = $this->taskStorage->getTaskById($dummyTask->getId());
|
||||
|
||||
|
@ -230,6 +242,9 @@ class ParallelSuperVisorTest extends TestCase
|
|||
// Then cycle the SuperVisor
|
||||
$this->superVisor->cycle();
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// Then re-fetch the Task
|
||||
$dummyTask = $this->taskStorage->getTaskById($dummyTask->getId());
|
||||
|
||||
|
@ -260,6 +275,9 @@ class ParallelSuperVisorTest extends TestCase
|
|||
// Then cycle the SuperVisor
|
||||
$this->superVisor->cycle();
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// Then re-fetch the Task
|
||||
$dummyTask = $this->taskStorage->getTaskById($dummyTask->getId());
|
||||
|
||||
|
@ -290,6 +308,9 @@ class ParallelSuperVisorTest extends TestCase
|
|||
// Then cycle the SuperVisor
|
||||
$this->superVisor->cycle();
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// Then re-fetch the Task
|
||||
$dummyTask = $this->taskStorage->getTaskById($dummyTask->getId());
|
||||
|
||||
|
@ -331,6 +352,9 @@ class ParallelSuperVisorTest extends TestCase
|
|||
// Then cycle the SuperVisor
|
||||
$this->superVisor->cycle();
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// Reload all tasks from TaskStorage
|
||||
$dummyTaskFailedYes = $this->taskStorage->getTaskById($dummyTaskFailedYes->getId());
|
||||
$dummyTaskPFailedYes = $this->taskStorage->getTaskById($dummyTaskPFailedYes->getId());
|
||||
|
@ -375,6 +399,9 @@ class ParallelSuperVisorTest extends TestCase
|
|||
// Cycle the SuperVisor
|
||||
$this->superVisor->cycle();
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// And check if the Task has been cancelled
|
||||
$dummyTask = $this->taskStorage->getTaskById($dummyTask->getId());
|
||||
$dummyTask2 = $this->taskStorage->getTaskById($dummyTask2->getId());
|
||||
|
@ -400,6 +427,9 @@ class ParallelSuperVisorTest extends TestCase
|
|||
// Cycle the SuperVisor
|
||||
$this->superVisor->cycle();
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// And check if the Task has been moved to Post
|
||||
$dummyTask = $this->taskStorage->getTaskById($dummyTask->getId());
|
||||
$this->assertEquals(Task::POST, $dummyTask->getStatus());
|
||||
|
@ -428,6 +458,9 @@ class ParallelSuperVisorTest extends TestCase
|
|||
// Cycle the SuperVisor
|
||||
$this->superVisor->cycle();
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// And check if the Tasks have been completed or moved to post
|
||||
$dummyTaskPostNo = $this->taskStorage->getTaskById($dummyTaskPostNo->getId());
|
||||
$dummyTaskPostYes = $this->taskStorage->getTaskById($dummyTaskPostYes->getId());
|
||||
|
@ -456,6 +489,9 @@ class ParallelSuperVisorTest extends TestCase
|
|||
// Cycle the SuperVisor
|
||||
$this->superVisor->cycle();
|
||||
|
||||
// Pause 1/10th of a second
|
||||
usleep(100000);
|
||||
|
||||
// And check if the Tasks have been completed or failed
|
||||
$dummyTaskFinished = $this->taskStorage->getTaskById($dummyTaskFinished->getId());
|
||||
$dummyTaskMissing = $this->taskStorage->getTaskById($dummyTaskMissing->getId());
|
||||
|
|
Loading…
Reference in New Issue