Last active
January 30, 2018 09:55
-
-
Save danield137/9f842ce16a0e383e1c5acf6d96cd6feb to your computer and use it in GitHub Desktop.
Python Rabbit Utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/local/bin python3 | |
| import json | |
| import pika | |
| def dump_queue_to_file(broker, queue): | |
| if not broker: | |
| raise ValueError('please set the broker url.') | |
| print('connecting to : {0}'.format(broker)) | |
| parameters = pika.URLParameters(broker) | |
| connection = pika.BlockingConnection(parameters) | |
| channel = connection.channel() | |
| method = channel.queue_declare(queue, passive=True) | |
| message_count = method.method.message_count | |
| print('reading messages : {0}'.format(message_count)) | |
| with open('{}_dump.jsonl'.format(queue), 'w+') as file: | |
| for x in range(0, message_count): | |
| # printing 10 messages of progress indication. | |
| print('current message : {}'.format(x)) | |
| result = channel.basic_get(queue=queue, no_ack=False) | |
| if result: | |
| # get_ok = result[0] | |
| # properties = result[1] | |
| body = result[2] | |
| body_json = json.loads(body) | |
| json.dump(body_json, file) | |
| file.write('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment