Total Complexity | 66 |
Total Lines | 404 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like worker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use worker, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class worker |
||
17 | { |
||
18 | // worker进程数 |
||
19 | public $count = 0; |
||
20 | // worker id,worker进程从1开始,0被master进程所使用 |
||
21 | public $worker_id = 0; |
||
22 | // worker 进程ID |
||
23 | public $worker_pid = 0; |
||
24 | // 进程用户 |
||
25 | public $user = ''; |
||
26 | // 进程名 |
||
27 | public $title = ''; |
||
28 | // 每个进程是否只运行一次 |
||
29 | public $run_once = true; |
||
30 | // 是否输出日志 |
||
31 | public $log_show = false; |
||
32 | // master进程启动回调 |
||
33 | public $on_start = false; |
||
34 | // master进程停止回调 |
||
35 | public $on_stop = false; |
||
36 | // worker进程启动回调 |
||
37 | public $on_worker_start = false; |
||
38 | // worker进程停止回调 |
||
39 | public $on_worker_stop = false; |
||
40 | // master进程ID |
||
41 | protected static $_master_pid = 0; |
||
42 | // worker进程ID |
||
43 | protected static $_worker_pids = array(); |
||
44 | // master、worker进程启动时间 |
||
45 | public $time_start = 0; |
||
46 | // master、worker进程运行状态 [starting|running|shutdown|reload] |
||
47 | protected static $_status = "starting"; |
||
48 | |||
49 | |||
50 | public function __construct() |
||
51 | { |
||
52 | self::$_master_pid = posix_getpid(); |
||
53 | // 产生时钟云,添加后父进程才可以收到信号 |
||
54 | declare(ticks = 1); |
||
55 | $this->install_signal(); |
||
|
|||
56 | } |
||
57 | |||
58 | /** |
||
59 | * 安装信号处理函数 |
||
60 | * @return void |
||
61 | */ |
||
62 | protected function install_signal() |
||
63 | { |
||
64 | // stop |
||
65 | pcntl_signal(SIGINT, array($this, 'signal_handler'), false); |
||
66 | // reload |
||
67 | pcntl_signal(SIGUSR1, array($this, 'signal_handler'), false); |
||
68 | // status |
||
69 | pcntl_signal(SIGUSR2, array($this, 'signal_handler'), false); |
||
70 | // ignore |
||
71 | pcntl_signal(SIGPIPE, SIG_IGN, false); |
||
72 | // install signal handler for dead kids |
||
73 | // pcntl_signal(SIGCHLD, array($this, 'signal_handler')); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * 卸载信号处理函数 |
||
78 | * @return void |
||
79 | */ |
||
80 | protected function uninstall_signal() |
||
81 | { |
||
82 | // uninstall stop signal handler |
||
83 | pcntl_signal(SIGINT, SIG_IGN, false); |
||
84 | // uninstall reload signal handler |
||
85 | pcntl_signal(SIGUSR1, SIG_IGN, false); |
||
86 | // uninstall status signal handler |
||
87 | pcntl_signal(SIGUSR2, SIG_IGN, false); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * 信号处理函数,会被其他类调用到,所以要设置为public |
||
92 | * @param int $signal |
||
93 | */ |
||
94 | public function signal_handler($signal) { |
||
95 | switch ($signal) { |
||
96 | // stop 2 |
||
97 | case SIGINT: |
||
98 | // master进程和worker进程都会调用 |
||
99 | $this->stop_all(); |
||
100 | break; |
||
101 | // reload 30 |
||
102 | case SIGUSR1: |
||
103 | echo "reload\n"; |
||
104 | break; |
||
105 | // show status 31 |
||
106 | case SIGUSR2: |
||
107 | echo "status\n"; |
||
108 | break; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * 运行worker实例 |
||
114 | */ |
||
115 | public function run() |
||
116 | { |
||
117 | $this->time_start = microtime(true); |
||
118 | $this->worker_id = 0; |
||
119 | $this->worker_pid = posix_getpid(); |
||
120 | $this->set_process_title($this->title); |
||
121 | |||
122 | // 这里赋值,worker进程也会克隆到 |
||
123 | if ($this->log_show) |
||
124 | { |
||
125 | log::$log_show = true; |
||
126 | } |
||
127 | |||
128 | if ($this->on_start) |
||
129 | { |
||
130 | call_user_func($this->on_start, $this); |
||
131 | } |
||
132 | |||
133 | // worker进程从1开始,0被master进程所使用 |
||
134 | for ($i = 1; $i <= $this->count; $i++) |
||
135 | { |
||
136 | $this->fork_one_worker($i); |
||
137 | } |
||
138 | $this->monitor_workers(); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * 创建一个子进程 |
||
143 | * @param Worker $worker |
||
144 | * @throws Exception |
||
145 | */ |
||
146 | public function fork_one_worker($worker_id) |
||
189 | } |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * 尝试设置运行当前进程的用户 |
||
194 | * |
||
195 | * @param $user_name |
||
196 | */ |
||
197 | protected static function set_process_user($user_name) |
||
198 | { |
||
199 | // 用户名为空 或者 当前用户不是root用户 |
||
200 | if(empty($user_name) || posix_getuid() !== 0) |
||
201 | { |
||
202 | return; |
||
203 | } |
||
204 | $user_info = posix_getpwnam($user_name); |
||
205 | if($user_info['uid'] != posix_getuid() || $user_info['gid'] != posix_getgid()) |
||
206 | { |
||
207 | if(!posix_setgid($user_info['gid']) || !posix_setuid($user_info['uid'])) |
||
208 | { |
||
209 | log::add('Can not run woker as '.$user_name." , You shuld be root", "Error"); |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * 设置当前进程的名称,在ps aux命令中有用 |
||
216 | * 注意 需要php>=5.5或者安装了protitle扩展 |
||
217 | * @param string $title |
||
218 | * @return void |
||
219 | */ |
||
220 | protected function set_process_title($title) |
||
221 | { |
||
222 | if (!empty($title)) |
||
223 | { |
||
224 | // 需要扩展 |
||
225 | if(extension_loaded('proctitle') && function_exists('setproctitle')) |
||
226 | { |
||
227 | @setproctitle($title); |
||
228 | } |
||
229 | // >=php 5.5 |
||
230 | elseif (function_exists('cli_set_process_title')) |
||
231 | { |
||
232 | cli_set_process_title($title); |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * 监控所有子进程的退出事件及退出码 |
||
239 | * @return void |
||
240 | */ |
||
241 | public function monitor_workers() |
||
242 | { |
||
243 | // 设置master进程的运行状态为运行中 |
||
244 | self::$_status = "running"; |
||
245 | while(1) |
||
246 | { |
||
247 | // pcntl_signal_dispatch 子进程无法接受到信号 |
||
248 | // 如果有信号到来,尝试触发信号处理函数 |
||
249 | //pcntl_signal_dispatch(); |
||
250 | // 挂起进程,直到有子进程退出或者被信号打断 |
||
251 | $status = 0; |
||
252 | $pid = pcntl_wait($status, WUNTRACED); |
||
253 | // 如果有信号到来,尝试触发信号处理函数 |
||
254 | //pcntl_signal_dispatch(); |
||
255 | |||
256 | // 子进程退出信号 |
||
257 | if($pid > 0) |
||
258 | { |
||
259 | //echo "worker[".$pid."] stop\n"; |
||
260 | //$this->stop(); |
||
261 | |||
262 | // 如果不是正常退出,是被kill等杀掉的 |
||
263 | if($status !== 0) |
||
264 | { |
||
265 | log::add("worker {$pid} exit with status $status", "Warning"); |
||
266 | } |
||
267 | |||
268 | // key 和 value 互换 |
||
269 | $worker_pids = array_flip(self::$_worker_pids); |
||
270 | // 通过 pid 得到 worker_id |
||
271 | $worker_id = $worker_pids[$pid]; |
||
272 | // 这里不unset掉,是为了进程重启 |
||
273 | self::$_worker_pids[$worker_id] = 0; |
||
274 | //unset(self::$_worker_pids[$pid]); |
||
275 | |||
276 | // 再生成一个worker |
||
277 | if (!$this->run_once) |
||
278 | { |
||
279 | $this->fork_one_worker($worker_id); |
||
280 | } |
||
281 | |||
282 | // 如果所有子进程都退出了,触发主进程退出函数 |
||
283 | $all_worker_stop = true; |
||
284 | foreach (self::$_worker_pids as $_worker_pid) |
||
285 | { |
||
286 | // 只要有一个worker进程还存在进程ID,就不算退出 |
||
287 | if ($_worker_pid != 0) |
||
288 | { |
||
289 | $all_worker_stop = false; |
||
290 | } |
||
291 | } |
||
292 | if ($all_worker_stop) |
||
293 | { |
||
294 | if ($this->on_stop) |
||
295 | { |
||
296 | call_user_func($this->on_stop, $this); |
||
297 | } |
||
298 | exit(0); |
||
299 | } |
||
300 | } |
||
301 | // 其他信号 |
||
302 | else |
||
303 | { |
||
304 | // worker进程接受到master进行信号退出的,会到这里来 |
||
305 | if ($this->on_stop) |
||
306 | { |
||
307 | call_user_func($this->on_stop, $this); |
||
308 | } |
||
309 | exit(0); |
||
310 | } |
||
311 | } |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * 执行关闭流程(所有进程) |
||
316 | * 事件触发,非正常程序执行完毕 |
||
317 | * @return void |
||
318 | */ |
||
319 | public function stop_all() |
||
320 | { |
||
321 | // 设置master、worker进程的运行状态为关闭状态 |
||
322 | self::$_status = "shutdown"; |
||
323 | // master进程 |
||
324 | if(self::$_master_pid === posix_getpid()) |
||
325 | { |
||
326 | // 循环给worker进程发送关闭信号 |
||
327 | foreach (self::$_worker_pids as $worker_pid) |
||
328 | { |
||
329 | posix_kill($worker_pid, SIGINT); |
||
330 | } |
||
331 | } |
||
332 | // worker进程 |
||
333 | else |
||
334 | { |
||
335 | // 接收到master进程发送的关闭信号之后退出,这里应该考虑业务的完整性,不能强行exit |
||
336 | $this->stop(); |
||
337 | exit(0); |
||
338 | } |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * 停止当前worker实例 |
||
343 | * 正常运行结束和接受信号退出,都会调用这个方法 |
||
344 | * @return void |
||
345 | */ |
||
346 | public function stop() |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * 检查错误,PHP exit之前会执行 |
||
358 | * @return void |
||
359 | */ |
||
360 | public function check_errors() |
||
361 | { |
||
362 | // 如果当前worker进程不是正常退出 |
||
363 | if(self::$_status != "shutdown") |
||
364 | { |
||
365 | $error_msg = "WORKER EXIT UNEXPECTED "; |
||
366 | $errors = error_get_last(); |
||
367 | if($errors && ($errors['type'] === E_ERROR || |
||
368 | $errors['type'] === E_PARSE || |
||
369 | $errors['type'] === E_CORE_ERROR || |
||
370 | $errors['type'] === E_COMPILE_ERROR || |
||
371 | $errors['type'] === E_RECOVERABLE_ERROR )) |
||
372 | { |
||
373 | $error_msg .= $this->get_error_type($errors['type']) . " {$errors['message']} in {$errors['file']} on line {$errors['line']}"; |
||
374 | } |
||
375 | log::add($error_msg, 'Error'); |
||
376 | } |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * 获取错误类型对应的意义 |
||
381 | * @param integer $type |
||
382 | * @return string |
||
383 | */ |
||
384 | protected function get_error_type($type) |
||
420 | } |
||
421 | } |
||
422 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.