added rabbitmq helpers and setup command

This commit is contained in:
Gardient
2021-09-22 22:13:04 +03:00
parent 80e4ddf1a8
commit b50c67ae1e
10 changed files with 98 additions and 4 deletions

19
rmq_helper/queues.py Normal file
View 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)