1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-30 04:39:01 +01:00
parallel/bin/worker.php
2015-12-12 00:47:41 -06:00

61 lines
1.5 KiB
PHP

#!/usr/bin/env php
<?php
// Redirect all output written using echo, print, printf, etc. to STDERR.
ob_start(function ($data) {
fwrite(STDERR, $data);
return '';
}, 1, 0);
$paths = [
dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'autoload.php',
dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php',
];
$autoloadPath = null;
foreach ($paths as $path) {
if (file_exists($path)) {
$autoloadPath = $path;
break;
}
}
if (null === $autoloadPath) {
fwrite(STDERR, 'Could not locate autoload.php.');
exit(1);
}
require $autoloadPath;
use Icicle\Concurrent\Sync\ChannelledStream;
use Icicle\Concurrent\Sync\Internal\ExitFailure;
use Icicle\Concurrent\Sync\Internal\ExitSuccess;
use Icicle\Concurrent\Worker\Environment;
use Icicle\Concurrent\Worker\Internal\TaskRunner;
use Icicle\Coroutine;
use Icicle\Loop;
use Icicle\Stream;
Coroutine\create(function () {
$channel = new ChannelledStream(Stream\stdin(), Stream\stdout());
$environment = new Environment();
$runner = new TaskRunner($channel, $environment);
try {
$result = new ExitSuccess(yield $runner->run());
} catch (Exception $exception) {
$result = new ExitFailure($exception);
}
// Attempt to return the result.
try {
yield $channel->send($result);
} catch (Exception $exception) {
// The result was not sendable! Try sending the reason why instead.
yield $channel->send(new ExitFailure($exception));
}
})->done();
Loop\run();