Example #1
0
 /**
  * @param GuzzleClient $client
  * @param RequestInterface $request
  * @return ResponseInterface
  */
 public function handle(GuzzleClient $client, RequestInterface $request)
 {
     try {
         $response = $client->send(new HttpRequest($request->getMethod(), $request->getUri(), $request->getHeaders(), $this->jsonEncoder->encode($request->getBody())));
     } catch (\Exception $e) {
         $this->logger->debug(sprintf('Exception thrown during Guzzle request'), ['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile(), 'trace' => $e->getTraceAsString()]);
         $response = new Response(500);
         if ($e instanceof ClientException) {
             $response = new Response(401);
         }
     }
     return $request->getResponseFactory()->create($response);
 }
Example #2
0
 /**
  * @param Message $message
  *
  * @return bool
  */
 public function write(Message $message)
 {
     $attempt = 1;
     $content = $message->getPayload();
     $contentLength = strlen($content);
     try {
         while ((int) $this->stream->write($content) !== $contentLength && $attempt++ < self::RETRY) {
             Sleep::millisecond(self::RETRY_INTERVAL);
         }
         $success = $attempt < self::RETRY;
     } catch (\Exception $e) {
         $this->logger->warning('An error occurred writing to APNS stream', array('error_message' => $e->getMessage()));
         $success = false;
     }
     return $success;
 }
Example #3
0
 protected function childSignalHandler($signo, $pid = null, $status = null)
 {
     //If no pid is provided, that means we're getting the signal from the system.  Let's figure out
     //which child process ended
     if (!$pid) {
         $pid = pcntl_waitpid(-1, $status, WNOHANG);
     }
     //Make sure we get all of the exited children
     while ($pid > 0) {
         if ($pid && isset($this->currentJobs[$pid])) {
             $exitCode = pcntl_wexitstatus($status);
             if ($exitCode != 0) {
                 $this->logger->info("{$pid} exited with status " . $exitCode);
             }
             unset($this->currentJobs[$pid]);
         } else {
             if ($pid) {
                 //Oh no, our job has finished before this parent process could even note that it had been launched!
                 //Let's make note of it and handle it when the parent process is ready for it
                 $this->logger->info("..... Adding {$pid} to the signal queue .....");
                 $this->signalQueue[$pid] = $status;
             }
         }
         $pid = pcntl_waitpid(-1, $status, WNOHANG);
     }
     return true;
 }
 public function tick()
 {
     if (null === $this->pdo) {
         $this->logger->warning('Unable to ping sql server, service pdo is unavailable');
         return;
     }
     //if connection is persistent we don't need to ping
     if (true === $this->pdo->getAttribute(\PDO::ATTR_PERSISTENT)) {
         return;
     }
     try {
         $startTime = microtime(true);
         $this->pdo->query('SELECT 1');
         $endTime = microtime(true);
         $this->logger->notice(sprintf('Successfully ping sql server (~%s ms)', round(($endTime - $startTime) * 100000), 2));
     } catch (\PDOException $e) {
         $this->logger->emergency('Sql server is gone, and unable to reconnect');
         throw $e;
     }
 }
Example #5
0
 /**
  * Launches a command as a separate process.
  *
  * The '--process-timeout' parameter can be used to set the process timeout
  * in seconds. Default timeout is 300 seconds.
  * If '--ignore-errors' parameter is specified any errors are ignored;
  * otherwise, an exception is raises if an error happened.
  *
  * @param string               $command
  * @param array                $params
  * @param LoggerInterface|null $logger
  *
  * @return integer The exit status code
  * @throws \RuntimeException if command failed and '--ignore-errors' parameter is not specified
  */
 public function runCommand($command, $params = [], LoggerInterface $logger = null)
 {
     $params = array_merge(['command' => $command], $params);
     if ($this->env && $this->env !== 'dev') {
         $params['--env'] = $this->env;
     }
     $ignoreErrors = false;
     if (array_key_exists('--ignore-errors', $params)) {
         $ignoreErrors = true;
         unset($params['--ignore-errors']);
     }
     $pb = new ProcessBuilder();
     $pb->add($this->getPhp())->add($this->consoleCmdPath);
     if (array_key_exists('--process-timeout', $params)) {
         $pb->setTimeout($params['--process-timeout']);
         unset($params['--process-timeout']);
     } else {
         $pb->setTimeout($this->defaultTimeout);
     }
     foreach ($params as $name => $val) {
         $this->processParameter($pb, $name, $val);
     }
     $process = $pb->inheritEnvironmentVariables(true)->getProcess();
     if (!$logger) {
         $logger = new NullLogger();
     }
     $exitCode = $process->run(function ($type, $data) use($logger) {
         if ($type === Process::ERR) {
             $logger->error($data);
         } else {
             $logger->notice($data);
         }
     });
     // synchronize all data caches
     if ($this->dataCacheManager) {
         $this->dataCacheManager->sync();
     }
     $this->processResult($exitCode, $ignoreErrors, $logger);
     return $exitCode;
 }
Example #6
0
 /**
  * @param \Thruway\ClientSession $session
  * @param $Uri
  * @throws \Exception
  * @return \React\Promise\Promise
  */
 public function unregister(ClientSession $session, $Uri)
 {
     // TODO: maybe add an option to wait for pending calls to finish
     $registration = null;
     foreach ($this->registrations as $k => $r) {
         if (isset($r['procedure_name'])) {
             if ($r['procedure_name'] == $Uri) {
                 $registration =& $this->registrations[$k];
                 break;
             }
         }
     }
     if ($registration === null) {
         throw new \Exception("registration not found");
     }
     // we remove the callback from the client here
     // because we don't want the client to respond to any more calls
     $registration['callback'] = null;
     $futureResult = new Deferred();
     if (!isset($registration["registration_id"])) {
         // this would happen if the registration was never acknowledged by the router
         // we should remove the registration and resolve any pending deferreds
         $this->logger->error("Registration ID is not set while attempting to unregister " . $Uri . "\n");
         // reject the pending registration
         $registration['futureResult']->reject();
         // TODO: need to figure out what to do in this off chance
         // We should still probably return a promise here that just rejects
         // there is an issue with the pending registration too that
         // the router may have a "REGISTERED" in transit and may still think that is
         // good to go - so maybe still send the unregister?
     }
     $requestId = Session::getUniqueId();
     // save the request id so we can find this in the registration
     // list to call the deferred and remove it from the list
     $registration['unregister_request_id'] = $requestId;
     $registration['unregister_deferred'] = $futureResult;
     $unregisterMsg = new UnregisterMessage($requestId, $registration['registration_id']);
     $session->sendMessage($unregisterMsg);
     return $futureResult->promise();
 }