1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-22 22:11:11 +01:00
parallel/bin/worker
2017-07-17 22:49:17 -05:00

60 lines
1.5 KiB
PHP

#!/usr/bin/env php
<?php
use Amp\Parallel\Sync;
use Amp\Parallel\Worker;
const AMP_WORKER = "amp-worker";
// Doesn't exist in phpdbg...
if (function_exists("cli_set_process_title")) {
@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",
];
foreach ($paths as $path) {
if (file_exists($path)) {
$autoloadPath = $path;
break;
}
}
if (!isset($autoloadPath)) {
fwrite(STDERR, "Could not locate autoload.php");
exit(1);
}
require $autoloadPath;
})();
Amp\Loop::run(function () {
$channel = new Sync\ChannelledSocket(STDIN, STDOUT);
$environment = new Worker\BasicEnvironment;
$runner = new Worker\Internal\TaskRunner($channel, $environment);
try {
$result = new Sync\Internal\ExitSuccess(yield $runner->run());
} catch (Sync\ChannelException $exception) {
exit(1); // Parent context died, simply exit.
} catch (Throwable $exception) {
$result = new Sync\Internal\ExitFailure($exception);
}
try {
yield $channel->send($result);
} catch (Throwable $exception) {
exit(1); // Parent context died, simply exit.
}
});