@@ -27,21 +27,29 @@ public function __construct($config)
27
27
public function start ()
28
28
{
29
29
\Swoole \Process::daemon (true , true );
30
- $ config = $ this ->config ;
30
+ $ config = $ this ->config ;
31
31
isset ($ config ['swoole ' ]['workNum ' ]) && $ this ->workNum =$ config ['swoole ' ]['workNum ' ];
32
32
33
33
//设置主进程
34
34
$ ppid = getmypid ();
35
35
$ pid_file = $ this ->config ['path ' ] . self ::PID_FILE ;
36
36
if (file_exists ($ pid_file )) {
37
- echo "已有进程运行中,请先结束或重启 \n" ;
38
- die ();
37
+ $ pid =file_get_contents ($ pid_file );
38
+ if ($ pid ) {
39
+ //尝试三次确定是否进程还存在,存在就退出
40
+ for ($ i =0 ; $ i < 3 ; ++$ i ) {
41
+ if (@\Swoole \Process::kill ($ pid , 0 )) {
42
+ die ('已有进程运行中,请先结束或重启 ' . PHP_EOL );
43
+ }
44
+ sleep (1 );
45
+ }
46
+ }
39
47
}
40
48
file_put_contents ($ pid_file , $ ppid );
41
49
$ this ->setProcessName ('job master ' . $ ppid . self ::PROCESS_NAME_LOG );
42
50
43
51
//根据配置信息,开启多个进程
44
- for ($ i = 0 ; $ i < $ this ->workNum ; $ i ++ ) {
52
+ for ($ i = 0 ; $ i < $ this ->workNum ; ++ $ i ) {
45
53
$ this ->reserveBot ($ i );
46
54
sleep (2 );
47
55
}
@@ -63,11 +71,11 @@ public function reserveBot($workNum)
63
71
echo $ e ->getMessage ();
64
72
}
65
73
66
- echo 'reserve process ' . $ workNum . " is working ... \n" ;
74
+ echo 'reserve process ' . $ workNum . ' is working ... ' . PHP_EOL ;
67
75
});
68
76
$ pid = $ reserveProcess ->start ();
69
77
$ this ->workers [$ pid ] = $ reserveProcess ;
70
- echo " reserve start... \n" ;
78
+ echo ' reserve start... ' . PHP_EOL ;
71
79
}
72
80
73
81
//监控子进程
@@ -97,7 +105,7 @@ public function registSignal(&$workers)
97
105
private function setExit ()
98
106
{
99
107
@unlink ($ this ->config ['path ' ] . self ::PID_FILE );
100
- $ this ->logger ->log ('Time: ' . microtime (true ) . '主进程退出 ' . "\n" );
108
+ $ this ->logger ->log ('Time: ' . microtime (true ) . '主进程退出 ' . '' . PHP_EOL );
101
109
foreach ($ this ->workers as $ pid => $ worker ) {
102
110
//平滑退出,用exit;强制退出用kill
103
111
\Swoole \Process::kill ($ pid );
0 commit comments