| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 | <?phpnamespace App\Console\Commands;use App\Http\Seller\Services\Chat\Service;use Illuminate\Console\Command;use Swoole\Process;class Swoole extends Command{    /**     * The name and signature of the console command.     *     * @var string     */    protected $signature = 'swoole:serve {action? : "命令:start|stop|restart|reload"} {--not|not_daemon : 进程不进入后台执行}';    protected $description = '启动客服服务';    protected $is_not_daemon = false;    /**     * Create a new command instance.     *     * @return void     */    public function __construct()    {        parent::__construct();    }    /**     * Execute the console command.     *     * @return mixed     */    public function handle()    {        $action = $this->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('<error>swoole server process is already running.</error>');            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);        }    }}
 |