当前位置:网站首页>Talk about how to hot restart a spoole or PHP cli process

Talk about how to hot restart a spoole or PHP cli process

2022-06-25 23:47:00 ndrandy

Before discussing this topic , Need to know linux The signal of , stay linux The most common function used to initiate a signal in is kill 了 , Such as kill -SIGUSR1 pid、kill -9 pid 、kill -SIGTERM pid wait . These are soft interrupts

After the process receives these signals , Default processing behavior , That is to exit the process directly regardless of the three seven twenty-one ,   At this time, if the process is processing business data , So sorry , It's just gone . So how to deal with it ?

  The core step

  1. Register signal processing functions , To intercept the signal received by the listening process , Change the default processing behavior of the process
  2. In the business layer code , Make a status mark , When the status is exitable , direct exit

 

The following is used php Simulation Implementation

<?php

$running = true;

pcntl_signal(SIGTERM, function () use (&$running) {
    // received stop command ,  Mark the switch as exitable 
    $running = false;
    // There is no direct exit
});

while ($running){
    // Here the business data logic is handled 
    //do step.1
    //do step.2
    //do step.3
    //do step.4
    //do step.5
    //do step.6
}

/**
 *  When the process receives SIGTERM Signal time ( namely :stop), The code logic may run to step.1 ~ step.6 Anywhere in between . received   Letter  
 *  Change immediately after the th $running= false, At this time, it is not direct exit, Guarantee this round while Finish the cycle , To the next round while When 
 * $running= false,while Loop exit ,  The process exits safely 
*/

 

原网站

版权声明
本文为[ndrandy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252056449659.html