public static fork ( Comos\Qpm\Process\Runnable | Callable $target ) : Comos\Qpm\Process\ChildProcess | ||
$target | Comos\Qpm\Process\Runnable | Callable | |
return | Comos\Qpm\Process\ChildProcess |
public function testStart_Start2ProcessWithOnePidFile() { $pidfile = $this->_pidFile; $process = \Comos\Qpm\Process\Process::fork(function () use($pidfile) { $man = new Manager($pidfile); $man->start(); usleep(200 * 1000); }); usleep(100 * 1000); $man1 = new Manager($pidfile); $process1 = $man1->getProcess(); $this->assertTrue($process1 instanceof \Comos\Qpm\Process\Process); try { $man2 = new Manager($this->_pidFile); $man2->start(); $this->fail('expects Exception'); } catch (\Exception $e) { $st = 0; $pidfile = pcntl_wait($st); $this->assertEquals($pidfile, $process->getPid()); $this->assertEquals($pidfile, $process1->getPid()); $this->assertTrue(\pcntl_wifexited($st)); $this->assertTrue($e instanceof \Comos\Qpm\Pid\Exception); $this->assertEquals('process exists, no need to start a new one', $e->getMessage()); } }
/** * @throws Comos\Qpm\Supervision\StopSignal */ protected function _startOne() { Logger::debug(__METHOD__ . '()'); $target = null; try { $target = \call_user_func($this->_config->getFactoryMethod()); } catch (StopSignal $ex) { Logger::debug('received stop signal'); throw $ex; } catch (\Exception $ex) { Logger::err($ex); \usleep(self::SLEEP_TIME_AFTER_ERROR); } if (!$target) { Logger::debug('fetched target is null. skipped'); return; } try { $process = Process::fork($target); $this->_children[$process->getPid()] = new ProcessStub($process, $this->_config); } catch (\Exception $ex) { Logger::err('exception', array('exception' => $ex)); \usleep(self::SLEEP_TIME_AFTER_ERROR); } }
public function testAllMethods() { parent::setUp(); $foo = function () { \usleep(150 * 1000); }; $child = Process\Process::fork($foo); $alive = $child->isAlive(); $this->assertTrue($alive); $this->assertNull($child->getStatus()->getTerminationSignal()); $this->assertNull($child->getStatus()->getExitCode()); $this->assertNull($child->getStatus()->getStopSignal()); $this->assertEquals(0, $child->getStatus()->getCode()); $this->assertFalse($child->getStatus()->isNormalExit()); $this->assertFalse($child->getStatus()->isSignaled()); $this->assertFalse($child->getStatus()->isStopped()); usleep(390 * 1000); $this->assertFalse($child->isAlive()); $this->assertFalse($child->getStatus()->isStopped()); $this->assertTrue($child->getStatus()->isNormalExit()); $this->assertEquals(0, $child->getStatus()->getTerminationSignal()); $this->assertEquals(0, $child->getStatus()->getStopSignal()); $this->assertFalse($child->getStatus()->isSignaled()); $this->assertEquals(0, $child->getStatus()->getExitCode()); }
public function testGetStatus() { $child = Process::fork(function () { usleep(100 * 1000); exit; }); $status = $child->getStatus(); $this->assertTrue($status instanceof \Comos\Qpm\Process\Status\ForkedChildStatus); $this->assertTrue($status instanceof \Comos\Qpm\Process\Status\NotExitStatus); $this->assertNull($status->getExitCode()); }
<?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);
/** * start a worker(child) Process. */ function startWorker() { Process::fork('worker'); }
protected function _startOne($groupId, Config $config) { $target = \call_user_func($config->getFactoryMethod()); $process = Process::fork($target); $this->_children[$process->getPid()] = new ProcessStub($process, $config, $groupId); }