/** * Handles termination signals, so we can gracefully stop all servers. */ public function shutdown() { if ($this->inShutdown) { return; } $this->inShutdown = true; //this method is also called during startup when something crashed, so //make sure we don't operate on nulls. $this->output->writeln('<error>Termination received, exiting.</error>'); if ($this->controller) { @$this->controller->shutdown(); } if ($this->web) { @$this->web->shutdown(); } if ($this->loop) { $this->loop->tick(); $this->loop->stop(); } foreach ($this->slaves as $slave) { if (is_resource($slave['process'])) { proc_terminate($slave['process']); } if ($slave['pid']) { //make sure its dead posix_kill($slave['pid'], SIGKILL); } } exit; }
public function testDealerRep() { $pids[] = $this->forkRepWorker(); $pids[] = $this->forkRepWorker(); $loop = new StreamSelectLoop(); $context = new Context($loop); $dealer = $context->getSocket(\ZMQ::SOCKET_DEALER); $dealer->bind('ipc://test2.ipc'); sleep(1); $msgs = array(); $dealer->on('message', function ($msg) use(&$msgs) { $msgs[] = $msg; }); $dealer->send(array('A', '', 'foo')); $dealer->send(array('B', '', 'bar')); $loop->addTimer(1, function () use($loop) { $loop->stop(); }); $loop->run(); foreach ($pids as $pid) { pcntl_waitpid($pid, $status, WUNTRACED); } $this->assertCount(2, $msgs); $this->assertContains(array('A', '', 'foobar'), $msgs); $this->assertContains(array('B', '', 'barbar'), $msgs); }
public function bye() { if ($this->connection->isWritable()) { $this->connection->write(json_encode(array('cmd' => 'unregister', 'pid' => getmypid()))); $this->connection->close(); } $this->loop->stop(); }
/** * {@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; }
public function testConnectFails() { $loop = new StreamSelectLoop(); $deferred = new Deferred(); $deferred->promise()->then(function () { $this->fail('should not have succeeded'); }, function ($value) use($loop) { $this->assertEquals(1, $value); $loop->stop(); }); $request = new RequestFactory(); $connector = new TcpConnector($loop); $client = new Client($connector, $request); $client->connect('127.0.0.1', 54320); $loop->run(); }
/** * Shuts down the event loop. This basically exits the process. */ public function shutdown() { if ($this->inShutdown) { return; } $this->inShutdown = true; $this->sendCurrentFiles(); $this->loop->tick(); if ($this->connection && $this->connection->isWritable()) { $this->connection->close(); } if ($this->server) { @$this->server->shutdown(); } if ($this->loop) { $this->loop->tick(); @$this->loop->stop(); } exit; }
/** * Shuts down the event loop. This basically exits the process. */ public function shutdown() { if ($this->inShutdown) { return; } if ($this->errorLogger && ($logs = $this->errorLogger->cleanLogs())) { $messages = array_map(function ($item) { //array($level, $message, $context); $message = $item[1]; $context = $item[2]; if (isset($context['file'])) { $message .= ' in ' . $context['file'] . ':' . $context['line']; } if (isset($context['stack'])) { foreach ($context['stack'] as $idx => $stack) { $message .= PHP_EOL . sprintf("#%d: %s%s %s%s", $idx, isset($stack['class']) ? $stack['class'] . '->' : '', $stack['function'], isset($stack['file']) ? 'in' . $stack['file'] : '', isset($stack['line']) ? ':' . $stack['line'] : ''); } } return $message; }, $logs); error_log(implode(PHP_EOL, $messages)); } $this->inShutdown = true; if ($this->loop) { $this->sendCurrentFiles(); $this->loop->tick(); } if ($this->controller && $this->controller->isWritable()) { $this->controller->close(); } if ($this->server) { @$this->server->shutdown(); } if ($this->loop) { $this->sendCurrentFiles(); $this->loop->tick(); @$this->loop->stop(); } exit; }
public function testReturnsResponse() { $loop = new StreamSelectLoop(); $request = new RequestFactory(); $server = new Server($loop); $server->on('connection', function (SocketConnection $connection) use($server, $request) { $connection->on('data', function ($data) use($connection, $request) { $req = $request->response($data); $response = new Response($req->getId(), ['1.0']); $connection->write($response->write()); }); $connection->on('close', function () use($server) { $server->shutdown(); }); }); $server->listen(54323, '127.0.0.1'); $tcp = new TcpConnector($loop); $client = new Client($tcp, $request); $client->connect('127.0.0.1', 54323)->then(function (Connection $connection) use($loop) { $deferred = new Deferred(); $deferred->promise()->then(function ($value) { $this->assertEquals(1, $value); }); $electrum = new ElectrumClient($connection); $electrum->getServerVersion('1.9.6', ' 0.6')->then(function () use($deferred, $connection) { $deferred->resolve(1); $connection->close(); }, function () use($loop) { $loop->stop(); $this->fail(); }); }); $loop->run(); }
/** * */ public function stop() { $this->loop->stop(); }