Skip to content

Commit

Permalink
fixes/tests for the default uv_spawn usage
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTechsTech committed Mar 23, 2020
1 parent 1714167 commit d7a310d
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 142 deletions.
2 changes: 0 additions & 2 deletions Spawn/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@

exit(0);
} catch (\Throwable $exception) {
require_once __DIR__ . \DIRECTORY_SEPARATOR . 'SerializableException.php';

$output = new SerializableException($exception);

$channel->error(\base64_encode(\serialize($output)));
Expand Down
64 changes: 47 additions & 17 deletions Spawn/Launcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Async\Spawn\SpawnError;
use Async\Spawn\SerializableException;
use Async\Spawn\LauncherInterface;
use UVProcess;

/**
* Launcher runs a command/script/application/callable in an independent process.
Expand All @@ -26,6 +27,7 @@ class Launcher implements LauncherInterface
* @var Process|\UVProcess
*/
protected $process;
protected $task;
protected $id;
protected $pid;
protected $in;
Expand Down Expand Up @@ -65,7 +67,8 @@ private function __construct(
\UVPipe $error = null,
\UVTimer $timer = null,
\UVLoop $loop = null,
bool $isYield = false
bool $isYield = false,
$task = null
) {
$this->timeout = $timeout;
$this->process = $process;
Expand All @@ -74,8 +77,9 @@ private function __construct(
$this->out = $output;
$this->err = $error;
$this->timer = $timer;
self::$uv = $loop;
$this->isYield = $isYield;
$this->task = $task;
self::$uv = $loop;
self::$launcher[$id] = $this;
}

Expand Down Expand Up @@ -125,16 +129,18 @@ public static function add(
$id = (int) $getId;
$launcher = isset($launch[$id]) ? $launch[$id] : null;
if ($launcher instanceof Launcher) {
\uv_idle_stop($launcher->idle);
\uv_unref($launcher->idle);
if ($launcher->idle instanceof \UVIdle && \uv_is_active($launcher->idle)) {
\uv_idle_stop($launcher->idle);
\uv_unref($launcher->idle);
}

if ($launcher->timer instanceof \UVTimer && \uv_is_active($launcher->timer)) {
\uv_timer_stop($launcher->timer);
\uv_unref($launcher->timer);
}

if ($signal) {
if ($signal === \UV::SIGABRT) {
if ($signal === \SIGINT) {
$launcher->status = 'timeout';
$launcher->triggerTimeout($launcher->isYield);
} else {
Expand Down Expand Up @@ -183,7 +189,8 @@ public static function add(
0
);
} else {
$taskArray = \explode(' ', $task);
$cmd = (\IS_WINDOWS) ? 'cmd /c ' . $task : $task;
$taskArray = \explode(' ', $cmd);
$process = \uv_spawn(
$uvLoop,
\array_shift($taskArray),
Expand All @@ -196,18 +203,31 @@ public static function add(
);
}

$timer = null;
$timer = \uv_timer_init($uvLoop);
if ($timeout) {
$timer = \uv_timer_init($uvLoop);
\uv_timer_start($timer, $timeout * 1000, 0, function ($timer) use ($process, $getId, &$launch) {
$launch[$getId]->status = 'timeout';
\uv_process_kill($process, \UV::SIGABRT);
\uv_timer_stop($timer);
if ($process instanceof \UVProcess && \uv_is_active($process)) {
\uv_process_kill($process, \SIGINT);
}

//\uv_timer_stop($timer);
\uv_unref($timer);
});
}

return new self($process, (int) $getId, $timeout, $in, $out, $err, $timer, $uvLoop, $isYield);
return new self(
$process,
(int) $getId,
$timeout,
$in,
$out,
$err,
$timer,
$uvLoop,
$isYield,
$task
);
}

public function start(): LauncherInterface
Expand Down Expand Up @@ -254,14 +274,21 @@ public function start(): LauncherInterface

public function restart(): LauncherInterface
{
if ($this->isRunning())
$this->stop();
if ($this->process instanceof Process) {
if ($this->isRunning())
$this->stop();

$process = clone $this->process;
$process = clone $this->process;
$launcher = $this->create($process, $this->id, $this->timeout);

$launcher = $this->create($process, $this->id, $this->timeout);
return $launcher->start();
} else {
$launcher = self::add($this->task);
if ($this->isRunning())
$this->stop();

return $launcher->start();
return $launcher->start();
}
}

public function run(bool $useYield = false)
Expand Down Expand Up @@ -365,7 +392,7 @@ protected function checkProcess(bool $useYield = false)
public function stop(): LauncherInterface
{
if ($this->process instanceof \UVProcess) {
\uv_process_kill($this->process, \UV::SIGKILL);
\uv_process_kill($this->process, \SIGKILL);
} else {
$this->process->stop();
}
Expand Down Expand Up @@ -529,6 +556,9 @@ public function getId(): int

public function getPid(): ?int
{
if ($this->process instanceof \UVProcess)
return (int) $this->process;

return $this->pid;
}

Expand Down
2 changes: 1 addition & 1 deletion Spawn/LauncherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function getErrorOutput();
/**
* Returns the Pid (process identifier), if applicable.
*
* @return int|null The process id if running, null otherwise
* @return int|null The process id if running, null otherwise
*/
public function getPid(): ?int;

Expand Down
4 changes: 2 additions & 2 deletions Spawn/Spawn.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ public static function uvLoop(\UVLoop $loop)
public static function daemon($task, $channel = null): LauncherInterface
{
if (\is_string($task)) {
$shadow = (('\\' === \DIRECTORY_SEPARATOR) ? 'start /b ' : 'nohup ') . $task;
$shadow = (('\\' === \IS_WINDOWS) ? 'start /b ' : 'nohup ') . $task;
} else {
$shadow[] = ('\\' === \DIRECTORY_SEPARATOR) ? 'start /b' : 'nohup';
$shadow[] = ('\\' === \IS_WINDOWS) ? 'start /b' : 'nohup';
$shadow[] = $task;
}

Expand Down
3 changes: 1 addition & 2 deletions examples/spawn.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ function (ChannelInterface $channel) {
echo $channel->read();
\usleep(1000);
return 'The game!';
},
0
}
)->progress(function ($type, $data) use ($ipc) {
if ('ping' === $data) {
$ipc->send('pang' . \PHP_EOL);
Expand Down
2 changes: 1 addition & 1 deletion tests/ChannelFallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testSimpleChannel()
$channel->write('ping');
echo $channel->read();
echo $channel->read();
usleep(1000);
usleep(5000);
return 9;
}, 10, $ipc)
->progress(
Expand Down
Loading

0 comments on commit d7a310d

Please sign in to comment.