forked from martinez099/redismq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_queue_client.py
371 lines (299 loc) · 11 KB
/
message_queue_client.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import json
import logging
import os
import threading
import uuid
import grpc
from message_queue_pb2 import SendRequest, ReceiveRequest, GetRequest, AcknowledgeRequest
from message_queue_pb2_grpc import MessageQueueStub
def send_message(service_name, func_name, params=None, rsp_to=1):
"""
Put a message into a queue and wait for the response.
:param service_name: The name of the service.
:param func_name: The name of the function.
:param params: The parameters.
:param rsp_to: An optional timeout (in seconds) to wait for the response, defaults to 1.
:return: The payload of the response.
"""
logging.debug(f'sending message to {service_name}.{func_name} ...')
msg_id = MQ.send_msg(service_name, func_name, json.dumps({
"params": params,
}))
logging.debug(f'receiving response from {service_name}.{func_name} ...')
rsp = MQ.recv_rsp(service_name, func_name, msg_id, rsp_to)
if not rsp:
raise TimeoutError('waiting for response from {}.{}'.format(service_name, func_name))
MQ.ack_rsp(service_name, func_name, msg_id, rsp)
return json.loads(rsp)
def send_message_async(service_name, func_name, params=None, rsp_handler=None, rsp_timeout=1):
"""
Put a message into a queue and return immediately.
:param service_name: The name of the service.
:param func_name: The name of the function.
:param params: Optional parameters.
:param rsp_handler: An optional callback handler.
:param rsp_timeout: Optional timeout in seconds, defaults to 1.
:return: The message ID.
"""
logging.debug(f'sending async message to {service_name}.{func_name} ...')
msg_id = MQ.send_msg(service_name, func_name, json.dumps({
"params": params,
}))
if rsp_handler:
Waiter(service_name, func_name, msg_id, rsp_handler, rsp_timeout).start()
return msg_id
class Waiter(threading.Thread):
"""
Waiter Thread class.
"""
def __init__(self, _service_name, _func_name, _msg_id, _handler, _timeout):
"""
:param _service_name:
:param _func_name:
:param _msg_id:
:param _handler:
:param _timeout:
"""
super(Waiter, self).__init__()
self.running = False
self.timeout = _timeout
self.service_name = _service_name
self.func_name = _func_name
self.msg_id = _msg_id
self.handler = _handler
def run(self):
"""
Wait for a response to receive, then call handler function with awaited result.
"""
if self.running:
return
self.running = True
response = MQ.recv_rsp(
ReceiveRequest(
service_name=self.service_name,
func_name=self.func_name,
msg_id=self.msg_id,
timeout=self.timeout
))
if not response:
self.running = False
return
try:
self.handler(response.payload)
except Exception as e:
logging.error(
'error calling handler function ({}) for {}.{}: {}'.format(
e.__class__.__name__, self.service_name, self.func_name, str(e)
)
)
self.running = False
class Consumers(object):
"""
Consumers class.
Helper class to handle multiple consumers.
"""
def __init__(self, service_name, handler_funcs, timeout=1):
"""
:param service_name: A service name.
:param handler_funcs: A list of handler functions.
:param timeout: An optional timeout in seconds, defaults to 1.
"""
self.service_name = service_name
self.handler_funcs = handler_funcs
self.timeout = timeout
self.threads = [threading.Thread(target=self._run,
name='{}.{}'.format(service_name, m.__name__),
args=(m, )) for m in handler_funcs]
self.running = False
def start(self):
"""
Spawn a consumer thread for all handler functions.
:return: None
"""
self.running = True
[t.start() for t in self.threads]
return self
def wait(self):
"""
Wait for all consumer threads to finsih. N.B. This is a blocking operation.
:return: None
"""
[t.join() for t in self.threads]
return self
def stop(self):
"""
Stop all consumer threads.
:return: None
"""
self.running = False
return self
def _run(self, handler_func):
while self.running:
logging.debug(f'receiving message in {self.service_name}.{handler_func.__name__} ...')
msg_id, msg_payload = MQ.recv_msg(self.service_name, handler_func.__name__, self.timeout)
if not msg_payload:
continue
try:
payload = json.loads(msg_payload)
params = payload['params']
except Exception as e:
rsp = {
"error": "Error parsing received payload ({}): {}".format(e.__class__.__name__, str(e))
}
else:
logging.debug(f'calling handler function in {self.service_name}.{handler_func.__name__} ...')
try:
rsp = handler_func(params)
except Exception as e:
rsp = None
logging.error(
"error calling handler function ({}) in {}.{}: {}".format(
e.__class__.__name__, self.service_name, handler_func.__name__, str(e)
)
)
MQ.ack_msg(self.service_name, handler_func.__name__, msg_id)
if not rsp:
continue
logging.debug(f'sending response back from {self.service_name}.{handler_func.__name__} ...')
MQ.send_rsp(self.service_name, handler_func.__name__, msg_id, json.dumps(rsp))
class MessageQueue(object):
"""
Message Queue class.
"""
def __init__(self):
host, port = os.getenv('MESSAGE_QUEUE_HOSTNAME', 'localhost'), os.getenv('MESSAGE_QUEUE_PORTNR', '50051')
self.channel = grpc.insecure_channel('{}:{}'.format(host, port))
self.stub = MessageQueueStub(self.channel)
self.subscribers = {}
def __del__(self):
self.channel.close()
def send_msg(self, service_name, func_name, payload, msg_id=None):
"""
Send a message.
:param service_name: The remote service to call.
:param func_name: The remote function to call.
:param payload: The message payload.
:param msg_id: The ID of the message, may be None.
:return: The message ID.
"""
response = self.stub.send_msg(
SendRequest(
service_name=service_name,
func_name=func_name,
msg_id=msg_id or str(uuid.uuid4()),
payload=payload,
))
return response.msg_id
def recv_rsp(self, service_name, func_name, msg_id, timeout):
"""
Receive a response. This is a blocking function.
:param service_name: The remote service to call.
:param func_name: The remote function to call.
:param msg_id: The message ID, may be None.
:param timeout: The timeout in seconds.
:return: The payload of the response.
"""
response = self.stub.recv_rsp(
ReceiveRequest(
service_name=service_name,
func_name=func_name,
msg_id=msg_id,
timeout=timeout
))
return response.payload
def get_rsp(self, service_name, func_name, msg_id):
"""
Get a response. This is a non-blocking function.
:param service_name: The remote service to call.
:param func_name: The remote function to call.
:param msg_id: The message ID, may be None.
:return: The payload of the response.
"""
response = self.stub.get_rsp(
GetRequest(
service_name=service_name,
func_name=func_name,
msg_id=msg_id,
))
return response.payload
def ack_rsp(self, service_name, func_name, msg_id, payload):
"""
Acknowledge a response.
:param service_name: The remote service to call.
:param func_name: The remote function to call.
:param msg_id: The message ID.
:param payload: The payload of the response.
:return: Success.
"""
response = self.stub.ack_rsp(
AcknowledgeRequest(
service_name=service_name,
func_name=func_name,
msg_id=msg_id,
payload=payload,
))
return response.success
def recv_msg(self, service_name, func_name, timeout=0):
"""
Receive a message.
:param service_name: The remote service to call.
:param func_name: The remote function to call.
:param timeout: The timeout in seconds.
:return: The message payload.
"""
response = self.stub.recv_msg(
ReceiveRequest(
service_name=service_name,
func_name=func_name,
msg_id=None,
timeout=timeout
))
return response.msg_id, response.payload
def get_msg(self, service_name, func_name):
"""
Get a message.
:param service_name: The remote service to call.
:param func_name: The remote function to call.
:return: The message ID and payload.
"""
response = self.stub.get_msg(
GetRequest(
service_name=service_name,
func_name=func_name,
msg_id=None,
))
return response.msg_id, response.payload
def ack_msg(self, service_name, func_name, msg_id):
"""
Acknowledge a message.
:param service_name: The remote service to call.
:param func_name: The remote function to call.
:param msg_id: The message ID.
:return: Success.
"""
response = self.stub.ack_msg(
AcknowledgeRequest(
service_name=service_name,
func_name=func_name,
msg_id=msg_id,
payload=None,
))
return response.success
def send_rsp(self, service_name, func_name, msg_id, payload):
"""
Send a response.
:param service_name: The remote service to call.
:param func_name: The remote function to call.
:param msg_id: The message ID.
:param payload: The payload of the response.
:return: Success.
"""
response = self.stub.send_rsp(
SendRequest(
service_name=service_name,
func_name=func_name,
msg_id=msg_id,
payload=payload,
))
return response.msg_id
MQ = MessageQueue()