<?php

use Comos\Qpm\Supervision\Supervisor;
use Comos\Qpm\Process\Runnable;
use Comos\Qpm\Log\Logger;
require __DIR__ . '/bootstrap.inc.php';
Logger::useSimpleLogger(__FILE__ . '.log');
class Task implements Runnable
{
    private $stop = false;
    public function run()
    {
        declare (ticks=1);
        pcntl_signal(SIGTERM, array($this, 'onTerm'));
        echo "B";
        while (!$this->stop) {
            sleep(1);
            echo '.';
        }
        echo "BYE\n";
    }
    public function onTerm()
    {
        echo "TERM\n";
        $this->stop = true;
    }
}
Supervisor::taskFactoryMode(array('timeout' => 5, 'termTimeout' => 1, 'quantity' => 3, 'worker' => 'Task'))->start();
Beispiel #2
0
 /**
  * to fork to create a process and run $target in there
  *
  * @param
  *            Runnable | \callable $target
  * @return ChildProcess
  * @throws \InvalidArgumentException
  */
 public static function fork($target)
 {
     if (!\is_callable($target) && !$target instanceof Runnable) {
         throw new \InvalidArgumentException('$target must be a valid callback or Comos\\Qpm\\Process\\Runnable');
     }
     $pid = \pcntl_fork();
     if ($pid == -1) {
         throw new FailToForkException('fail to folk.');
     }
     if ($pid == 0) {
         try {
             if ($target instanceof Runnable) {
                 $code = $target->run();
             } else {
                 $code = \call_user_func($target);
             }
         } catch (\Exception $ex) {
             Logger::err($ex);
             $code = -1;
         }
         if (\is_null($code)) {
             $code = 0;
         } elseif (!\is_int($code)) {
             $code = 1;
         }
         exit($code);
     }
     return new ChildProcess($pid, self::getCurrentPid());
 }
Beispiel #3
0
 /**
  * 
  * @return boolean
  */
 private function invokeOnTimeout()
 {
     $onTimeout = $this->getConfig()->getOnTimeout();
     if (!$onTimeout) {
         return false;
     }
     try {
         \call_user_func($onTimeout, $this->getProcess());
     } catch (\Exception $e) {
         \Comos\Qpm\Log\Logger::err($e);
         return false;
     }
     return true;
 }
Beispiel #4
0
 protected function tearDown()
 {
     Logger::useNullLogger();
     @unlink($this->_logFile);
     parent::tearDown();
 }
Beispiel #5
0
 public function stop()
 {
     Logger::debug(__CLASS__ . '::' . __METHOD__ . '()');
     if (!$this->_currentProcess->isCurrent()) {
         return;
     }
     $this->_stoped = true;
     foreach ($this->_children as $child) {
         try {
             $child->getProcess()->kill();
         } catch (Exception $ex) {
             Logger::err($ex);
         }
     }
     $this->_waitToEnd();
 }
Beispiel #6
0
 public function stop()
 {
     Logger::debug(__METHOD__ . ' before-stop');
     $this->_keeper->stop();
     Logger::debug(__METHOD__ . ' after-stop');
 }
Beispiel #7
0
 public function stop()
 {
     if (!$this->_currentProcess->isCurrent()) {
         return;
     }
     $this->_stoped = true;
     foreach ($this->_children as $stub) {
         try {
             $stub->getProcess()->kill();
         } catch (\Exception $ex) {
             Logger::err('fail to kill process', array('exception' => $ex));
         }
     }
     while (count($this->_children)) {
         $status = 0;
         $pid = \pcntl_wait($status);
         unset($this->_children[$pid]);
     }
 }