Known PHP is a dynamic state language, at every time of processing requests are temporary code to compile the execution so for this kind of language is not need to restart when online, directly with the method of documents covering or replacement can also adopt the way of soft chain, because this makes its performance is often reviled, So there’s a process management tool called php-fpm that works with it, and now php-FPM is standard with PHP (apache doesn’t use it), but php-FPM is written in C, which is a static language. So if you need to restart the php-fPM configuration (although this is very rare), how does it work when you need to restart? So let’s talk about it.

There are generally two issues to consider during a smooth restart: on the one hand, how to handle pending requests and on the other hand, how to handle processes that take too long. Php-fpm provides its own solutions to both of these aspects, which are interdependent rather than independent.

Note: Kill -usr2 PID This PID can be either the PID of the master process or the PID of the worker process. If it is the PID of the master process, it will restart all worker processes. If it is the PID of the worker process, it is only restarted as a worker process.

How to handle pending requests:

After sending the restart signal, the master will be notified. The master will get the event and send the SIGQUIT signal to the worker process. After receiving the signal, the worker process will hand it to the corresponding signal processing function for processing. The worker process will exit when it calls the fcGI_accept_request process for the next process. If the process is not finished, it will exit. This will cause the code logic to run half way, so pay attention to this. Php-fpm can only handle one request at a time, so there is no need for counters, which makes it simpler.

If the processing time is too long, how to handle it:

When the signal is sent to the PHP-FPM process, the phP-FPM process will restart the operation process. In the whole restart process, a timeout mechanism is added to ensure a smooth restart. If the time exceeds the set time, the process will be forcibly restarted and interrupted. This configuration is set in the phP-Fpm.ini file, where process_Control_TIMEOUT =10 is used, indicating a maximum of 10 seconds.

  Example:    

Code test, restart causes the code execution part to exit when the code logic takes 11 seconds with a 10-second timeout set.

    public function actionTest() {$model = new bad(); 
      $model->save(array('id'= > 75329370)); // It is important to note that the sleep method is not used to wait for the time, since it is called by the system sleep method, it will be disabled on restart$model->db()->query('select sleep(11) from orders where id=75329370');                                                                                                            
       $model2 = new lock();
        $model2->save(array('id'= > 10251,'info'= > 1212));return; 
   }Copy the code