Skip to content

Commit 37faae0

Browse files
author
lixiaopei
committed
优化启动命令
1 parent 2e78c46 commit 37faae0

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/Process.php

+15-7
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,29 @@ public function __construct($config)
2727
public function start()
2828
{
2929
\Swoole\Process::daemon(true, true);
30-
$config = $this->config;
30+
$config = $this->config;
3131
isset($config['swoole']['workNum']) && $this->workNum=$config['swoole']['workNum'];
3232

3333
//设置主进程
3434
$ppid = getmypid();
3535
$pid_file = $this->config['path'] . self::PID_FILE;
3636
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+
}
3947
}
4048
file_put_contents($pid_file, $ppid);
4149
$this->setProcessName('job master ' . $ppid . self::PROCESS_NAME_LOG);
4250

4351
//根据配置信息,开启多个进程
44-
for ($i = 0; $i < $this->workNum; $i++) {
52+
for ($i = 0; $i < $this->workNum; ++$i) {
4553
$this->reserveBot($i);
4654
sleep(2);
4755
}
@@ -63,11 +71,11 @@ public function reserveBot($workNum)
6371
echo $e->getMessage();
6472
}
6573

66-
echo 'reserve process ' . $workNum . " is working ...\n";
74+
echo 'reserve process ' . $workNum . ' is working ...' . PHP_EOL;
6775
});
6876
$pid = $reserveProcess->start();
6977
$this->workers[$pid] = $reserveProcess;
70-
echo "reserve start...\n";
78+
echo 'reserve start...' . PHP_EOL;
7179
}
7280

7381
//监控子进程
@@ -97,7 +105,7 @@ public function registSignal(&$workers)
97105
private function setExit()
98106
{
99107
@unlink($this->config['path'] . self::PID_FILE);
100-
$this->logger->log('Time: ' . microtime(true) . '主进程退出' . "\n");
108+
$this->logger->log('Time: ' . microtime(true) . '主进程退出' . '' . PHP_EOL);
101109
foreach ($this->workers as $pid => $worker) {
102110
//平滑退出,用exit;强制退出用kill
103111
\Swoole\Process::kill($pid);

run.php swoole-bot.php

File renamed without changes.

0 commit comments

Comments
 (0)