-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Process.php
182 lines (158 loc) · 5.2 KB
/
Process.php
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Aerys;
use Amp\Loop;
use Psr\Log\LoggerInterface as PsrLogger;
abstract class Process {
const STOPPED = 0;
const STARTED = 1;
const STOPPING = 2;
private $logger;
private $exitCode = 0;
private $state = self::STOPPED;
abstract protected function doStart(Console $console): \Generator;
abstract protected function doStop(): \Generator;
public function __construct(PsrLogger $logger) {
$this->logger = $logger;
}
/**
* Start the process.
*
* @param \Aerys\Console $console
* @return \Generator
*/
public function start(Console $console): \Generator {
try {
if ($this->state) {
throw new \Error(
"A process may only be started once"
);
}
$this->registerSignalHandler();
$this->registerShutdownHandler();
$this->registerErrorHandler();
$this->state = self::STARTED;
yield from $this->doStart($console);
// Once we make it this far we no longer want to terminate
// the process in the event of an uncaught exception inside
// the event loop -- log it instead.
Loop::setErrorHandler([$this->logger, "critical"]);
} catch (\Throwable $uncaught) {
$this->exitCode = 1;
$this->logger->critical($uncaught);
if (method_exists($this->logger, "flush")) {
$this->logger->flush();
}
static::exit();
}
}
/**
* Stop the process.
*
* @return \Generator
*/
public function stop(): \Generator {
try {
switch ($this->state) {
case self::STOPPED:
case self::STOPPING:
return;
case self::STARTED:
break;
}
$this->state = self::STOPPING;
yield from $this->doStop();
} catch (\Throwable $uncaught) {
$this->exitCode = 1;
$this->logger->critical($uncaught);
} finally {
static::exit();
}
}
private function registerSignalHandler() {
if (PHP_SAPI === "phpdbg") {
// phpdbg captures SIGINT so don't bother inside the debugger
return;
}
$onSignal = [$this, "stop"];
$loop = Loop::get()->getHandle();
if (is_resource($loop) && get_resource_type($loop) == "uv_loop") {
Loop::unreference(Loop::onSignal(\UV::SIGINT, $onSignal));
Loop::unreference(Loop::onSignal(\UV::SIGTERM, $onSignal));
} elseif (extension_loaded("pcntl")) {
Loop::unreference(Loop::onSignal(\SIGINT, $onSignal));
Loop::unreference(Loop::onSignal(\SIGTERM, $onSignal));
}
}
private function registerShutdownHandler() {
register_shutdown_function(function () {
if (!$err = \error_get_last()) {
return;
}
switch ($err["type"]) {
case E_ERROR:
case E_PARSE:
case E_USER_ERROR:
case E_CORE_ERROR:
case E_CORE_WARNING:
case E_COMPILE_ERROR:
case E_COMPILE_WARNING:
case E_RECOVERABLE_ERROR:
break;
default:
return;
}
$this->exitCode = 1;
$msg = "{$err["message"]} in {$err["file"]} on line {$err["line"]}";
$previous = Loop::get();
try {
Loop::set((new Loop\DriverFactory)->create());
Loop::run(function () use ($msg) {
$this->logger->critical($msg);
yield from $this->stop();
});
} finally {
Loop::set($previous);
}
});
}
private function registerErrorHandler() {
set_error_handler(function ($errno, $msg, $file, $line) {
if (!(error_reporting() & $errno)) {
return;
}
$msg = "{$msg} in {$file} on line {$line}";
switch ($errno) {
case E_ERROR:
case E_PARSE:
case E_USER_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_RECOVERABLE_ERROR:
$this->logger->error($msg);
break;
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_WARNING:
case E_USER_WARNING:
$this->logger->warning($msg);
break;
case E_NOTICE:
case E_USER_NOTICE:
case E_DEPRECATED:
case E_USER_DEPRECATED:
case E_STRICT:
$this->logger->notice($msg);
break;
default:
$this->logger->warning($msg);
break;
}
});
}
/**
* This function only exists as protected so we can test for its invocation.
*/
protected function exit() {
exit($this->exitCode);
}
}