/** * Tries to get the result for given result ID from RabbitMQ. * * @throws AMQPTimeoutException * @param string $resultId The UUID of the result to getc * @param int $timeout Timeout in seconds after which the fetching shall stop * @returns string JSON encoded data string */ public function getResult($resultId, $timeout = null) { if (is_null($timeout)) { $timeout = $this->timeout; } $hashId = Common::getHash($resultId, $this->amqNumQueues); $resultQueue = $this->getResultQueue($hashId); $this->waitId = $resultId; Common::$lg->addDebug(sprintf("Client waiting for request %s during %ds on %s (exchange %s)", $resultId, $timeout, $resultQueue, $this->resultExchange)); $this->resultChannel->basic_consume($resultQueue, '', false, false, false, false, array($this, 'onResult')); while (!$this->response) { try { $this->resultChannel->wait(null, false, $timeout); } catch (AMQPTimeoutException $e) { Common::$lg->addCritical("Client hit the fan after {$timeout}s"); throw $e; } } $res = json_decode($this->response); $data = $res->{'data'}; $this->response = null; return $data; }
/** * Puts a result on a RabbitMQ queue over AMQP. * * Puts the data for the given result ID (UUID, version 4) on RabbitMQ * queue. * * @param string $resultId The UUID of the result to put * @param string $data Data to submit * @returns void */ public function putResult($resultId, $data) { $routingKey = $resultId; $hashId = Common::getHash($routingKey, $this->amqNumQueues); $resultQueue = $this->getResultQueue($hashId); $body = array('result_id' => $resultId, 'data' => $data); $serializedBody = json_encode($body); $message = new AMQPMessage($serializedBody, array('content_type' => Constants\AMQ::CONTENT_TYPE, 'content_encoding' => $this->msgEncoding, 'delivery_mode' => Constants\AMQ::DELIVERY_MODE, 'correlation_id' => $hashId)); $this->resultChannel->basic_publish($message, $this->resultExchange, $hashId); Common::$lg->addDebug("Server published result {$resultId} within {$resultQueue} -> {$this->resultExchange}"); }