Reputation: 145
I'm trying to set up a program that will consume from every queue in RabbitMQ and depending on certain messages it will run certain scripts. Unfortunately while adding consumers if it runs into a single error (i.e. timeout or queue not found) the entire channel is dead. Additionally queues come and go so it has to refresh the queues list quite often. Is this even possible? Here is my code so far.
import pika
import requests
import sys
try:
host = sys.argv[1]
except:
host = "localhost"
def get_queues(host="localhost", port=15672, user="guest", passwd="guest", virtual_host=None):
url = 'http://%s:%s/api/queues/%s' % (host, port, virtual_host or '')
response = requests.get(url, auth=(user, passwd))
return response.json()
queues = get_queues(host)
def get_on_message(queue):
def on_message(channel, method_frame, header_frame, body):
print("message from", queue)
channel.basic_ack(delivery_tag=method_frame.delivery_tag)
return on_message
connection = pika.BlockingConnection(pika.ConnectionParameters(host))
channel = connection.channel()
for queue in queues:
print(channel.is_open)
try:
channel.basic_consume(get_on_message(queue["name"]), queue["name"])
print("queue added",queue["name"])
except Exception as e:
print("queue failed",queue["name"])
sys.exit()
try:
channel.start_consuming()
except KeyboardInterrupt:
channel.stop_consuming()
connection.close()
Is there a right way to do this or is doing this not right at all?
Upvotes: 2
Views: 1049
Reputation: 10202
It possible to consume from every queue in any language. It's also wrong and if this is something that is required, then the whole design/setup should be re-thought.
EDIT after comments:
Basically, you'd need to get the names of all existing queues which can be programmatically done via the rest api (potentially even by calling rabbitmqctl and parsing the output). Once you have the names of the queues, you can simply consume from them as it is explained in the tutorial.
Once again, I don't think that this the right way to go, and perhaps you should consider using topic exchange - I'm guessing this since you wrote queues come and go
.
Upvotes: 6