Swoole 明白manager进程和worker进程的启航顺序,以及演示怎么样停止可能重启服务端。

 测试的代码首要功能:开启三个tcp服务器。然后设置了管制进度和做事经过start的回调举行更名。设置了pid_file保存了服务端运转的mast进度。

详解

./nginx  #打开 nginx
nginx -s reload|reopen|stop|quit  #重新加载配置|重启|停止|退出 nginx
nginx -t   #测试配置是否有语法错误

nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

-?,-h           : 打开帮助信息
-v              : 显示版本信息并退出
-V              : 显示版本和配置选项信息,然后退出
-t              : 检测配置文件是否有语法错误,然后退出
-q              : 在检测配置文件期间屏蔽非错误信息
-s signal       : 给一个 nginx 主进程发送信号:stop(停止), quit(退出), reopen(重启), reload(重新加载配置文件)
-p prefix       : 设置前缀路径(默认是:/usr/local/nginx/)
-c filename     : 设置配置文件(默认是:/usr/local/nginx/conf/nginx.conf)
-g directives   : 设置配置文件外的全局指令

 

-s signal      Send a signal to the master process.  The argument signal can be one of: stop, quit, reopen, reload.
                    The following table shows the corresponding system signals:

                    stop    SIGTERM
                    quit    SIGQUIT
                    reopen  SIGUSR1
                    reload  SIGHUP

SIGNALS
     The master process of nginx can handle the following signals:

     SIGINT, SIGTERM  Shut down quickly.
     SIGHUP           Reload configuration, start the new worker process with a new configuration, and gracefully shut
                      down old worker processes.
     SIGQUIT          Shut down gracefully.
     SIGUSR1          Reopen log files.
     SIGUSR2          Upgrade the nginx executable on the fly.
     SIGWINCH         Shut down worker processes gracefully.

 

<?php
//创建Server对象,监听 127.0.0.1:9501端口
$serv = new swoole_server("127.0.0.1", 9501);
$serv->set(array(
    'max_request ' => 10, //reactor thread num
    'worker_num' => 4,    //worker process num
    'log_file' => 'swoole.log',
    'pid_file' => 'server.pid',
));



$serv->on('managerStart',function($serv){
    swoole_set_process_name("managerprocess");
});

$serv->on('workerStart',function($serv, $worker_id){
    if($worker_id >= $serv->setting['worker_num']) {
        swoole_set_process_name("workprocess_".($worker_id-$serv->setting['worker_num']));
    } else {
        swoole_set_process_name("workprocess_{$worker_id}");
    }
});
$serv->on('start',function($serv){
    echo "到这一步说明服务已经起来了,manager,work都已经回调start完成";
});
//监听连接进入事件
$serv->on('connect', function ($serv, $fd) {  
    echo "Client: Connect.\n";
});

//监听数据接收事件
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
    $serv->send($fd, "Server: ".$data);
});

//监听连接关闭事件
$serv->on('close', function ($serv, $fd) {
    echo "Client: Close.\n";
});

//启动服务器
$serv->start(); 

停止

翻看进度号:

$ ps -ef|grep nginx
root      5747     1  0 May23 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
500      12037  7886  0 10:00 pts/1    00:00:00 grep nginx
nobody   25581  5747  0 Sep27 ?        00:01:16 nginx: worker process      
nobody   25582  5747  0 Sep27 ?        00:01:25 nginx: worker process      
nobody   25583  5747  0 Sep27 ?        00:02:59 nginx: worker process      
nobody   25584  5747  0 Sep27 ?        00:02:05 nginx: worker process

杀掉进度:

$ kill -9 5747

开启服务器后

重启

1. 证实nginx配置文件是或不是正确

进去nginx安装目录sbin下,输入指令:

$ ./nginx -t
…… nginx.conf syntax is ok
…… nginx.conf test is successful

看样子上述展现,表明配置文件正确!

2. 重启nginx服务

进入nginx安装目录sbin下,输入指令:

# ./nginx -s reload
或者
# kill -HUP 5747

 

图片 1

翻开进度,在server->start 后,拉起workNum+2(master,manager进程)。

图片 2

下一场查看pid_file里的master进度id是不是是1827,上海教室所示。结果肯定是相同的

图片 3

 

上面写二个脚本去重启和平息服务端。(原理正是给mast进度发送信号)

<?php
    $options  = 's';
    $command = getopt($options);
    $pidFile = 'server.pid';
    if(isset($command['s'])){
        if($command['s']=='stop'){
            stop();
        }else{
            reload();
        }
    }else{
        die("请输入-s stop|reload");
    }

    function stop(){
        global $pidFile;
        if (file_exists($pidFile)) {
            $pid = file_get_contents($pidFile);

            if (!swoole_process::kill($pid, 0)) {
                echo "PID :{$pid} not exist \n";
                return false;
            }
            swoole_process::kill($pid);
            //等待5秒
            $time = time();
            $flag = false;
            while (true) {
                usleep(1000);
                if (!swoole_process::kill($pid, 0)) {
                    echo "server stop at " . date("y-m-d h:i:s") . "\n";
                    if (is_file($pidFile)) {
                        unlink($pidFile);
                    }
                    $flag = true;
                    break;
                } else {
                    if (time() - $time > 5) {
                        echo "stop server fail.try again \n";
                        break;
                    }
                }
            }
            return $flag;
        } else {
            echo "pid 文件不存在,请执行查找主进程pid,kill!\n";
            return false;
        }
    }

    function reload(){
        global $pidFile;
        if (file_exists($pidFile)) {
            $sig = SIGUSR1;
            $pid = file_get_contents($pidFile);
            if (!swoole_process::kill($pid, 0)) {
                echo "pid :{$pid} not exist \n";
                return;
            }
            swoole_process::kill($pid, $sig);
            echo "send server reload command at " . date("y-m-d h:i:s") . "\n";
        } else {
            echo "pid 文件不存在,请执行查找主进程pid,kill!\n";
        }
    }

先实施重启

图片 4

再查看进程

图片 5

主进度照旧1827,不过工作经过就被reload的了。

接下来实施退出

图片 6

查看进度确实丢失了

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图