-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathprocess-runner.php
More file actions
98 lines (78 loc) · 2.71 KB
/
process-runner.php
File metadata and controls
98 lines (78 loc) · 2.71 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php declare(strict_types=1);
namespace Amp\Parallel\Context\Internal;
use Amp\ByteStream;
use Amp\Parallel\Context\ProcessContext;
use Amp\Parallel\Ipc;
use Amp\Parallel\Ipc\IpcHub;
use Amp\TimeoutCancellation;
use Revolt\EventLoop;
\define("AMP_CONTEXT", "process");
\define("AMP_CONTEXT_ID", \getmypid());
// Doesn't exist in phpdbg...
if (\function_exists("cli_set_process_title")) {
\set_error_handler(static fn () => true);
try {
\cli_set_process_title("amp-process");
} finally {
\restore_error_handler();
}
}
(function (): void {
$paths = [
\dirname(__DIR__, 5) . "/autoload.php",
\dirname(__DIR__, 3) . "/vendor/autoload.php",
];
foreach ($paths as $path) {
if (\file_exists($path)) {
$autoloadPath = $path;
break;
}
}
if (!isset($autoloadPath)) {
\trigger_error(
"Could not locate autoload.php in any of the following files: " . \implode(", ", $paths),
E_USER_ERROR,
);
}
/** @psalm-suppress UnresolvableInclude */
require $autoloadPath;
})();
// Wrap in a closure to keep local variables out of global scope
(function () use ($argv, $argc): void {
try {
foreach (ProcessContext::getIgnoredSignals() as $signal) {
EventLoop::unreference(EventLoop::onSignal($signal, static fn () => null));
}
} catch (EventLoop\UnsupportedFeatureException) {
// Signal handling not supported on current event loop driver.
}
/** @var list<string> $argv */
if (!isset($argv[1])) {
\trigger_error("No socket path provided", E_USER_ERROR);
}
if (!isset($argv[2])) {
\trigger_error("No hub class provided", E_USER_ERROR);
}
if (!isset($argv[3]) || !\is_numeric($argv[3])) {
\trigger_error("No key length provided", E_USER_ERROR);
}
if (!isset($argv[4]) || !\is_numeric($argv[4])) {
\trigger_error("No timeout provided", E_USER_ERROR);
}
[, $uri, $hubClass, $length, $timeout] = $argv;
$length = (int) $length;
$timeout = (int) $timeout;
\assert($length > 0 && $timeout > 0);
if (!isset(\class_implements($hubClass)[IpcHub::class])) {
throw new \Error("Passed hub class $hubClass does not implement IpcHub!");
}
// Remove script path, socket path, key length, and timeout from process arguments.
$argv = \array_slice($argv, 5);
try {
$cancellation = new TimeoutCancellation($timeout);
$key = Ipc\readKey(ByteStream\getStdin(), $cancellation, $length);
} catch (\Throwable $exception) {
\trigger_error($exception->getMessage(), E_USER_ERROR);
}
runContext($hubClass, $uri, $key, $cancellation, $argv);
})();