Swoole supports hot start

Source: Internet
Author: User
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

  • Related Article

    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.