-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathworker.py
More file actions
421 lines (362 loc) · 14.5 KB
/
worker.py
File metadata and controls
421 lines (362 loc) · 14.5 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import fnmatch
import importlib
import logging
import os
import signal
import sys
import traceback
import time
from multiprocessing import Event, Process, Queue
try:
from queue import Empty, Full
except ImportError:
from Queue import Empty, Full
try:
from inspect import getfullargspec as get_args
except ImportError:
from inspect import getargspec as get_args
import boto3
from pyqs.utils import get_aws_region_name, decode_message, TaskContext
MESSAGE_DOWNLOAD_BATCH_SIZE = 10
LONG_POLLING_INTERVAL = 20
logger = logging.getLogger("pyqs")
def get_conn(region=None, access_key_id=None, secret_access_key=None):
if not region:
region = get_aws_region_name()
return boto3.client(
"sqs",
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key, region_name=region,
)
class BaseWorker(Process):
def __init__(self, *args, **kwargs):
self.parent_id = kwargs.pop('parent_id')
super(BaseWorker, self).__init__(*args, **kwargs)
self.should_exit = Event()
def shutdown(self):
logger.info(
"Received shutdown signal, shutting down PID {}!".format(
os.getpid()))
self.should_exit.set()
def parent_is_alive(self):
if os.getppid() != self.parent_id:
logger.info(
"Parent process has gone away, exiting process {}!".format(
os.getpid()))
return False
return True
class ReadWorker(BaseWorker):
def __init__(self, queue_url, internal_queue, batchsize,
connection_args=None, *args, **kwargs):
super(ReadWorker, self).__init__(*args, **kwargs)
if connection_args is None:
connection_args = {}
self.connection_args = connection_args
self.conn = get_conn(**self.connection_args)
self.queue_url = queue_url
sqs_queue = self.conn.get_queue_attributes(
QueueUrl=queue_url, AttributeNames=['All'])['Attributes']
self.visibility_timeout = int(sqs_queue['VisibilityTimeout'])
self.internal_queue = internal_queue
self.batchsize = batchsize
def run(self):
# Set the child process to not receive any keyboard interrupts
signal.signal(signal.SIGINT, signal.SIG_IGN)
logger.info(
"Running ReadWorker: {}, pid: {}".format(
self.queue_url, os.getpid()))
while not self.should_exit.is_set() and self.parent_is_alive():
self.read_message()
self.internal_queue.close()
self.internal_queue.cancel_join_thread()
def read_message(self):
messages = self.conn.receive_message(
QueueUrl=self.queue_url,
MaxNumberOfMessages=self.batchsize,
WaitTimeSeconds=LONG_POLLING_INTERVAL,
AttributeNames=["ApproximateReceiveCount"]
).get('Messages', [])
logger.debug(
"Successfully got {} messages from SQS queue {}".format(
len(messages), self.queue_url)) # noqa
start = time.time()
for message in messages:
end = time.time()
if int(end - start) >= self.visibility_timeout:
# Don't add any more messages since they have
# re-appeared in the sqs queue Instead just reset and get
# fresh messages from the sqs queue
msg = (
"Clearing Local messages since we exceeded "
"their visibility_timeout"
)
logger.warning(msg)
break
message_body = decode_message(message)
try:
packed_message = {
"queue": self.queue_url,
"message": message,
"start_time": start,
"timeout": self.visibility_timeout,
}
self.internal_queue.put(
packed_message, True, self.visibility_timeout)
except Full:
msg = (
"Timed out trying to add the following message "
"to the internal queue after {} seconds: {}"
).format(self.visibility_timeout, message_body) # noqa
logger.warning(msg)
continue
else:
logger.debug(
"Message successfully added to internal queue "
"from SQS queue {} with body: {}".format(
self.queue_url, message_body)) # noqa
class ProcessWorker(BaseWorker):
def __init__(self, internal_queue, interval, connection_args=None, *args,
**kwargs):
super(ProcessWorker, self).__init__(*args, **kwargs)
if connection_args is None:
self.conn = get_conn()
else:
self.conn = get_conn(**connection_args)
self.internal_queue = internal_queue
self.interval = interval
self._messages_to_process_before_shutdown = 100
def run(self):
# Set the child process to not receive any keyboard interrupts
signal.signal(signal.SIGINT, signal.SIG_IGN)
logger.info("Running ProcessWorker, pid: {}".format(os.getpid()))
messages_processed = 0
while not self.should_exit.is_set() and self.parent_is_alive():
processed = self.process_message()
if processed:
messages_processed += 1
time.sleep(self.interval)
else:
# If we have no messages wait a moment before rechecking.
time.sleep(0.001)
if messages_processed >= self._messages_to_process_before_shutdown:
self.shutdown()
def process_message(self):
try:
packed_message = self.internal_queue.get(timeout=0.5)
except Empty:
# Return False if we did not attempt to process any messages
return False
message = packed_message['message']
queue_url = packed_message['queue']
fetch_time = packed_message['start_time']
timeout = packed_message['timeout']
message_body = decode_message(message)
full_task_path = message_body['task']
args = message_body['args']
kwargs = message_body['kwargs']
message_id = message['MessageId']
receipt_handle = message['ReceiptHandle']
approx_receive_count = int(message.get('Attributes', {}).get("ApproximateReceiveCount", 1))
task_name = full_task_path.split(".")[-1]
task_path = ".".join(full_task_path.split(".")[:-1])
task_module = importlib.import_module(task_path)
task = getattr(task_module, task_name)
# if the task accepts the optional _context argument, pass it the TaskContext
if '_context' in get_args(task).args:
kwargs = dict(kwargs)
kwargs['_context'] = TaskContext(
conn=self.conn,
queue_url=queue_url,
message_id=message_id,
receipt_handle=receipt_handle,
approx_receive_count=approx_receive_count
)
current_time = time.time()
if int(current_time - fetch_time) >= timeout:
logger.warning(
"Discarding task {} with args: {} and kwargs: {} due to "
"exceeding visibility timeout".format( # noqa
full_task_path,
repr(args),
repr(kwargs),
)
)
return True
try:
start_time = time.clock()
task(*args, **kwargs)
except Exception:
end_time = time.clock()
logger.exception(
"Task {} raised error in {:.4f} seconds: with args: {} "
"and kwargs: {}: {}".format(
full_task_path,
end_time - start_time,
args,
kwargs,
traceback.format_exc(),
)
)
# since the task failed, mark it is available again quickly (10 seconds)
self.conn.change_message_visibility(
QueueUrl=queue_url,
ReceiptHandle=receipt_handle,
VisibilityTimeout=10
)
return True
else:
end_time = time.clock()
self.conn.delete_message(
QueueUrl=queue_url,
ReceiptHandle=receipt_handle
)
logger.info(
"Processed task {} in {:.4f} seconds with args: {} "
"and kwargs: {}".format(
full_task_path,
end_time - start_time,
repr(args),
repr(kwargs),
)
)
return True
class ManagerWorker(object):
def __init__(self, queue_prefixes, worker_concurrency, interval, batchsize,
prefetch_multiplier=2, region=None, access_key_id=None,
secret_access_key=None):
self.connection_args = {
"region": region,
"access_key_id": access_key_id,
"secret_access_key": secret_access_key,
}
self.batchsize = batchsize
if batchsize > MESSAGE_DOWNLOAD_BATCH_SIZE:
self.batchsize = MESSAGE_DOWNLOAD_BATCH_SIZE
if batchsize <= 0:
self.batchsize = 1
self.interval = interval
self.prefetch_multiplier = prefetch_multiplier
self.load_queue_prefixes(queue_prefixes)
self.queue_urls = self.get_queue_urls_from_queue_prefixes(
self.queue_prefixes)
self.setup_internal_queue(worker_concurrency)
self.reader_children = []
self.worker_children = []
self._pid = os.getpid()
self._initialize_reader_children()
self._initialize_worker_children(worker_concurrency)
self._running = True
self._register_signals()
def _register_signals(self):
for SIG in [signal.SIGINT, signal.SIGTERM, signal.SIGQUIT,
signal.SIGHUP]:
self.register_shutdown_signal(SIG)
def _initialize_reader_children(self):
for queue_url in self.queue_urls:
self.reader_children.append(
ReadWorker(
queue_url, self.internal_queue, self.batchsize,
connection_args=self.connection_args,
parent_id=self._pid,
)
)
def _initialize_worker_children(self, number):
for index in range(number):
self.worker_children.append(
ProcessWorker(
self.internal_queue, self.interval,
connection_args=self.connection_args,
parent_id=self._pid,
)
)
def load_queue_prefixes(self, queue_prefixes):
self.queue_prefixes = queue_prefixes
logger.info("Loading Queues:")
for queue_prefix in queue_prefixes:
logger.info("[Queue]\t{}".format(queue_prefix))
def get_queue_urls_from_queue_prefixes(self, queue_prefixes):
conn = get_conn(**self.connection_args)
queue_urls = conn.list_queues().get('QueueUrls', [])
matching_urls = []
for prefix in queue_prefixes:
matching_urls.extend([
queue_url for queue_url in queue_urls if
fnmatch.fnmatch(queue_url.rsplit("/", 1)[1], prefix)
])
logger.info("Found matching SQS Queues: {}".format(matching_urls))
return matching_urls
def setup_internal_queue(self, worker_concurrency):
self.internal_queue = Queue(
worker_concurrency * self.prefetch_multiplier * self.batchsize)
def start(self):
for child in self.reader_children:
child.start()
for child in self.worker_children:
child.start()
def stop(self):
for child in self.reader_children:
child.shutdown()
for child in self.reader_children:
child.join()
for child in self.worker_children:
child.shutdown()
for child in self.worker_children:
child.join()
def sleep(self):
counter = 0
while self._running:
counter = counter + 1
if counter % 1000 == 0:
counter = 0
self.process_counts()
self.replace_workers()
time.sleep(0.001)
self._exit()
def register_shutdown_signal(self, SIG):
signal.signal(SIG, self._graceful_shutdown)
def _graceful_shutdown(self, signum, frame):
logger.info('Received shutdown signal %s', signum)
self._running = False
def _exit(self):
logger.info('Graceful shutdown. Sending shutdown signal to children.')
self.stop()
sys.exit(0)
def process_counts(self):
reader_count = sum(map(lambda x: x.is_alive(), self.reader_children))
worker_count = sum(map(lambda x: x.is_alive(), self.worker_children))
logger.debug("Reader Processes: {}".format(reader_count))
logger.debug("Worker Processes: {}".format(worker_count))
def replace_workers(self):
self._replace_reader_children()
self._replace_worker_children()
def _replace_reader_children(self):
for index, reader in enumerate(self.reader_children):
if not reader.is_alive():
logger.info(
"Reader Process {} is no longer responding, "
"spawning a new reader.".format(reader.pid))
queue_url = reader.queue_url
self.reader_children.pop(index)
worker = ReadWorker(
queue_url, self.internal_queue, self.batchsize,
connection_args=self.connection_args,
parent_id=self._pid,
)
worker.start()
self.reader_children.append(worker)
def _replace_worker_children(self):
for index, worker in enumerate(self.worker_children):
if not worker.is_alive():
logger.info(
"Worker Process {} is no longer responding, "
"spawning a new worker.".format(worker.pid))
self.worker_children.pop(index)
worker = ProcessWorker(
self.internal_queue, self.interval,
connection_args=self.connection_args,
parent_id=self._pid,
)
worker.start()
self.worker_children.append(worker)