def _send_reply(self, conn, reply=None, failure=None, ending=False, log_failure=True): if self.reply_q and not self._obsolete_reply_queues.reply_q_valid(self.reply_q, self.msg_id): return if failure: failure = rpc_common.serialize_remote_exception(failure, log_failure) msg = {"result": reply, "failure": failure} if ending: msg["ending"] = True rpc_amqp._add_unique_id(msg) unique_id = msg[rpc_amqp.UNIQUE_ID] # If a reply_q exists, add the msg_id to the reply and pass the # reply_q to direct_send() to use it as the response queue. # Otherwise use the msg_id for backward compatibility. if self.reply_q: msg["_msg_id"] = self.msg_id try: if ending: LOG.debug( "sending reply msg_id: %(msg_id)s " "reply queue: %(reply_q)s" % {"msg_id": self.msg_id, "unique_id": unique_id, "reply_q": self.reply_q} ) conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg)) except rpc_amqp.AMQPDestinationNotFound: self._obsolete_reply_queues.add(self.reply_q, self.msg_id) else: # TODO(sileht): look at which version of oslo-incubator rpc # send need this, but I guess this is older than icehouse # if this is icehouse, we can drop this at Mitaka # if this is havana, we can drop this now. conn.direct_send(self.msg_id, rpc_common.serialize_msg(msg))
def _send_reply(self, conn, reply=None, failure=None, ending=True): if not self._obsolete_reply_queues.reply_q_valid( self.reply_q, self.msg_id): return if failure: failure = rpc_common.serialize_remote_exception(failure) # NOTE(sileht): ending can be removed in N*, see Listener.wait() # for more detail. msg = { 'result': reply, 'failure': failure, 'ending': ending, '_msg_id': self.msg_id } rpc_amqp._add_unique_id(msg) unique_id = msg[rpc_amqp.UNIQUE_ID] LOG.debug( "sending reply msg_id: %(msg_id)s " "reply queue: %(reply_q)s " "time elapsed: %(elapsed)ss", { 'msg_id': self.msg_id, 'unique_id': unique_id, 'reply_q': self.reply_q, 'elapsed': self.stopwatch.elapsed() }) conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg))
def _send_reply(self, conn, reply=None, failure=None, ending=False, log_failure=True): if (self.reply_q and not self._obsolete_reply_queues.reply_q_valid(self.reply_q, self.msg_id)): return if failure: failure = rpc_common.serialize_remote_exception(failure, log_failure) msg = {'result': reply, 'failure': failure} if ending: msg['ending'] = True rpc_amqp._add_unique_id(msg) # If a reply_q exists, add the msg_id to the reply and pass the # reply_q to direct_send() to use it as the response queue. # Otherwise use the msg_id for backward compatibility. if self.reply_q: msg['_msg_id'] = self.msg_id try: conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg)) except rpc_amqp.AMQPDestinationNotFound: self._obsolete_reply_queues.add(self.reply_q, self.msg_id) else: # TODO(sileht): look at which version of oslo-incubator rpc # send need this, but I guess this is older than icehouse # if this is icehouse, we can drop this at Mitaka # if this is havana, we can drop this now. conn.direct_send(self.msg_id, rpc_common.serialize_msg(msg))
def _send(self, target, ctxt, message, wait_for_reply=None, timeout=None, call_monitor_timeout=None, envelope=True, notify=False, retry=None, transport_options=None): msg = message if wait_for_reply: msg_id = uuid.uuid4().hex msg.update({'_msg_id': msg_id}) msg.update({'_reply_q': self._get_reply_q()}) msg.update({'_timeout': call_monitor_timeout}) rpc_amqp._add_unique_id(msg) unique_id = msg[rpc_amqp.UNIQUE_ID] rpc_amqp.pack_context(msg, ctxt) if envelope: msg = rpc_common.serialize_msg(msg) if wait_for_reply: self._waiter.listen(msg_id) log_msg = "CALL msg_id: %s " % msg_id else: log_msg = "CAST unique_id: %s " % unique_id try: with self._get_connection(rpc_common.PURPOSE_SEND) as conn: if notify: exchange = self._get_exchange(target) LOG.debug(log_msg + "NOTIFY exchange '%(exchange)s'" " topic '%(topic)s'", {'exchange': exchange, 'topic': target.topic}) conn.notify_send(exchange, target.topic, msg, retry=retry) elif target.fanout: log_msg += "FANOUT topic '%(topic)s'" % { 'topic': target.topic} LOG.debug(log_msg) conn.fanout_send(target.topic, msg, retry=retry) else: topic = target.topic exchange = self._get_exchange(target) if target.server: topic = '%s.%s' % (target.topic, target.server) LOG.debug(log_msg + "exchange '%(exchange)s'" " topic '%(topic)s'", {'exchange': exchange, 'topic': topic}) conn.topic_send(exchange_name=exchange, topic=topic, msg=msg, timeout=timeout, retry=retry, transport_options=transport_options) if wait_for_reply: result = self._waiter.wait(msg_id, timeout, call_monitor_timeout) if isinstance(result, Exception): raise result return result finally: if wait_for_reply: self._waiter.unlisten(msg_id)
def _send_reply(self, conn, reply=None, failure=None, ending=False, log_failure=True): if (self.reply_q and not self._obsolete_reply_queues.reply_q_valid( self.reply_q, self.msg_id)): return if failure: failure = rpc_common.serialize_remote_exception( failure, log_failure) msg = {'result': reply, 'failure': failure} if ending: msg['ending'] = True rpc_amqp._add_unique_id(msg) # If a reply_q exists, add the msg_id to the reply and pass the # reply_q to direct_send() to use it as the response queue. # Otherwise use the msg_id for backward compatibility. if self.reply_q: msg['_msg_id'] = self.msg_id try: conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg)) except rpc_amqp.AMQPDestinationNotFound: self._obsolete_reply_queues.add(self.reply_q, self.msg_id) else: # TODO(sileht): look at which version of oslo-incubator rpc # send need this, but I guess this is older than icehouse # if this is icehouse, we can drop this at M # if this is havana, we can drop this now. conn.direct_send(self.msg_id, rpc_common.serialize_msg(msg))
def _send(self, target, ctxt, message, wait_for_reply=None, timeout=None, call_monitor_timeout=None, envelope=True, notify=False, retry=None): msg = message if wait_for_reply: msg_id = uuid.uuid4().hex msg.update({'_msg_id': msg_id}) msg.update({'_reply_q': self._get_reply_q()}) msg.update({'_timeout': call_monitor_timeout}) rpc_amqp._add_unique_id(msg) unique_id = msg[rpc_amqp.UNIQUE_ID] rpc_amqp.pack_context(msg, ctxt) if envelope: msg = rpc_common.serialize_msg(msg) if wait_for_reply: self._waiter.listen(msg_id) log_msg = "CALL msg_id: %s " % msg_id else: log_msg = "CAST unique_id: %s " % unique_id try: with self._get_connection(rpc_common.PURPOSE_SEND) as conn: if notify: exchange = self._get_exchange(target) LOG.debug(log_msg + "NOTIFY exchange '%(exchange)s'" " topic '%(topic)s'", {'exchange': exchange, 'topic': target.topic}) conn.notify_send(exchange, target.topic, msg, retry=retry) elif target.fanout: log_msg += "FANOUT topic '%(topic)s'" % { 'topic': target.topic} LOG.debug(log_msg) conn.fanout_send(target.topic, msg, retry=retry) else: topic = target.topic exchange = self._get_exchange(target) if target.server: topic = '%s.%s' % (target.topic, target.server) LOG.debug(log_msg + "exchange '%(exchange)s'" " topic '%(topic)s'", {'exchange': exchange, 'topic': topic}) conn.topic_send(exchange_name=exchange, topic=topic, msg=msg, timeout=timeout, retry=retry) if wait_for_reply: result = self._waiter.wait(msg_id, timeout, call_monitor_timeout) if isinstance(result, Exception): raise result return result finally: if wait_for_reply: self._waiter.unlisten(msg_id)
def _send(self, target, ctxt, message, wait_for_reply=None, timeout=None, envelope=True, notify=False, retry=None): # FIXME(markmc): remove this temporary hack class Context(object): def __init__(self, d): self.d = d def to_dict(self): return self.d context = Context(ctxt) msg = message if wait_for_reply: msg_id = uuid.uuid4().hex msg.update({'_msg_id': msg_id}) LOG.debug('MSG_ID is %s', msg_id) msg.update({'_reply_q': self._get_reply_q()}) rpc_amqp._add_unique_id(msg) rpc_amqp.pack_context(msg, context) if envelope: msg = rpc_common.serialize_msg(msg) if wait_for_reply: self._waiter.listen(msg_id) try: with self._get_connection(rpc_amqp.PURPOSE_SEND) as conn: if notify: conn.notify_send(self._get_exchange(target), target.topic, msg, retry=retry) elif target.fanout: conn.fanout_send(target.topic, msg, retry=retry) else: topic = target.topic if target.server: topic = '%s.%s' % (target.topic, target.server) conn.topic_send(exchange_name=self._get_exchange(target), topic=topic, msg=msg, timeout=timeout, retry=retry) if wait_for_reply: result = self._waiter.wait(msg_id, timeout) if isinstance(result, Exception): raise result return result finally: if wait_for_reply: self._waiter.unlisten(msg_id)
def serialize(ctxt, publisher_id, priority, event_type, payload): message = {'message_id': six.text_type(uuid.uuid4()), 'publisher_id': publisher_id, 'timestamp': timeutils.utcnow(), 'priority': priority, 'event_type': event_type, 'payload': payload, } rpc_amqp._add_unique_id(message) rpc_amqp.pack_context(message, ctxt) message = rpc_common.serialize_msg(message) return message
def _send_reply(self, conn, reply=None, failure=None, log_failure=True): if not self._obsolete_reply_queues.reply_q_valid(self.reply_q, self.msg_id): return if failure: failure = rpc_common.serialize_remote_exception(failure, log_failure) # NOTE(sileht): ending can be removed in N*, see Listener.wait() # for more detail. msg = {"result": reply, "failure": failure, "ending": True, "_msg_id": self.msg_id} rpc_amqp._add_unique_id(msg) unique_id = msg[rpc_amqp.UNIQUE_ID] LOG.debug( "sending reply msg_id: %(msg_id)s " "reply queue: %(reply_q)s", {"msg_id": self.msg_id, "unique_id": unique_id, "reply_q": self.reply_q}, ) conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg))
def _send_reply(self, conn, reply=None, failure=None, ending=False, log_failure=True): if failure: failure = rpc_common.serialize_remote_exception(failure, log_failure) msg = {'result': reply, 'failure': failure} if ending: msg['ending'] = True rpc_amqp._add_unique_id(msg) # If a reply_q exists, add the msg_id to the reply and pass the # reply_q to direct_send() to use it as the response queue. # Otherwise use the msg_id for backward compatibility. if self.reply_q: msg['_msg_id'] = self.msg_id conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg)) else: conn.direct_send(self.msg_id, rpc_common.serialize_msg(msg))
def _send_reply(self, conn, reply=None, failure=None, ending=True): if not self._obsolete_reply_queues.reply_q_valid(self.reply_q, self.msg_id): return if failure: failure = rpc_common.serialize_remote_exception(failure) # NOTE(sileht): ending can be removed in N*, see Listener.wait() # for more detail. msg = {'result': reply, 'failure': failure, 'ending': ending, '_msg_id': self.msg_id} rpc_amqp._add_unique_id(msg) unique_id = msg[rpc_amqp.UNIQUE_ID] LOG.debug("sending reply msg_id: %(msg_id)s " "reply queue: %(reply_q)s " "time elapsed: %(elapsed)ss", { 'msg_id': self.msg_id, 'unique_id': unique_id, 'reply_q': self.reply_q, 'elapsed': self.stopwatch.elapsed()}) conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg))
def _send_reply(self, conn, reply=None, failure=None, ending=False, log_failure=True): if failure: failure = rpc_common.serialize_remote_exception( failure, log_failure) msg = {'result': reply, 'failure': failure} if ending: msg['ending'] = True rpc_amqp._add_unique_id(msg) # If a reply_q exists, add the msg_id to the reply and pass the # reply_q to direct_send() to use it as the response queue. # Otherwise use the msg_id for backward compatibility. if self.reply_q: msg['_msg_id'] = self.msg_id conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg)) else: conn.direct_send(self.msg_id, rpc_common.serialize_msg(msg))
def _send(self, target, ctxt, message, wait_for_reply=None, timeout=None, envelope=True, notify=False, retry=None): # FIXME(markmc): remove this temporary hack class Context(object): def __init__(self, d): self.d = d def to_dict(self): return self.d context = Context(ctxt) msg = message if wait_for_reply: msg_id = uuid.uuid4().hex msg.update({"_msg_id": msg_id}) msg.update({"_reply_q": self._get_reply_q()}) rpc_amqp._add_unique_id(msg) unique_id = msg[rpc_amqp.UNIQUE_ID] rpc_amqp.pack_context(msg, context) if envelope: msg = rpc_common.serialize_msg(msg) if wait_for_reply: self._waiter.listen(msg_id) log_msg = "CALL msg_id: %s " % msg_id else: log_msg = "CAST unique_id: %s " % unique_id try: with self._get_connection(rpc_amqp.PURPOSE_SEND) as conn: if notify: exchange = self._get_exchange(target) log_msg += "NOTIFY exchange '%(exchange)s'" " topic '%(topic)s'" % { "exchange": exchange, "topic": target.topic, } LOG.debug(log_msg) conn.notify_send(exchange, target.topic, msg, retry=retry) elif target.fanout: log_msg += "FANOUT topic '%(topic)s'" % {"topic": target.topic} LOG.debug(log_msg) conn.fanout_send(target.topic, msg, retry=retry) else: topic = target.topic exchange = self._get_exchange(target) if target.server: topic = "%s.%s" % (target.topic, target.server) log_msg += "exchange '%(exchange)s'" " topic '%(topic)s'" % { "exchange": exchange, "topic": target.topic, } LOG.debug(log_msg) conn.topic_send(exchange_name=exchange, topic=topic, msg=msg, timeout=timeout, retry=retry) if wait_for_reply: result = self._waiter.wait(msg_id, timeout) if isinstance(result, Exception): raise result return result finally: if wait_for_reply: self._waiter.unlisten(msg_id)
def _send(self, target, ctxt, message, wait_for_reply=None, timeout=None, envelope=True, notify=False, retry=None): # FIXME(markmc): remove this temporary hack class Context(object): def __init__(self, d): self.d = d def to_dict(self): return self.d context = Context(ctxt) msg = message if wait_for_reply: msg_id = uuid.uuid4().hex msg.update({'_msg_id': msg_id}) msg.update({'_reply_q': self._get_reply_q()}) rpc_amqp._add_unique_id(msg) unique_id = msg[rpc_amqp.UNIQUE_ID] rpc_amqp.pack_context(msg, context) if envelope: msg = rpc_common.serialize_msg(msg) if wait_for_reply: self._waiter.listen(msg_id) log_msg = "CALL msg_id: %s " % msg_id else: log_msg = "CAST unique_id: %s " % unique_id try: with self._get_connection(rpc_common.PURPOSE_SEND) as conn: if notify: exchange = self._get_exchange(target) log_msg += "NOTIFY exchange '%(exchange)s'" \ " topic '%(topic)s'" % { 'exchange': exchange, 'topic': target.topic} LOG.debug(log_msg) conn.notify_send(exchange, target.topic, msg, retry=retry) elif target.fanout: log_msg += "FANOUT topic '%(topic)s'" % { 'topic': target.topic } LOG.debug(log_msg) conn.fanout_send(target.topic, msg, retry=retry) else: topic = target.topic exchange = self._get_exchange(target) if target.server: topic = '%s.%s' % (target.topic, target.server) log_msg += "exchange '%(exchange)s'" \ " topic '%(topic)s'" % { 'exchange': exchange, 'topic': target.topic} LOG.debug(log_msg) conn.topic_send(exchange_name=exchange, topic=topic, msg=msg, timeout=timeout, retry=retry) if wait_for_reply: result = self._waiter.wait(msg_id, timeout) if isinstance(result, Exception): raise result return result finally: if wait_for_reply: self._waiter.unlisten(msg_id)
def _send(self, target, ctxt, message, wait_for_reply=None, timeout=None, envelope=True, notify=False, retry=None): msg = message #如果等待reply,则生成msg._msg_id/msg._reply_q;并生成self._waiter if wait_for_reply: msg_id = uuid.uuid4().hex msg.update({'_msg_id': msg_id}) msg.update({'_reply_q': self._get_reply_q()}) #wait已经生成关联消费者到reply_q队列 #msg._unique_id = uuid rpc_amqp._add_unique_id(msg) unique_id = msg[rpc_amqp.UNIQUE_ID] rpc_amqp.pack_context(msg, ctxt) #ctxt信息放入msg if envelope: msg = rpc_common.serialize_msg(msg) if wait_for_reply: self._waiter.listen( msg_id) #wait在ReplyWaiters中创建msg_id队列,用于接受reply log_msg = "CALL msg_id: %s " % msg_id else: log_msg = "CAST unique_id: %s " % unique_id try: with self._get_connection(rpc_common.PURPOSE_SEND) as conn: if notify: exchange = self._get_exchange(target) log_msg += "NOTIFY exchange '%(exchange)s'" \ " topic '%(topic)s'" % { 'exchange': exchange, 'topic': target.topic} LOG.debug(log_msg) conn.notify_send(exchange, target.topic, msg, retry=retry) elif target.fanout: log_msg += "FANOUT topic '%(topic)s'" % { 'topic': target.topic } LOG.debug(log_msg) conn.fanout_send(target.topic, msg, retry=retry) else: topic = target.topic exchange = self._get_exchange(target) if target.server: topic = '%s.%s' % (target.topic, target.server) log_msg += "exchange '%(exchange)s'" \ " topic '%(topic)s'" % { 'exchange': exchange, 'topic': topic} LOG.debug(log_msg) conn.topic_send(exchange_name=exchange, topic=topic, msg=msg, timeout=timeout, retry=retry) if wait_for_reply: result = self._waiter.wait( msg_id, timeout) #wait最多等待timeout,在ReplyWaiters的msg_id队列获取reply数据 if isinstance(result, Exception): raise result return result finally: if wait_for_reply: self._waiter.unlisten(msg_id)