def remote_exec(self, name, version, args, blocking=True): _req = RpcRequestMessage(version, args) _corr_id = str(uuid4()) self._response[_corr_id] = None self._rabbit['chan'].basic_publish( exchange='', routing_key=name, properties=pika.BasicProperties( correlation_id=_corr_id, reply_to=self._rabbit['callback_queue'] ), body=_req.dumps() ) # If not blocking, return the correlation id so the caller can lookup # the return if it wants too. if not blocking: return _corr_id return self.get_response(_corr_id, clear_response=True, wait=True)
def _request_handler(self, ch, meth, prop, body): _key = meth.routing_key try: _req = RpcRequestMessage.loads(body) _fn = self._get_function(_key, _req.version) self._logger.debug("Dipsatching Request: {}".format(_req)) if _req.args: _data = _fn(**_req.args) else: _data = _fn() _resp = RpcResponseMessage(0, return_value=_data) self._logger.debug("Preparing Response: {}".format(_resp)) _ret = _resp.dumps() except exp.ExceptionBase as ex: self._logger.info("Exception Detected: {}".format(ex)) _ret = RpcResponseMessage(ex.code, error_message=ex.message).dumps() except Exception as ex: self._logger.warning("Unhandled Exception Detected: {}".format(ex)) _ex = exp.UnhandledException(ex.message) _ret = RpcResponseMessage( _ex.code, error_message=_ex.message ).dumps() self._logger.debug("Dispatching Response: {}".format(_ret)) ch.basic_ack(delivery_tag=meth.delivery_tag) if prop.reply_to: self._logger.debug("Responding to request: {}".format(_key)) ch.basic_publish( exchange='', routing_key=prop.reply_to, body=str(_ret), properties=pika.BasicProperties( correlation_id=prop.correlation_id ) )