1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-27 04:44:56 +01:00
parallel/bin/worker
2017-03-16 17:03:59 -05:00

50 lines
1.3 KiB
PHP

#!/usr/bin/env php
<?php declare(strict_types = 1);
use Amp\Parallel\Sync\{ ChannelledSocket, Internal\ExitFailure, Internal\ExitSuccess };
use Amp\Parallel\Worker\{ BasicEnvironment, Internal\TaskRunner };
@cli_set_process_title('amp-worker');
// Redirect all output written using echo, print, printf, etc. to STDERR.
ob_start(function ($data) {
fwrite(STDERR, $data);
return '';
}, 1, PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_FLUSHABLE);
(function () {
$paths = [
dirname(__DIR__, 3) . '/autoload.php',
dirname(__DIR__) . '/vendor/autoload.php',
];
$autoloadPath = null;
foreach ($paths as $path) {
if (file_exists($path)) {
$autoloadPath = $path;
break;
}
}
if ($autoloadPath === null) {
fwrite(STDERR, 'Could not locate autoload.php.');
exit(1);
}
require $autoloadPath;
})();
Amp\Loop::run(function () {
$channel = new ChannelledSocket(STDIN, STDOUT, false);
$environment = new BasicEnvironment;
$runner = new TaskRunner($channel, $environment);
try {
$result = new ExitSuccess(yield $runner->run());
} catch (Throwable $exception) {
$result = new ExitFailure($exception);
}
$channel->send($result); // Do not yield sending result on channel, process does not care if result arrives.
});