protected function _doTestStartAll($baseConfig, $useFactoryMethodToCreateKeeper = false)
 {
     $config = $baseConfig;
     $config['quantity'] = $quantity = 3;
     if (!$useFactoryMethodToCreateKeeper) {
         $keeper = new OneForOneKeeper(array(new Config($config)));
     } else {
         $keeper = \Comos\Qpm\Supervision\Supervisor::oneForOne($config)->getKeeper();
     }
     $keeper->startAll();
     $pids = array();
     for ($i = 0; $i < $quantity; $i++) {
         $status = 0;
         $pids[\pcntl_wait($status)] = true;
     }
     $currentPid = \posix_getpid();
     $r = \file_get_contents($this->_logFile);
     $lines = explode("\n", $r);
     $count = 0;
     foreach ($lines as $line) {
         if (trim($line)) {
             list($pid, $ppid) = explode(',', $line);
             $count++;
             $this->assertTrue($pids[$pid]);
             $this->assertEquals($currentPid, $ppid);
         }
     }
     $this->assertEquals($quantity, $count);
 }
Exemple #2
0
 public function testMultiGroupOneForOne_CreateKeeper()
 {
     Supervisor::multiGroupOneForOne(array(array('worker' => function () {
         exit;
     }), array('worker' => function () {
         exit;
     }), array('worker' => function () {
         exit;
     }, 'quantity' => 3, 'maxRestartTimes' => 3)));
 }
 public function testRun_WithTimeout_WithOnTimeout()
 {
     Supervisor::taskFactoryMode(array('quantity' => 3, 'timeout' => 1, 'onTimeout' => array($this, 'onTimeout'), 'factory' => array($this, 'fetchTask_WithSleeping')))->start();
     $content = \file_get_contents($this->logFile);
     $arr = array_filter(explode(',', $content), function ($i) {
         return $i !== '';
     });
     sort($arr);
     $this->assertEquals(array('1', '4', '5', '7', '10', '13', '14'), $arr);
     $this->assertEquals(3, \count($this->timeoutProcesses));
 }
 public function testOneForOne_WithTimeout_WithOnTimeout()
 {
     $conf = array('worker' => array($this, 'worker'), 'quantity' => 2, 'timeout' => 1, 'maxRestartTimes' => 10, 'withIn' => 30, 'onTimeout' => array($this, 'onTimeout'));
     try {
         Supervisor::oneForOne($conf)->start();
         $this->fail('expects OutOfPolicyException');
     } catch (\Exception $ex) {
         $this->assertInstanceOf('\\Comos\\Qpm\\Supervision\\OutOfPolicyException', $ex);
     }
     $data = \file_get_contents($this->logFile);
     $ms = null;
     $this->assertEquals(1, preg_match('/^b{10,12}$/', $data, $ms));
     $this->assertEquals(12, $this->onTimeoutCount);
 }
<?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();
<?php

namespace Comos\Qpm\Supervision;

require \dirname(__DIR__) . '/bootstrap.inc.php';
use Comos\Qpm\Process\Process;
$file = $argv[1];
$file1 = $argv[2];
$configs = array(array('worker' => function () use($file) {
    \file_put_contents($file, 1, \FILE_APPEND);
    \usleep(1000 * 1000);
}), array('worker' => function () use($file1) {
    \file_put_contents($file1, '2', \FILE_APPEND);
    \usleep(1000 * 500);
}, 'quantity' => 2));
$supProcessCallback = function () use($configs) {
    $supervisor = Supervisor::multiGroupOneForOne($configs);
    $supervisor->start();
    $supervisor->registerSignalHandler();
};
$supProcess = Process::fork($supProcessCallback);
usleep(5000 * 1000);
$supProcess->terminate();
usleep(5000 * 1000);
<?php

use Comos\Qpm\Supervision\Supervisor;
require __DIR__ . '/bootstrap.inc.php';
Supervisor::oneForOne(array('timeout' => 5, 'termTimeout' => 3, 'worker' => function () {
    declare (ticks=1);
    $GLOBALS['stop'] = 0;
    pcntl_signal(SIGTERM, function () {
        echo "TERM\n";
        $GLOBALS['stop'] = 1;
    });
    while (!$GLOBALS['stop']) {
        sleep(1);
        echo '.';
    }
    echo "BYE\n";
}))->start();