Beispiel #1
0
    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)
Beispiel #2
0
 def get_value(self, agent_id, desc_domain, selector):
     log.debug("GETVALUE: %s %s:%s", agent_id, desc_domain, selector)
     if not self._check_agent_id(agent_id):
         return None
     value = self.store.get_value(str(desc_domain), str(selector))
     if value is None:
         return ""
     return serializer.dumps(value)
Beispiel #3
0
 def send_signal(self, signal_name, args):
     # Send a signal on the exchange
     body = {'signal_name': signal_name, 'args': args}
     body = serializer.dumps(body)
     b = False
     while not b:
         try:
             self.channel.basic_publish(
                 exchange='rebus_signals', routing_key='', body=body,
                 properties=pika.BasicProperties(delivery_mode=2,))
             b = True
         except pika.exceptions.ConnectionClosed:
             log.info("Disconnected (in send_signal). "
                      "Trying to reconnect...")
             self.reconnect()
             time.sleep(0.5)
Beispiel #4
0
    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