Swoole understands the boot order of the manager process and worker processes, and demonstrates how to stop or restart the server.

Source: Internet
Author: User

Test the code main function: Open a TCP server. The management process and the callback of the worker process start are then set to rename. Set the Pid_file to save the mast process initiated by the server.

<?PHP//creating the server object, listening on the 127.0.0.1:9501 port$serv=NewSwoole_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"By this step, the service is up, and Manager,work has already recalled start.";});//Listening for connection entry events$serv->on (' Connect ',function($serv,$FD) {      Echo"Client:connect.\n";});//listen for data receive events$serv->on (' Receive ',function($serv,$FD,$from _id,$data) {    $serv->send ($FD, "Server:".$data);});//Listening for connection shutdown events$serv->on (' Close ',function($serv,$FD) {    Echo"Client:close.\n";});//Start the server$serv->start ();

After you turn on the server

View process

Then see if the mast process ID in Pid_file is 1827, as shown in. The results are clearly consistent.

Write a script below to restart and stop the server. (The principle is to send a signal to the mast process)

<?PHP$options= ' s '; $command= Getopt ($options); $pidFile= ' Server.pid '; if(isset($command[' s '])){        if($command[' s ']== ' stop ') {stop (); }Else{reload (); }    }Else{         die("Please enter-S stop|reload"); }    functionStop () {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); //wait 5 seconds            $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 file does not exist, please do find main process pid,kill!\n"; return false; }    }    functionReload () {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 file does not exist, please do find main process pid,kill!\n"; }    }

Perform a reboot first

View process again

The main process is still 1827, but the work process is reload.

Then execute the exit

The viewing process is really missing.

Swoole understands the boot order of the manager process and worker processes, and demonstrates how to stop or restart the server.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.