pyinfra/mini_queue/consumer.py
Julius Unverfehrt 9ed1c48b56 blackkked
2022-02-14 16:37:20 +01:00

34 lines
1.0 KiB
Python

import sys
import pika
from retry import retry
from mini_queue.utils.config import CONFIG
def callback(channel, method, properties, body):
print("Received %r" % body)
response = body
channel.basic_publish(exchange="", routing_key=CONFIG.rabbitmq.queues.output, body=response)
channel.basic_ack(delivery_tag=method.delivery_tag)
def init_params():
credentials = pika.PlainCredentials(CONFIG.rabbitmq.user, CONFIG.rabbitmq.password)
parameters = pika.ConnectionParameters(
host=CONFIG.rabbitmq.host,
port=CONFIG.rabbitmq.port,
heartbeat=CONFIG.rabbitmq.heartbeat,
credentials=credentials,
)
return parameters
# @retry(pika.exceptions.AMQPConnectionError, delay=5, jitter=(1, 3))
def consume(parameters, queue):
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.basic_consume(queue=queue, auto_ack=False, on_message_callback=callback)
print(" [*] Waiting for messages. To exit press CTRL+C")
channel.start_consuming()