/
worker
54 lines (44 loc) · 1.44 KB
/
worker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env php
<?php
use Amp\Parallel\Sync;
use Amp\Parallel\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',
];
$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 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);
}
$channel->send($result); // Do not yield sending result on channel, process does not care if result arrives.
});