コード例 #1
0
ファイル: RpcClient.php プロジェクト: BobrD/ProcessManager
 /**
  * @param $channel
  * @param string $message
  * @return DeferredPromise
  */
 public function call($channel, $message)
 {
     $loop = LoopFactory::create();
     $factory = new StompFactory($loop);
     $client = $factory->createClient();
     $deferred = new Deferred();
     $timer = $loop->addTimer(2, function () use($deferred, $client) {
         $client->disconnect();
         $deferred->reject(new RpcTimeIsOutException());
     });
     $client->connect()->then(function (StompClient $client) use($message, $channel, $loop, $deferred, $timer) {
         $rpcReceiver = function (Frame $frame) use($deferred, $timer, $client) {
             $timer->cancel();
             $client->disconnect();
             try {
                 $deferred->resolve($frame);
             } catch (\Exception $e) {
                 $deferred->reject($e);
             }
         };
         $client->sendToTemp($channel, $message, [], $rpcReceiver);
     }, function () use($deferred, $client) {
         $client->disconnect();
         $deferred->reject(new \RuntimeException('Error start rpc connection'));
     });
     $loop->run();
     return $deferred->promise();
 }
コード例 #2
0
ファイル: Manager.php プロジェクト: BobrD/ProcessManager
 /**
  * Start manager loop
  */
 private function startLoop()
 {
     $this->client = $this->stompFactory->createClient();
     $this->client->connect()->then(function (Client $client) {
         $this->loop->addPeriodicTimer(self::PROVISION_TIME, function () {
             $this->provision();
         });
         $client->subscribe($this->channel, function (Frame $frame) use($client) {
             try {
                 $request = $this->messageTransformer->decodeRequest($frame->body);
                 $closure = $request->getClosure();
                 if (!is_callable($closure)) {
                     throw new ManagerException('Запрос не содерджит callable');
                 }
                 $result = $closure($this);
                 $response = new Response($result);
             } catch (\Exception $e) {
                 $this->logger->error('Exception при обработке запроса ' . $e->getMessage());
                 $response = new Response($e->getMessage(), Response::STATUS_ERROR);
             }
             if ($replayTo = $frame->getHeader('reply-to')) {
                 $body = $this->messageTransformer->encodeResponse($response);
                 $client->send($replayTo, $body);
             }
         });
     }, function (\Exception $e) {
         $this->logger->critical($e->getMessage());
     });
     $this->loop->run();
 }