added rabbitmq helpers and setup command
This commit is contained in:
4
.idea/mahssage-bus-api.iml
generated
4
.idea/mahssage-bus-api.iml
generated
@@ -7,8 +7,4 @@
|
||||
<orderEntry type="jdk" jdkName="Python 3.9 (mahssage-bus-api)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
<component name="PyDocumentationSettings">
|
||||
<option name="format" value="PLAIN" />
|
||||
<option name="myDocStringFormat" value="Plain" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -95,3 +95,4 @@ def register_commands(app: Flask):
|
||||
app.cli.add_command(commands.clean)
|
||||
app.cli.add_command(commands.urls)
|
||||
app.cli.add_command(commands.seed)
|
||||
app.cli.add_command(commands.setup_rabmq)
|
||||
|
||||
@@ -5,6 +5,8 @@ from flask import current_app
|
||||
from flask.cli import with_appcontext
|
||||
from werkzeug.exceptions import MethodNotAllowed, NotFound
|
||||
|
||||
import rmq_helper
|
||||
from .target.models import Target
|
||||
from .target_exchange.models import TargetExchange
|
||||
|
||||
|
||||
@@ -91,3 +93,16 @@ def seed():
|
||||
|
||||
TargetExchange.ensure_created("")
|
||||
TargetExchange.ensure_created("webhooks")
|
||||
|
||||
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def setup_rabmq():
|
||||
"""Set up rabbitMQ"""
|
||||
for exchange in TargetExchange.query.filter(TargetExchange.name != "").all():
|
||||
rmq_helper.ensure_exchange_exists(exchange.name)
|
||||
|
||||
for target in Target.query.join(Target.exchange).filter(TargetExchange.name == "").all():
|
||||
rmq_helper.ensure_queue_exists(target.routing_key)
|
||||
|
||||
pass
|
||||
|
||||
@@ -18,6 +18,7 @@ class Config(object):
|
||||
JWT_AUTH_HEADER_PREFIX = 'Token'
|
||||
APISPEC_TITLE = 'MahssageBus API'
|
||||
APISPEC_VERSION = 'v0.1'
|
||||
RABBITMQ_HOST = os.environ.get("RABBITMQ_HOST", "localhost")
|
||||
|
||||
|
||||
class ProdConfig(Config):
|
||||
|
||||
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
3
rmq_helper/__init__.py
Normal file
3
rmq_helper/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""RabbitMQ helpers"""
|
||||
from .exchanges import ensure_exchange_exists, exchange_publish
|
||||
from .queues import ensure_queue_exists, queue_publish
|
||||
31
rmq_helper/exchanges.py
Normal file
31
rmq_helper/exchanges.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import json
|
||||
|
||||
from pika.adapters.blocking_connection import BlockingChannel
|
||||
|
||||
from .extension import with_pika_channel
|
||||
|
||||
|
||||
@with_pika_channel
|
||||
def ensure_exchange_exists(name: str, channel: BlockingChannel = None) -> None:
|
||||
"""Ensures exchange exists
|
||||
|
||||
:param name: the name of the exchange
|
||||
:param channel: a pika channel, if none specified a new one will be created
|
||||
"""
|
||||
channel.exchange_declare(exchange=name, exchange_type='topic')
|
||||
|
||||
|
||||
@with_pika_channel
|
||||
def exchange_publish(exchange_name: str, topic: str, message, channel: BlockingChannel = None):
|
||||
"""Publishes a message to a
|
||||
|
||||
:param exchange_name: the name of the exchange
|
||||
:param topic: the topic to publish to
|
||||
:param message: the message to publish (will be turned into a json)
|
||||
:param channel: a pika channel, if none specified a new one will be created
|
||||
"""
|
||||
ensure_exchange_exists(exchange_name, channel=channel)
|
||||
|
||||
body = json.dumps(message)
|
||||
body_bytes = body.encode('utf-8')
|
||||
channel.basic_publish(exchange=exchange_name, routing_key=topic, body=body_bytes)
|
||||
25
rmq_helper/extension.py
Normal file
25
rmq_helper/extension.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from functools import wraps
|
||||
|
||||
import pika
|
||||
|
||||
from .settings import RABBITMQ_HOST
|
||||
|
||||
|
||||
def with_pika_channel(f):
|
||||
@wraps(f)
|
||||
def wrapper(*args, **kwargs):
|
||||
# if we are already passing channel, just reuse it
|
||||
if 'channel' in kwargs:
|
||||
return f(*args, **kwargs)
|
||||
|
||||
# otherwise set up a new connection and channel to wrap our code with
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host=RABBITMQ_HOST))
|
||||
channel = connection.channel()
|
||||
|
||||
ret = f(*args, **kwargs, channel=channel)
|
||||
|
||||
channel.close()
|
||||
return ret
|
||||
|
||||
return wrapper
|
||||
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)
|
||||
3
rmq_helper/settings.py
Normal file
3
rmq_helper/settings.py
Normal file
@@ -0,0 +1,3 @@
|
||||
import os
|
||||
|
||||
RABBITMQ_HOST = os.environ.get("RABBITMQ_HOST", "localhost")
|
||||
Reference in New Issue
Block a user