|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Jenner |
|
5
|
|
|
* Date: 2015/8/12 |
|
6
|
|
|
* Time: 15:25 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Jenner\SimpleFork; |
|
10
|
|
|
|
|
11
|
|
|
class Process |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Runnable|callable |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $runnable; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var int |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $pid = 0; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string custom process name |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $name = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var bool if the process is started |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $started = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $running = false; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var int the signal which made the process terminate |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $term_signal = null; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var int the signal which made the process stop |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $stop_signal = null; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var int error code |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $errno = null; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string error message |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $errmsg = null; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var bool |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $if_signal = false; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var array |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $callbacks = array(); |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var array signal handlers |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $signal_handlers = array(); |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $execution it can be a Runnable object, callback function or null |
|
76
|
|
|
* @param null $name process name,you can manager the process by it's name. |
|
77
|
|
|
*/ |
|
78
|
61 |
|
public function __construct($execution = null, $name = null) |
|
79
|
|
|
{ |
|
80
|
61 |
|
if (!is_null($execution) && $execution instanceof Runnable) { |
|
81
|
9 |
|
$this->runnable = $execution; |
|
82
|
61 |
|
} elseif (!is_null($execution) && is_callable($execution)) { |
|
83
|
46 |
|
$this->runnable = $execution; |
|
84
|
55 |
|
} elseif (!is_null($execution)) { |
|
85
|
|
|
$message = "param execution is not a object of Runnable or callable"; |
|
86
|
|
|
throw new \InvalidArgumentException($message); |
|
87
|
|
|
} else { |
|
88
|
12 |
|
Utils::checkOverwriteRunMethod(get_class($this)); |
|
89
|
|
|
} |
|
90
|
58 |
|
if (!is_null($name)) { |
|
91
|
3 |
|
$this->name = $name; |
|
92
|
3 |
|
} |
|
93
|
|
|
|
|
94
|
58 |
|
$this->initStatus(); |
|
95
|
58 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* init process status |
|
99
|
|
|
*/ |
|
100
|
58 |
|
protected function initStatus() |
|
101
|
|
|
{ |
|
102
|
58 |
|
$this->pid = null; |
|
103
|
58 |
|
$this->running = null; |
|
104
|
58 |
|
$this->term_signal = null; |
|
105
|
58 |
|
$this->stop_signal = null; |
|
106
|
58 |
|
$this->errno = null; |
|
107
|
58 |
|
$this->errmsg = null; |
|
108
|
58 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* get pid |
|
112
|
|
|
* |
|
113
|
|
|
* @return int |
|
114
|
|
|
*/ |
|
115
|
9 |
|
public function getPid() |
|
116
|
|
|
{ |
|
117
|
9 |
|
return $this->pid; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* get or set name |
|
122
|
|
|
* |
|
123
|
|
|
* @param string|null $name |
|
124
|
|
|
* @return mixed |
|
125
|
|
|
*/ |
|
126
|
3 |
|
public function name($name = null) |
|
127
|
|
|
{ |
|
128
|
3 |
|
if (!is_null($name)) { |
|
129
|
|
|
$this->name = $name; |
|
130
|
|
|
} else { |
|
131
|
3 |
|
return $this->name; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* if the process is stopped |
|
137
|
|
|
* |
|
138
|
|
|
* @return bool |
|
139
|
|
|
*/ |
|
140
|
3 |
|
public function isStopped() |
|
141
|
|
|
{ |
|
142
|
3 |
|
return !$this->isRunning(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* if the process is running |
|
147
|
|
|
* |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
55 |
|
public function isRunning() |
|
151
|
|
|
{ |
|
152
|
55 |
|
$this->updateStatus(); |
|
153
|
55 |
|
return $this->running; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* update the process status |
|
158
|
|
|
* |
|
159
|
|
|
* @param bool $block |
|
160
|
|
|
*/ |
|
161
|
55 |
|
protected function updateStatus($block = false) |
|
162
|
|
|
{ |
|
163
|
55 |
|
if ($this->running !== true) { |
|
164
|
24 |
|
return; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
55 |
|
if ($block) { |
|
168
|
12 |
|
$res = pcntl_waitpid($this->pid, $status); |
|
|
|
|
|
|
169
|
12 |
|
} else { |
|
170
|
55 |
|
$res = pcntl_waitpid($this->pid, $status, WNOHANG | WUNTRACED); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
55 |
|
if ($res === -1) { |
|
174
|
|
|
$message = "pcntl_waitpid failed. the process maybe available"; |
|
175
|
|
|
throw new \RuntimeException($message); |
|
176
|
55 |
|
} elseif ($res === 0) { |
|
177
|
55 |
|
$this->running = true; |
|
178
|
55 |
|
} else { |
|
179
|
52 |
|
if (pcntl_wifsignaled($status)) { |
|
180
|
6 |
|
$this->term_signal = pcntl_wtermsig($status); |
|
181
|
6 |
|
} |
|
182
|
52 |
|
if (pcntl_wifstopped($status)) { |
|
183
|
|
|
$this->stop_signal = pcntl_wstopsig($status); |
|
184
|
|
|
} |
|
185
|
52 |
|
if (pcntl_wifexited($status)) { |
|
186
|
46 |
|
$this->errno = pcntl_wexitstatus($status); |
|
187
|
46 |
|
$this->errmsg = pcntl_strerror($this->errno); |
|
188
|
46 |
|
} else { |
|
189
|
6 |
|
$this->errno = pcntl_get_last_error(); |
|
190
|
6 |
|
$this->errmsg = pcntl_strerror($this->errno); |
|
191
|
|
|
} |
|
192
|
52 |
|
if (pcntl_wifsignaled($status)) { |
|
193
|
6 |
|
$this->if_signal = true; |
|
194
|
6 |
|
} else { |
|
195
|
46 |
|
$this->if_signal = false; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
52 |
|
$this->running = false; |
|
199
|
|
|
} |
|
200
|
55 |
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* if the process is started |
|
204
|
|
|
* |
|
205
|
|
|
* @return bool |
|
206
|
|
|
*/ |
|
207
|
15 |
|
public function isStarted() |
|
208
|
|
|
{ |
|
209
|
15 |
|
return $this->started; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* get pcntl errno |
|
214
|
|
|
* |
|
215
|
|
|
* @return int |
|
216
|
|
|
*/ |
|
217
|
9 |
|
public function errno() |
|
218
|
|
|
{ |
|
219
|
9 |
|
return $this->errno; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* get pcntl errmsg |
|
224
|
|
|
* |
|
225
|
|
|
* @return string |
|
226
|
|
|
*/ |
|
227
|
6 |
|
public function errmsg() |
|
228
|
|
|
{ |
|
229
|
6 |
|
return $this->errmsg; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
3 |
|
public function ifSignal() |
|
233
|
|
|
{ |
|
234
|
3 |
|
return $this->if_signal; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* start the sub process |
|
239
|
|
|
* and run the callback |
|
240
|
|
|
* |
|
241
|
|
|
* @return string pid |
|
242
|
|
|
*/ |
|
243
|
55 |
|
public function start() |
|
244
|
|
|
{ |
|
245
|
55 |
|
if (!empty($this->pid) && $this->isRunning()) { |
|
246
|
|
|
throw new \LogicException("the process is already running"); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
55 |
|
$callback = $this->getCallable(); |
|
250
|
|
|
|
|
251
|
55 |
|
$pid = pcntl_fork(); |
|
252
|
55 |
|
if ($pid < 0) { |
|
253
|
|
|
throw new \RuntimeException("fork error"); |
|
254
|
55 |
|
} elseif ($pid > 0) { |
|
255
|
55 |
|
$this->pid = $pid; |
|
256
|
55 |
|
$this->running = true; |
|
257
|
55 |
|
$this->started = true; |
|
258
|
55 |
|
} else { |
|
259
|
|
|
$this->pid = getmypid(); |
|
260
|
|
|
$this->signal(); |
|
261
|
|
|
foreach ($this->signal_handlers as $signal => $handler) { |
|
262
|
|
|
pcntl_signal($signal, $handler); |
|
263
|
|
|
} |
|
264
|
|
|
call_user_func($callback); |
|
265
|
|
|
exit(0); |
|
|
|
|
|
|
266
|
|
|
} |
|
267
|
55 |
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* get sub process callback |
|
271
|
|
|
* |
|
272
|
|
|
* @return array|callable|null |
|
273
|
|
|
*/ |
|
274
|
55 |
|
protected function getCallable() |
|
275
|
|
|
{ |
|
276
|
55 |
|
$callback = null; |
|
|
|
|
|
|
277
|
55 |
|
if (is_object($this->runnable) && $this->runnable instanceof Runnable) { |
|
278
|
9 |
|
$callback = array($this->runnable, 'run'); |
|
279
|
55 |
|
} elseif (is_callable($this->runnable)) { |
|
280
|
46 |
|
$callback = $this->runnable; |
|
281
|
46 |
|
} else { |
|
282
|
6 |
|
$callback = array($this, 'run'); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
55 |
|
return $callback; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* register signal SIGTERM handler, |
|
290
|
|
|
* when the parent process call shutdown and use the default signal, |
|
291
|
|
|
* this handler will be triggered |
|
292
|
|
|
*/ |
|
293
|
|
|
protected function signal() |
|
294
|
|
|
{ |
|
295
|
|
|
pcntl_signal(SIGTERM, function () { |
|
296
|
|
|
exit(0); |
|
|
|
|
|
|
297
|
|
|
}); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* kill self |
|
302
|
|
|
* |
|
303
|
|
|
* @param bool|true $block |
|
304
|
|
|
* @param int $signal |
|
305
|
|
|
*/ |
|
306
|
12 |
|
public function shutdown($block = true, $signal = SIGTERM) |
|
307
|
|
|
{ |
|
308
|
12 |
|
if (empty($this->pid)) { |
|
309
|
|
|
$message = "the process pid is null, so maybe the process is not started"; |
|
310
|
|
|
throw new \LogicException($message); |
|
311
|
|
|
} |
|
312
|
12 |
|
if (!$this->isRunning()) { |
|
313
|
|
|
throw new \LogicException("the process is not running"); |
|
314
|
|
|
} |
|
315
|
12 |
|
if (!posix_kill($this->pid, $signal)) { |
|
316
|
|
|
throw new \RuntimeException("kill son process failed"); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
12 |
|
$this->updateStatus($block); |
|
320
|
12 |
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* waiting for the sub process exit |
|
324
|
|
|
* |
|
325
|
|
|
* @param bool|true $block if block the process |
|
326
|
|
|
* @param int $sleep default 0.1s check sub process status |
|
327
|
|
|
* every $sleep milliseconds. |
|
328
|
|
|
*/ |
|
329
|
34 |
|
public function wait($block = true, $sleep = 100000) |
|
330
|
|
|
{ |
|
331
|
34 |
|
while (true) { |
|
332
|
34 |
|
if ($this->isRunning() === false) { |
|
333
|
34 |
|
return; |
|
334
|
|
|
} |
|
335
|
31 |
|
if (!$block) { |
|
336
|
|
|
break; |
|
337
|
|
|
} |
|
338
|
31 |
|
usleep($sleep); |
|
339
|
31 |
|
} |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* register sub process signal handler, |
|
344
|
|
|
* when the sub process start, the handlers will be registered |
|
345
|
|
|
* |
|
346
|
|
|
* @param $signal |
|
347
|
|
|
* @param callable $handler |
|
348
|
|
|
*/ |
|
349
|
|
|
public function registerSignalHandler($signal, callable $handler) |
|
350
|
|
|
{ |
|
351
|
|
|
$this->signal_handlers[$signal] = $handler; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* you should overwrite this function |
|
356
|
|
|
* if you do not use the Runnable or callback. |
|
357
|
|
|
*/ |
|
358
|
|
|
public function run() |
|
359
|
|
|
{ |
|
360
|
|
|
} |
|
361
|
|
|
} |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.