public function setUp()
 {
     $this->port = !empty($GLOBALS['port']) ? (int) $GLOBALS['port'] : 8080;
     $this->loop = Factory::create();
     $this->server = new Server($this->loop, $this->port, $this->path);
     $loop = $this->loop;
     $this->loop->addPeriodicTimer(10, function () use($loop) {
         $loop->stop();
     });
 }
Beispiel #2
0
 /**
  * Starts the main loop. Blocks.
  */
 public function run()
 {
     Debug::enable();
     gc_disable();
     //necessary, since connections will be dropped without reasons after several hundred connections.
     $this->loop = \React\EventLoop\Factory::create();
     $this->controller = new \React\Socket\Server($this->loop);
     $this->controller->on('connection', array($this, 'onSlaveConnection'));
     $this->controller->listen(5500);
     $this->web = new \React\Socket\Server($this->loop);
     $this->web->on('connection', array($this, 'onWeb'));
     $this->web->listen($this->port, $this->host);
     $this->tcpConnector = new \React\SocketClient\TcpConnector($this->loop);
     $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop);
     $pcntl->on(SIGTERM, [$this, 'shutdown']);
     $pcntl->on(SIGINT, [$this, 'shutdown']);
     if ($this->isDebug()) {
         $this->loop->addPeriodicTimer(0.5, function () {
             $this->checkChangedFiles();
         });
     }
     $this->isRunning = true;
     $loopClass = (new \ReflectionClass($this->loop))->getShortName();
     $this->output->writeln("<info>Starting PHP-PM with {$this->slaveCount} workers, using {$loopClass} ...</info>");
     for ($i = 0; $i < $this->slaveCount; $i++) {
         $this->newInstance(5501 + $i);
     }
     $this->loop->run();
 }
Beispiel #3
0
 /**
  * Starts the main loop. Blocks.
  */
 public function run()
 {
     Debug::enable();
     //make whatever is necessary to disable all stuff that could buffer output
     ini_set('zlib.output_compression', 0);
     ini_set('output_buffering', 0);
     ini_set('implicit_flush', 1);
     ob_implicit_flush(1);
     $this->loop = \React\EventLoop\Factory::create();
     $this->controller = new React\Server($this->loop);
     $this->controller->on('connection', array($this, 'onSlaveConnection'));
     $this->controllerHost = $this->getNewControllerHost();
     $this->controller->listen(5500, $this->controllerHost);
     $this->web = new \React\Socket\Server($this->loop);
     $this->web->on('connection', array($this, 'onWeb'));
     $this->web->listen($this->port, $this->host);
     $this->tcpConnector = new \React\SocketClient\TcpConnector($this->loop);
     $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop);
     $pcntl->on(SIGTERM, [$this, 'shutdown']);
     $pcntl->on(SIGINT, [$this, 'shutdown']);
     $pcntl->on(SIGCHLD, [$this, 'handleSigchld']);
     $pcntl->on(SIGUSR1, [$this, 'restartWorker']);
     if ($this->isDebug()) {
         $this->loop->addPeriodicTimer(0.5, function () {
             $this->checkChangedFiles();
         });
     }
     $this->isRunning = true;
     $loopClass = (new \ReflectionClass($this->loop))->getShortName();
     $this->output->writeln("<info>Starting PHP-PM with {$this->slaveCount} workers, using {$loopClass} ...</info>");
     for ($i = 0; $i < $this->slaveCount; $i++) {
         $this->newInstance(5501 + $i);
     }
     $this->loop->run();
 }
Beispiel #4
0
 private function setTimer()
 {
     $this->loop->addPeriodicTimer(1, function () {
         if ($this->getBridge() instanceof Bridges\TimerBridgeInterface) {
             $this->getBridge()->timer($this);
         }
     });
 }
 /**
  * {@inheritDoc}
  * @see \Symfony\Component\Console\Command\Command::execute()
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // Start listenning
     $this->socket->listen($this->port, $this->host);
     // Periodically call determining if we should stop or not
     $this->loop->addPeriodicTimer($input->getOption('check-interval'), function () use($output) {
         if ($this->shouldExitCommand($output)) {
             $this->loop->stop();
             $this->writeln($output, 'Event loop stopped:' . $this->port);
             $this->returnValue = 10;
         }
     });
     // Main loop
     $this->writeln($output, 'Starting event loop:' . $this->port);
     $this->loop->run();
     return $this->returnValue;
 }
Beispiel #6
0
 public function onDispatch2(MvcEvent $e)
 {
     $this->socket->on('connection', function (ConnectionInterface $conn) {
         $conn->on('data', function ($dataRaw) use($conn) {
             $dataRaw = trim($dataRaw);
             $data = Json::decode($dataRaw, Json::TYPE_ARRAY);
             if ($data['queueName'] == 'checkEngine') {
                 return;
             }
             if (!isset($this->config[$data['queueName']])) {
                 $this->debug("Bad queue name: " . $data['queueName'], ['debug-enable' => 1]);
                 return;
             }
             $queueName = $data['queueName'];
             $this->checkWorkersCount($queueName);
             $process = $this->handleData($data, $dataRaw, $this->getQueueConfig($queueName), $conn);
             if (!$process) {
                 return;
             }
             $this->processes[$queueName][] = $process;
         });
     });
     $this->loop->addPeriodicTimer(1, function ($timer) {
         $processes = [];
         foreach ($this->processes as $queueName => $queue) {
             /** @var Process $process */
             foreach ($queue as $process) {
                 if ($process->isRunning()) {
                     $processes[$queueName][] = $process;
                 } else {
                     $this->printProcessOutput($queueName, $process);
                 }
             }
         }
         $this->processes = $processes;
     });
     $this->socket->listen(4000);
     $this->loop->run();
 }
Beispiel #7
0
 /**
  * Registers a periodically triggered status event.
  */
 private function registerTimedEvents()
 {
     $this->reactLoop->addPeriodicTimer($this->config->getStatusLoopInterval(), function () {
         $memoryUsageMb = memory_get_usage(true) / 1024 / 1024;
         $this->runtimeStatistics->setMemoryUsageMb($memoryUsageMb);
         if ($memoryUsageMb > $this->config->getMemoryLimitMbWarn()) {
             $this->logger->warning("MemoryUsage:   {$memoryUsageMb} MB.");
         } else {
             if ($memoryUsageMb > $this->config->getMemoryLimitMbInfo()) {
                 $this->logger->info("MemoryUsage:   {$memoryUsageMb} MB.");
             }
         }
         $memoryPeakUsageMb = memory_get_peak_usage(true) / 1024 / 1024;
         $this->runtimeStatistics->setMemoryPeakUsageMb($memoryPeakUsageMb);
         if ($memoryPeakUsageMb > $this->config->getMemoryPeakLimitMbWarn()) {
             $this->logger->warning("MemoryPeakUsage " . memory_get_peak_usage(true) / 1024 / 1024 . " MB.");
         }
         $rateObjects = $this->runtimeStatistics->getAddedObjectRate($this->config->getStatusLoopInterval());
         $rateEvictions = $this->runtimeStatistics->getEvictionRate($this->config->getStatusLoopInterval());
         $this->logger->info("Added objects: {$this->runtimeStatistics->getAddedObjectCount()}, evictions: {$this->runtimeStatistics->getEvictedObjectCount()} ({$rateObjects} Obj/Sec, {$rateEvictions} Evi/Sec).");
         $this->runtimeStatistics->tick();
     });
 }