From 37faae08c96030bd0cd9d214bf6bf66d2c8fba38 Mon Sep 17 00:00:00 2001 From: lixiaopei Date: Mon, 11 Jun 2018 09:53:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=90=AF=E5=8A=A8=E5=91=BD?= =?UTF-8?q?=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Process.php | 22 +++++++++++++++------- run.php => swoole-bot.php | 0 2 files changed, 15 insertions(+), 7 deletions(-) rename run.php => swoole-bot.php (100%) diff --git a/src/Process.php b/src/Process.php index 92d577a..c73f143 100644 --- a/src/Process.php +++ b/src/Process.php @@ -27,21 +27,29 @@ public function __construct($config) public function start() { \Swoole\Process::daemon(true, true); - $config = $this->config; + $config = $this->config; isset($config['swoole']['workNum']) && $this->workNum=$config['swoole']['workNum']; //设置主进程 $ppid = getmypid(); $pid_file = $this->config['path'] . self::PID_FILE; if (file_exists($pid_file)) { - echo "已有进程运行中,请先结束或重启\n"; - die(); + $pid=file_get_contents($pid_file); + if ($pid) { + //尝试三次确定是否进程还存在,存在就退出 + for ($i=0; $i < 3; ++$i) { + if (@\Swoole\Process::kill($pid, 0)) { + die('已有进程运行中,请先结束或重启' . PHP_EOL); + } + sleep(1); + } + } } file_put_contents($pid_file, $ppid); $this->setProcessName('job master ' . $ppid . self::PROCESS_NAME_LOG); //根据配置信息,开启多个进程 - for ($i = 0; $i < $this->workNum; $i++) { + for ($i = 0; $i < $this->workNum; ++$i) { $this->reserveBot($i); sleep(2); } @@ -63,11 +71,11 @@ public function reserveBot($workNum) echo $e->getMessage(); } - echo 'reserve process ' . $workNum . " is working ...\n"; + echo 'reserve process ' . $workNum . ' is working ...' . PHP_EOL; }); $pid = $reserveProcess->start(); $this->workers[$pid] = $reserveProcess; - echo "reserve start...\n"; + echo 'reserve start...' . PHP_EOL; } //监控子进程 @@ -97,7 +105,7 @@ public function registSignal(&$workers) private function setExit() { @unlink($this->config['path'] . self::PID_FILE); - $this->logger->log('Time: ' . microtime(true) . '主进程退出' . "\n"); + $this->logger->log('Time: ' . microtime(true) . '主进程退出' . '' . PHP_EOL); foreach ($this->workers as $pid => $worker) { //平滑退出,用exit;强制退出用kill \Swoole\Process::kill($pid); diff --git a/run.php b/swoole-bot.php similarity index 100% rename from run.php rename to swoole-bot.php