Skip to content

Commit

Permalink
General fixes, docblock updates and comments, BC
Browse files Browse the repository at this point in the history
- added `displayOn()` and `display()` to show child process output when using `wait()/run()`
- updated required, name changes
  • Loading branch information
TheTechsTech committed Feb 15, 2020
1 parent b8a37ec commit 95ebf4d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 21 deletions.
1 change: 0 additions & 1 deletion Processor/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Async\Processor\Processor;
use Async\Processor\ProcessorError;
use Async\Processor\SerializableException;

try {
Expand Down
15 changes: 10 additions & 5 deletions Processor/Launcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ public function yielding()
return yield from $this->run(true);
}

public function display()
{
if ($this->showOutput) {
\printf('%s', $this->getRealOutput());
$this->realOutput = null;
}
}

public function wait($waitTimer = 1000, bool $useYield = false)
{
while ($this->isRunning()) {
Expand All @@ -101,10 +109,7 @@ public function wait($waitTimer = 1000, bool $useYield = false)
return $this->triggerTimeout();
}

if ($this->showOutput) {
\printf('%s', $this->getRealOutput());
$this->realOutput = null;
}
$this->display();

if ($useYield)
$this->yieldLiveUpdate($this->getRealOutput());
Expand Down Expand Up @@ -153,7 +158,7 @@ public function isRunning(): bool
return $this->process->isRunning();
}

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

Expand Down
19 changes: 17 additions & 2 deletions Processor/LauncherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Async\Processor;

use Async\Processor\Process;

interface LauncherInterface
{
/**
Expand Down Expand Up @@ -169,9 +171,22 @@ public function isTerminated(): bool;
public function isSuccessful(): bool;

/**
* Set process to display output of parent process.
* Set process to display output of child process.
*
* @return LauncherInterface
*/
public function showOutput();
public function displayOn();

/**
* Display child process output, if set.
*/
public function display();


/**
* A PHP process
*
* @return Process
*/
public function getProcess(): Process;
}
7 changes: 5 additions & 2 deletions Processor/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ function spawn($shellCallable, int $timeout = 300, $processChannel = null): Laun
return Processor::create($shellCallable, $timeout, $processChannel);
}

function spawn_run(LauncherInterface $process)
/**
* Start the process and wait to terminate, and return results in index array.
*/
function spawn_run(LauncherInterface $process, bool $displayOutput = false)
{
return $process->run();
return $displayOutput ? $process->displayOn()->run() : $process->run();
}
}
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ $process = Processor::create(function () use ($thing) {
\spawn_run($process);
// Or
$process->run();

// Second option can be used to set to display child output, default is false
\spawn_run($process, true);
// Or
$process->displayOn()->run();
```

## Event hooks

When creating asynchronous processes, you'll get an instance of `ProcessInterface` returned.
When creating asynchronous processes, you'll get an instance of `LauncherInterface` returned.
You can add the following event hooks on a process.

```php
Expand Down Expand Up @@ -85,6 +90,12 @@ There also `->done`, and `->progress` part of `->then()` extended callback metho
}
);

// To turn on to display child output.
->displayOn();

// To display child output, only by third party means once turned on.
->display();

// Processes can be retried.
->restart();
->run();
Expand Down
11 changes: 5 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">

<testsuites>
<testsuite name="Processor Test Suite">
<directory suffix=".php">tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">Processor/</directory>
<exclude>
<file>./Processor/Container.php</file>
</exclude>
</whitelist>
</whitelist>
</filter>

<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
Expand Down
2 changes: 0 additions & 2 deletions tests/ErrorHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Async\Tests;

use Error;
use ParseError;
use Async\Processor\Processor;
use Async\Processor\ProcessorError;
use PHPUnit\Framework\TestCase;
Expand Down
7 changes: 5 additions & 2 deletions tests/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Async\Tests;

use InvalidArgumentException;
use Async\Processor\Process;
use Async\Processor\Processor;
use Async\Processor\ProcessorError;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -86,6 +86,9 @@ public function testStart()
$process = Processor::create(function () {
usleep(1000);
});

$this->assertTrue($process->getProcess() instanceof Process);
$this->assertIsNumeric($process->getId());
$this->assertFalse($process->isRunning());
$this->assertFalse($process->isTimedOut());
$this->assertFalse($process->isTerminated());
Expand All @@ -105,7 +108,7 @@ public function testLiveOutput()
usleep(1000);
});
$this->expectOutputString('hello child');
$process->showOutput()->run();
$process->displayOn()->run();
}

public function testGetOutputShell()
Expand Down

0 comments on commit 95ebf4d

Please sign in to comment.