def signal_handler(self, ch, method, properties, body): f = {'new_descriptor': self.broadcast_wrapper, 'targeted_descriptor': self.targeted_wrapper, 'bus_exit': self.bus_exit_handler, 'on_idle': self.agent.on_idle} signal_type = serializer.loads(body) f[signal_type['signal_name']](**signal_type['args'])
def _rpc_callback(self, ch, method, properties, body): # Parse the rpc request body = serializer.loads(body) func_name = body['func_name'] args = body['args'] # Call the function ret = self._call_rpc_func(func_name, args) ret = serializer.dumps(ret) # Push the result of the function on the return queue b = False while not b: try: retpublish = ch.basic_publish( exchange='', routing_key=properties.reply_to, body=ret, properties=pika.BasicProperties( correlation_id=properties.correlation_id)) b = True except pika.exceptions.ConnectionClosed: log.info( "Disconnected (in _rpc_callback). Trying to reconnect") self._reconnect() ch.basic_ack(delivery_tag=method.delivery_tag)
def send_rpc(self, func_name, args, high_priority=True): # TODO catch any exception derived from pika.exceptions.AMQPError # Call the remote function body = serializer.dumps({'func_name': func_name, 'args': args}) corr_id = str(m_uuid.uuid4()) routing_key = 'rebus_master_rpc_highprio' if high_priority \ else 'rebus_master_rpc_lowprio' b = False while not b: try: retpublish = self.channel.basic_publish( exchange='', routing_key=routing_key, body=body, properties=pika.BasicProperties( reply_to=self.return_queue, correlation_id=corr_id, )) b = True except pika.exceptions.ConnectionClosed: log.info("Disconnected. Trying to reconnect") self.reconnect() # Wait for the return value response = None b = False while not b: try: meth, props, resp = self.channel.basic_get(self.return_queue) except pika.exceptions.ConnectionClosed: log.info("Disconnected. Trying to reconnect") self.reconnect() if meth: if corr_id == props.correlation_id: b = True response = str(resp) response = serializer.loads(response) self.channel.basic_ack(delivery_tag=meth.delivery_tag) # TODO try/reconnect + break if basic_ack impossible else: log.warning("An RPC returned with a wrong correlation ID") else: time.sleep(0.001) return response