argument('action'); $this->is_not_daemon = $this->option('not_daemon'); if ($action) { $this->$action(); } else { $this->start(); } } /** * 启动server */ private function start() { $pid = $this->getMasterPid(); if ($this->isRunning($pid)) { $this->warn('swoole server process is already running.'); return false; } $this->info('Starting swoole server...'); if ($this->is_not_daemon) { app('config')->set('swoole.daemonize', false); } (new Service())->run(); } private function restart() { $pid = $this->getMasterPid(); $this->stop(); sleep(1); if (!$this->isRunning($pid)) { $this->start(); } } /** * 柔性重启server * * @return bool */ private function reload() { // 柔性重启使用管理PID $pid = $this->getMasterPid(); if (!$this->isRunning($pid)) { $this->warn('no swoole server process running.'); return false; } $this->info('Reloading swoole server...'); Process::kill($pid, SIGUSR1); $this->info('> success'); } private function stop() { $pid = $this->getMasterPid(); if (!$this->isRunning($pid)) { $this->warn('no swoole server process running.'); return false; } $this->info('Stopping swoole server...'); Process::kill($pid, SIGTERM); $this->removePid(); $this->info('> success'); } /** * 获取主进程PID * @access protected * @return int */ private function getMasterPid() { $pidFile = config('swoole.pid_file'); if (is_file($pidFile)) { $masterPid = (int)file_get_contents($pidFile); } else { $masterPid = 0; } return $masterPid; } /** * 判断PID是否在运行 * @access protected * @param int $pid * @return bool */ protected function isRunning($pid) { if (empty($pid)) { return false; } return Process::kill($pid, 0); } /** * 删除PID文件 * @access protected * @return void */ protected function removePid() { $masterPid = config('swoole.pid_file'); if (is_file($masterPid)) { unlink($masterPid); } } }