for the docker

This commit was merged in pull request #2.
This commit is contained in:
Gardient
2021-10-01 21:30:51 +03:00
parent b5a66b5d2d
commit fe424e3fa2
10 changed files with 86 additions and 81 deletions

View File

@@ -15,18 +15,23 @@ class RabbitMQ:
def init_app(self, app: Flask):
self.host = app.config.get('RABBITMQ_HOST', 'localhost')
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(host=self.host))
self.channel = self.connection.channel()
def _ensure_connection(self):
if self.channel is None:
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(host=self.host))
self.channel = self.connection.channel()
def ensure_queue_exists(self, queue_name: str):
"""Ensures the queue exists on the default exchange
:param queue_name: the name of the queue
"""
self._ensure_connection()
self.channel.queue_declare(queue=queue_name, durable=True)
def queue_publish(self, queue_name: str, message):
self._ensure_connection()
self.ensure_queue_exists(queue_name)
body = json.dumps(message)
@@ -38,6 +43,7 @@ class RabbitMQ:
:param name: the name of the exchange
"""
self._ensure_connection()
self.channel.exchange_declare(exchange=name, exchange_type='topic', durable=True)
def exchange_publish(self, exchange_name: str, topic: str, message):
@@ -47,6 +53,7 @@ class RabbitMQ:
:param topic: the topic to publish to
:param message: the message to publish (will be turned into a json)
"""
self._ensure_connection()
self.ensure_exchange_exists(exchange_name)
body = json.dumps(message)