The so-called hot restart, that is, when the server related code changes, no need to stop the service, but the server is still running in the state to update the file. Swoole This function with the built-in reload function and two custom semaphores. Let me first explain the three signals available to Swoole: SIGTERM,SIGUSR1,SIGUSR2. Sigterm is used to stop the server, SIGUSR1 is used to restart all worker processes, SIGUSR2 is used to restart all the task worker processes. How do you implement the hot update code file? The callback function in Swoole has this callback Onworkerstart, which is called when the worker process starts. Therefore, Onworkerstart is called when Swoole_server receives the SIGUSR1 signal and restarts all worker processes. If you require all the code files in the Onworkerstart, the PHP files will be require again after each onworkerstart, so that the hot update of the code files can be implemented. Look at the code implementation:
Public Function OnStart ($serv) { cli_set_process_title ("Reload_master");} Public Function Onworkerstart ($serv, $worker _id) { require_once "reload_page.php"; Test (); A function defined in the reload_page.php}
First, the process name is set by the PHP cli_set_process_title function in the OnStart callback function. In Onworkerstart, require related PHP files. Then, create a new reload.sh file and enter the following:
echo "Reloading ..." cmd=$ (pidof reload_master) kill-usr1 "$cmd" echo "Reloaded"
This way, you can restart the server by executing this script. Click here to view the full source