Skip to content

Commit

Permalink
BC - name changes, added showOutput() so isRunning() can display …
Browse files Browse the repository at this point in the history
…parent output if set, update required versions
TheTechsTech committed Feb 14, 2020
1 parent 66389d2 commit 9167ab3
Showing 6 changed files with 55 additions and 25 deletions.
21 changes: 17 additions & 4 deletions Processor/Launcher.php
Original file line number Diff line number Diff line change
@@ -13,12 +13,12 @@
use Async\Processor\Process;
use Async\Processor\ProcessorError;
use Async\Processor\SerializableException;
use Async\Processor\ProcessInterface;
use Async\Processor\LauncherInterface;

/**
* Launcher runs a command/script/application/callable in an independent process.
*/
class Launcher implements ProcessInterface
class Launcher implements LauncherInterface
{
protected $timeout = null;
protected $process;
@@ -31,6 +31,7 @@ class Launcher implements ProcessInterface
protected $realTimeOutput;

protected $startTime;
protected $showOutput = false;

protected $successCallbacks = [];
protected $errorCallbacks = [];
@@ -54,7 +55,7 @@ public function start(): self
$this->startTime = \microtime(true);

$this->process->start(function ($type, $buffer) {
$this->realTimeOutput .= $buffer;
$this->realTimeOutput = $buffer;
});

$this->pid = $this->process->getPid();
@@ -144,7 +145,19 @@ public function isTimedOut(): bool

public function isRunning(): bool
{
return $this->process->isRunning();
$isRunning = $this->process->isRunning();
if ($isRunning && $this->showOutput) {
echo $this->getRealOutput();
}

return $isRunning;
}

public function showOutput(): self
{
$this->showOutput = true;

return $this;
}

public function isSuccessful(): bool
27 changes: 17 additions & 10 deletions Processor/ProcessInterface.php → Processor/LauncherInterface.php
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

namespace Async\Processor;

interface ProcessInterface
interface LauncherInterface
{
/**
* Gets PHP's process ID
@@ -16,14 +16,14 @@ public function getId(): int;
/**
* Start the process
*
* @return ProcessInterface
* @return LauncherInterface
*/
public function start();

/**
* Restart the process
*
* @return ProcessInterface
* @return LauncherInterface
*/
public function restart();

@@ -57,7 +57,7 @@ public function wait($waitTimer = 1000, bool $useYield = false);
* @param callable $failCallback
* @param callable $progressCallback
*
* @return ProcessInterface
* @return LauncherInterface
*/
public function then(callable $doneCallback, callable $failCallback = null, callable $progressCallback = null);

@@ -66,7 +66,7 @@ public function then(callable $doneCallback, callable $failCallback = null, call
*
* @param callable $callback
*
* @return ProcessInterface
* @return LauncherInterface
*/
public function done(callable $callback);

@@ -75,7 +75,7 @@ public function done(callable $callback);
*
* @param callable $progressCallback
*
* @return ProcessInterface
* @return LauncherInterface
*/
public function progress(callable $progressCallback);

@@ -84,7 +84,7 @@ public function progress(callable $progressCallback);
*
* @param mixed $update
*
* @return ProcessInterface
* @return LauncherInterface
*/
public function triggerOutput($update = null);

@@ -93,7 +93,7 @@ public function triggerOutput($update = null);
*
* @param callable $callback
*
* @return ProcessInterface
* @return LauncherInterface
*/
public function catch(callable $callback);

@@ -102,7 +102,7 @@ public function catch(callable $callback);
*
* @param callable $callback
*
* @return ProcessInterface
* @return LauncherInterface
*/
public function timeout(callable $callback);

@@ -136,7 +136,7 @@ public function getPid(): ?int;
/**
* Stops the running process.
*
* @return ProcessInterface
* @return LauncherInterface
*/
public function stop();

@@ -167,4 +167,11 @@ public function isTerminated(): bool;
* @return bool true if the process ended successfully, false otherwise
*/
public function isSuccessful(): bool;

/**
* Set process to display output of parent process.
*
* @return LauncherInterface
*/
public function showOutput();
}
10 changes: 5 additions & 5 deletions Processor/Processor.php
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
use Closure;
use Async\Processor\Launcher;
use Async\Processor\Process;
use Async\Processor\ProcessInterface;
use Async\Processor\LauncherInterface;
use Opis\Closure\SerializableClosure;

class Processor
@@ -62,9 +62,9 @@ public static function init(string $autoload = null)
*
* @param mixed $task
*
* @return ProcessInterface
* @return LauncherInterface
*/
public static function create($task, int $timeout = 300, $input = null): ProcessInterface
public static function create($task, int $timeout = 300, $input = null): LauncherInterface
{
if (!self::$isInitialized) {
self::init();
@@ -99,9 +99,9 @@ public static function phpPath(string $executable): void
*
* @param string $task daemon
*
* @return ProcessInterface
* @return LauncherInterface
*/
public static function daemon($task, $channel = null): ProcessInterface
public static function daemon($task, $channel = null): LauncherInterface
{
if (\is_string($task)) {
$shadow = (('\\' === \DIRECTORY_SEPARATOR) ? 'start /b ' : 'nohup ') . $task;
8 changes: 4 additions & 4 deletions Processor/functions.php
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
declare(strict_types=1);

use Async\Processor\Processor;
use Async\Processor\ProcessInterface;
use Async\Processor\LauncherInterface;

if (!\function_exists('spawn')) {
/**
@@ -13,14 +13,14 @@
* @param int $timeout
* @param mixed $processChannel
*
* @return ProcessInterface
* @return LauncherInterface
*/
function spawn($shellCallable, int $timeout = 300, $processChannel = null): ProcessInterface
function spawn($shellCallable, int $timeout = 300, $processChannel = null): LauncherInterface
{
return Processor::create($shellCallable, $timeout, $processChannel);
}

function await_spawn(ProcessInterface $process)
function await_spawn(LauncherInterface $process)
{
return $process->run();
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -19,9 +19,9 @@
}
],
"require": {
"php": ">7.1",
"php": ">7.2",
"opis/closure": "^3.5.1",
"symfony/process": "^5.0.0"
"symfony/process": "^5.0.4"
},
"autoload": {
"files": [
10 changes: 10 additions & 0 deletions tests/ProcessorTest.php
Original file line number Diff line number Diff line change
@@ -98,6 +98,16 @@ public function testStart()
$this->assertTrue($process->isTerminated());
}

public function testLiveOutput()
{
$process = Processor::create(function () {
echo 'hello child';
});
$this->expectOutputRegex('/hello child/');
$process->showOutput()->start();;
$process->wait();
}

public function testGetOutputShell()
{
if ('\\' === \DIRECTORY_SEPARATOR) {

0 comments on commit 9167ab3

Please sign in to comment.