Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Communications Backbone System
communications-backbone
Commits
4408fd96
Commit
4408fd96
authored
2 years ago
by
Trishna Saeharaseelan
Browse files
Options
Download
Email Patches
Plain Diff
feat(wrappers): add wrappers for rabbitmq
parent
1352b3a1
Changes
23
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
112 additions
and
32 deletions
+112
-32
workers/core_subscribe.py
workers/core_subscribe.py
+0
-26
wrappers/__init__.py
wrappers/__init__.py
+9
-6
wrappers/rabbit_mq.py
wrappers/rabbit_mq.py
+103
-0
No files found.
workers/core_subscribe.py
deleted
100644 → 0
View file @
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
)
This diff is collapsed.
Click to expand it.
wrappers/__init__.py
View file @
4408fd96
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",
]
This diff is collapsed.
Click to expand it.
wrappers/rabbit_mq.py
View file @
4408fd96
#!/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"
)
This diff is collapsed.
Click to expand it.
Prev
1
2
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment