-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathQueueClient.py
More file actions
79 lines (66 loc) · 2.69 KB
/
QueueClient.py
File metadata and controls
79 lines (66 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import pika
from pika.exchange_type import ExchangeType
import json
from enum import Enum
from shared.settings import settings
import ssl
class QueueNames(Enum):
action_triggers = 'actions.triggers'
events_new = 'event.new'
job_tasks = 'job.tasks'
scheduler_tasks = 'scheduler.tasks'
class Exchanges(Enum):
actions = 'actions'
events = 'events'
exports = 'exports'
jobs = 'jobs'
scheduler = 'scheduler'
class RoutingKeys(Enum):
action_trigger_event_new = 'actions.new_actions_trigger'
event_new = 'events.new'
job_add_task = 'job.add_task'
scheduler = 'scheduler.all'
class QueueClient:
def __init__(self):
if settings.DIFFGRAM_SYSTEM_MODE == 'testing':
return
ssl_options = None
if settings.RABBITMQ_USE_SSL:
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
ssl_context.set_ciphers('ECDHE+AESGCM:!ECDSA')
ssl_options = pika.SSLOptions(context = ssl_context)
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(host = settings.RABBITMQ_HOST,
port = settings.RABBITMQ_PORT,
ssl_options = ssl_options,
heartbeat = 10,
credentials = pika.PlainCredentials(settings.RABBITMQ_DEFAULT_USER,
settings.RABBITMQ_DEFAULT_PASS))
)
self.main_channel = self.connection.channel()
self.main_channel.exchange_declare(exchange = Exchanges.actions.value,
exchange_type = ExchangeType.direct.value)
self.main_channel.exchange_declare(
exchange = Exchanges.events.value,
exchange_type = ExchangeType.direct.value)
self.main_channel.exchange_declare(
exchange = Exchanges.exports.value,
exchange_type = ExchangeType.direct.value)
def send_message(self,
message: dict,
routing_key: str,
exchange: str):
"""
Publishes a message to rabbit MQ. For now its using the default
actions exchange. But we can modify this wrapper to include more paremeters
for the exchange name.
:param message:
:param routing_key:
:param exchange:
:return:
"""
self.main_channel.basic_publish(
exchange = exchange,
routing_key = routing_key,
body = json.dumps(message).encode('utf-8'),
properties = pika.BasicProperties(content_type = 'application/json'))