Search code examples
pythonherokurabbitmqpika

Cannot send to rpc_server my request i am using RabbitMQ on Heroku with pika


I just set up a RabbitMQ add-on in heroku. After developing my app to queue up and consume messages running on a local instance, I deployed it to Heroku and have not been able to send my message successfully yet.

the rpc_queue never receive anything, my urls are corrects and my rpc_server listen well but never receive anything

server.py

parameters = pika.URLParameters('amqp://djeo4uf8:f4323HekqiVXgt_vlnqPfJnvJruzszbn@sad-groundsel-39.bigwig.lshift.net:10723/fl1sX7CbcDds')        
    connection = pika.BlockingConnection(parameters)

    channel = connection.channel()

    channel.queue_declare(queue='rpc_queue')

    def on_request(ch, method, props, body):            
        data = loads( b64decode(body) )
        response = getattr(functions, data["command"] )(data["args"])   
        response =  b64encode( dumps(response) )

        ch.basic_publish(exchange='',
                         routing_key=props.reply_to,
                         properties=pika.BasicProperties(correlation_id = \
                                                             props.correlation_id),
                         body=str(response))
        ch.basic_ack(delivery_tag = method.delivery_tag)

    channel.basic_qos(prefetch_count=1)
    channel.basic_consume(on_request, queue='rpc_queue')

    print(" [x] Awaiting RPC requests")
    channel.start_consuming()   

client.py

class RpcClient(object):
    def __init__(self):
        parameters = pika.URLParameters('amqp://djeo4uf8:f4323HekqiVXgt_vlnqPfJnvJruzszbn@sad-groundsel-39.bigwig.lshift.net:10722/fl1sX7CbcDds') 
        parameters.socket_timeout = 5
        self.connection = pika.BlockingConnection(parameters)
        self.channel = self.connection.channel()

        result = self.channel.queue_declare(exclusive=True)
        self.callback_queue = result.method.queue

        self.channel.basic_consume(self.on_response, no_ack=True,
                                   queue=self.callback_queue)

    def on_response(self, ch, method, props, body):     
        if self.corr_id == props.correlation_id:
            self.response =loads( b64decode(body) )

    def call(self, dict_data):
        self.response = None
        self.corr_id = str(uuid.uuid4())
        self.channel.basic_publish(exchange='',
                                   routing_key='rpc_queue',
                                   properties=pika.BasicProperties(
                                         reply_to = self.callback_queue,
                                         correlation_id = self.corr_id,
                                         ),
                                   body=b64encode( dumps(dict_data) ) )

        while self.response is None:             
            self.connection.process_data_events()
        self.channel.close()
        self.connection.close() 
        return self.response

so i do the call and i never receive anything because my rpc_queue always is empty

rpc = RpcClient()           
response = rpc.call(payloadObj)

Much thanks in advance!


Solution

  • Urls in server and client using different ports 10723 and 10722, to receive messages it should be either same RabbitMQ node listening on both ports or clustered nodes with HA queues.