added rabbitmq helpers and setup command
This commit is contained in:
19
rmq_helper/queues.py
Normal file
19
rmq_helper/queues.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import json
|
||||
|
||||
from pika.adapters.blocking_connection import BlockingChannel
|
||||
|
||||
from .extension import with_pika_channel
|
||||
|
||||
|
||||
@with_pika_channel
|
||||
def ensure_queue_exists(name: str, channel: BlockingChannel = None):
|
||||
channel.queue_declare(queue=name)
|
||||
|
||||
|
||||
@with_pika_channel
|
||||
def queue_publish(queue_name: str, message, channel: BlockingChannel = None):
|
||||
ensure_queue_exists(queue_name, channel=channel)
|
||||
|
||||
body = json.dumps(message)
|
||||
body_bytes = body.encode('utf-8')
|
||||
channel.basic_publish(exchange='', routing_key=queue_name, body=body_bytes)
|
||||
Reference in New Issue
Block a user