Commit 4408fd96 authored by Trishna Saeharaseelan's avatar Trishna Saeharaseelan
Browse files

feat(wrappers): add wrappers for rabbitmq

parent 1352b3a1
#!/usr/bin/env python
import pika
def get_connection():
return pika.BlockingConnection(pika.ConnectionParameters(host="localhost"))
def subscribe(
queue_name,
topic,
publish_exchange="soar_publish",
broadcast_exchange="soar_broadcast",
):
adm_connection = get_connection()
admin_channel = adm_connection.channel()
admin_channel.exchange_declare(exchange=broadcast_exchange, exchange_type="fanout")
admin_channel.queue_bind(exchange=broadcast_exchange, queue=queue_name)
sub_connection = get_connection()
subscriber_channel = sub_connection.channel()
subscriber_channel.exchange_declare(
exchange=publish_exchange, exchange_type="topic"
)
subscriber_channel.queue_bind(
exchange=publish_exchange, queue=queue_name, routing_key=topic
)
import os
# import os
# __all__ = [
# os.path.splitext(os.path.basename(x))[0]
# for x in os.listdir(os.path.dirname(__file__))
# if x.endswith(".py") and x != "__init__.py"
# ]
__all__ = [
"rabbit_mq",
"zero_mq",
"kombu",
]
# "zero_mq",
# "kombu",
]
#!/usr/bin/env python
import pika
# import sys
# import os
# import json
def get_connection(host="localhost"):
return pika.BlockingConnection(pika.ConnectionParameters(host=host))
def callback(channel, method, properties, body):
# message = json.loads(body.decode())
# topic = message["topic"]
# deliver(body, topic, publish_exchange)
# channel.basic_ack(delivery_tag=method.delivery_tag)
print(" [x] %r:%r" % (method.routing_key, body))
def publish(
host="localhost",
queue_name=None,
exchange_name=None,
exchange_type="topic",
routing_key=None,
message="Testing Publish",
):
"""
Publishes message to specific channels only.
<>
Ref: https://www.rabbitmq.com/tutorials/tutorial-one-python.html
Args:
host:
queue_name:,
exchange_name:
exchange_type(optional):
routing_key(optional): (applicable if not `fanout` exchange_type)
message:
Returns:
"""
connection = get_connection(host)
channel = connection.channel()
channel.exchange_declare(
exchange=exchange_name,
exchange_type=exchange_type,
)
message = message.encode(encoding="UTF-8")
channel.basic_publish(
exchange=exchange_name,
routing_key=routing_key,
body=message,
)
print("Published %r on exchange %r" % (message, exchange_name))
connection.close()
def subscribe(
host,
queue_name="",
publish_exchange="soar_publish",
broadcast_exchange="soar_broadcast",
topics_list=[],
):
def enable_subscription(
exchange,
# queue_name,
current_exchange_type,
subscription_routing_key=None,
):
connection = get_connection(host)
subscriber_channel = connection.channel()
subscriber_channel.exchange_declare(
exchange=exchange, exchange_type=current_exchange_type
)
# TODO: Discuss> should the queue names be different?
# (do we randomize the naming?)
result = subscriber_channel.queue_declare(queue=queue_name)
queue = result.method.queue
subscriber_channel.queue_bind(
exchange=publish_exchange,
queue=queue,
routing_key=subscription_routing_key,
)
print(" [*] Waiting for logs. To exit press CTRL+C")
subscriber_channel.basic_consume(
queue=queue, on_message_callback=callback, auto_ack=True
)
subscriber_channel.start_consuming()
# Subsribe to list of topics (publish type)
for topic in topics_list:
enable_subscription(publish_exchange, "topic", topic)
# Subsribe to all messages (broadcast type)
enable_subscription(broadcast_exchange, "fanout")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment