Explorar el Código

Enable for graceful restarts on systems with pcntl_signal enabled.

If the process recieves a SIGTERM or SIGHUP it will restart after current job is done or timeout is received.
Daum hace 9 años
padre
commit
cc857557f2
Se han modificado 1 ficheros con 30 adiciones y 1 borrados
  1. 30 1
      Service/GearmanExecute.php

+ 30 - 1
Service/GearmanExecute.php

@@ -60,6 +60,13 @@ class GearmanExecute extends AbstractGearmanService
      */
     protected $executeOptionsResolver;
 
+    /**
+     * Boolean to track if a system signal has been received
+     * @var boolean
+     */
+    protected $stopWorkSignalReceived;
+
+
     /**
      * Construct method
      *
@@ -83,6 +90,28 @@ class GearmanExecute extends AbstractGearmanService
                 'timeout'                => array('null', 'scalar'),
             ))
         ;
+
+        $this->stopWorkSignalReceived = false;
+
+        /**
+         * If the pcntl_signal exists, subscribe to the terminate and restart events for graceful worker stops.
+         */
+        if(false !== function_exists('pcntl_signal'))
+        {
+            declare(ticks = 1);
+            pcntl_signal(SIGTERM, array($this,"handleSystemSignal"));
+            pcntl_signal(SIGHUP,  array($this,"handleSystemSignal"));
+
+        }
+    }
+
+    /**
+     * Toggles that work should be stopped, we only subscribe to SIGTERM and SIGHUP
+     * @param int $signno Signal number
+     */
+    public function handleSystemSignal($signo)
+    {
+        $this->stopWorkSignalReceived = true;
     }
 
     /**
@@ -299,7 +328,7 @@ class GearmanExecute extends AbstractGearmanService
         /**
          * Executes GearmanWorker with all jobs defined
          */
-        while ($gearmanWorker->work()) {
+        while (false === $this->stopWorkSignalReceived && $gearmanWorker->work()) {
 
             $iterations--;