-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdequeue.py
More file actions
40 lines (34 loc) · 1.49 KB
/
Copy pathdequeue.py
File metadata and controls
40 lines (34 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Script to dequeue a message from the specified RabbitMQ queue.
"""
import pika
import argparse
def parse_args():
parser = argparse.ArgumentParser(description='Dequeues a message from a RabbitMQ queue.')
parser.add_argument('--queue', required=True,
help='The name of the RabbitMQ queue to send the message to.')
parser.add_argument('--host', default='localhost',
help='The host where the RabbitMQ server is running.')
parser.add_argument('--rpc', default=None, help='The correlation ID (only for RPC calls).')
return parser.parse_args()
def main(args):
connection = pika.BlockingConnection(pika.ConnectionParameters(host=args.host))
channel = connection.channel()
queue = channel.queue_declare(queue=args.queue, durable=True)
method, properties, body = channel.basic_get(queue=args.queue)
if args.rpc:
waiting_count = 0
while (not args.rpc == properties.correlation_id
and waiting_count < queue.method.message_count):
method, properties, body = channel.basic_get(queue=args.queue)
waiting_count += 1
print(body.decode())
channel.basic_ack(delivery_tag=method.delivery_tag)
if waiting_count > 0:
print('WARNING: %d messages in response queue.' % waiting_count)
else:
print(body.decode())
channel.basic_ack(delivery_tag=method.delivery_tag)
connection.close()
if __name__ == '__main__':
main(parse_args())