Skip to content

Commit

Permalink
Merge pull request #112 from spatie/configure-binary
Browse files Browse the repository at this point in the history
Make binary configurable
  • Loading branch information
brendt authored Apr 15, 2020
2 parents 4deae48 + 4e39ec3 commit f047726
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
composer.lock
vendor
tests/another-php-binary
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ $pool->wait();
Note that a pool will be rendered useless after being stopped, and a new pool should be
created if needed.

### Using another PHP binary

By default the pool will use `php` to execute its child processes. You can configure another binary like so:

```php
Pool::create()
->withBinary('/path/to/php');
```

### Working with tasks

Besides using closures, you can also work with a `Task`.
Expand Down
15 changes: 14 additions & 1 deletion src/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Pool implements ArrayAccess

protected $stopped = false;

protected $binary = 'php';

public function __construct()
{
if (static::isSupported()) {
Expand Down Expand Up @@ -92,6 +94,13 @@ public function sleepTime(int $sleepTime): self
return $this;
}

public function withBinary(string $binary): self
{
$this->binary = $binary;

return $this;
}

public function notify()
{
if (count($this->inProgress) >= $this->concurrency) {
Expand Down Expand Up @@ -120,7 +129,11 @@ public function add($process, ?int $outputLength = null): Runnable
}

if (! $process instanceof Runnable) {
$process = ParentRuntime::createProcess($process, $outputLength);
$process = ParentRuntime::createProcess(
$process,
$outputLength,
$this->binary
);
}

$this->putInQueue($process);
Expand Down
4 changes: 2 additions & 2 deletions src/Runtime/ParentRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function init(string $autoloader = null)
*
* @return \Spatie\Async\Process\Runnable
*/
public static function createProcess($task, ?int $outputLength = null): Runnable
public static function createProcess($task, ?int $outputLength = null, ?string $binary = 'php'): Runnable
{
if (! self::$isInitialised) {
self::init();
Expand All @@ -65,7 +65,7 @@ public static function createProcess($task, ?int $outputLength = null): Runnable
}

$process = new Process([
'php',
$binary,
self::$childProcessScript,
self::$autoloader,
self::encodeTask($task),
Expand Down
30 changes: 30 additions & 0 deletions tests/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ public function it_can_handle_success()
$this->assertEquals(10, $counter, (string) $pool->status());
}

/** @test */
public function it_can_configure_another_binary()
{
$binary = __DIR__.'/another-php-binary';

if (! file_exists($binary)) {
symlink(PHP_BINARY, $binary);
}

$pool = Pool::create()->withBinary($binary);

$counter = 0;

foreach (range(1, 5) as $i) {
$pool->add(function () {
return 2;
})->then(function (int $output) use (&$counter) {
$counter += $output;
});
}

$pool->wait();

$this->assertEquals(10, $counter, (string) $pool->status());

if (file_exists($binary)) {
unlink($binary);
}
}

/** @test */
public function it_can_handle_timeout()
{
Expand Down

0 comments on commit f047726

Please sign in to comment.