This repository was archived by the owner on Nov 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathsource_reader.py
More file actions
611 lines (525 loc) · 20.6 KB
/
Copy pathsource_reader.py
File metadata and controls
611 lines (525 loc) · 20.6 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#!/usr/bin/python
"""A long-running process that receives requests to read data from various ContentSources,
and reads at reasonable intervals. It can also handle sources that can push changes. """
from __future__ import print_function
import sys
import signal
from random import uniform
from time import sleep
from threading import Thread, Event, currentThread
from traceback import print_stack
from datetime import datetime, timedelta
from abc import ABCMeta, abstractmethod
from logging.config import fileConfig
from pyramid.paster import get_appsettings
from zope.component import getGlobalSiteManager
from kombu import BrokerConnection, Exchange, Queue
from kombu.mixins import ConsumerMixin
from kombu.utils.debug import setup_logging
from sqlalchemy import inspect
from sqlalchemy.exc import TimeoutError
from ..lib import logging
from ..lib.sentry import capture_exception
from ..lib.config import set_config
from ..lib.enum import OrderedEnum
from ..lib.sqla import configure_engine
from . import configure
log = logging.getLogger()
pool_counter = 0
class ReaderStatus(OrderedEnum):
# See doc/sourcereader.dot
CREATED = 0
READING = 1
WAIT_FOR_PUSH = 2 # A state where new data will come without prompting
PAUSED = 3 # A state where new data will come when prompted
CLOSED = 4
SHUTDOWN = 5
TRANSIENT_ERROR = 10 # Try again later (same connection)
CLIENT_ERROR = 11 # Make a new connection to re-try
IRRECOVERABLE_ERROR = 12 # This server will never work.
known_transitions = {
ReaderStatus.CREATED: {
ReaderStatus.READING,
ReaderStatus.CLOSED,
ReaderStatus.CLIENT_ERROR,
ReaderStatus.IRRECOVERABLE_ERROR,
},
ReaderStatus.READING: {
ReaderStatus.CLIENT_ERROR,
ReaderStatus.READING,
ReaderStatus.IRRECOVERABLE_ERROR,
ReaderStatus.PAUSED,
ReaderStatus.TRANSIENT_ERROR,
ReaderStatus.WAIT_FOR_PUSH,
ReaderStatus.CLOSED,
ReaderStatus.SHUTDOWN, # Should this not go to closed first?
},
ReaderStatus.PAUSED: {
ReaderStatus.CLOSED,
ReaderStatus.READING,
ReaderStatus.WAIT_FOR_PUSH,
},
ReaderStatus.CLIENT_ERROR: {
ReaderStatus.SHUTDOWN,
ReaderStatus.CLIENT_ERROR,
ReaderStatus.IRRECOVERABLE_ERROR,
ReaderStatus.READING,
},
ReaderStatus.CLOSED: {
ReaderStatus.CLIENT_ERROR,
ReaderStatus.SHUTDOWN,
},
ReaderStatus.IRRECOVERABLE_ERROR: {
ReaderStatus.IRRECOVERABLE_ERROR,
ReaderStatus.READING,
ReaderStatus.SHUTDOWN,
},
ReaderStatus.TRANSIENT_ERROR: {
ReaderStatus.READING,
},
ReaderStatus.WAIT_FOR_PUSH: {
ReaderStatus.CLIENT_ERROR,
ReaderStatus.CLOSED,
ReaderStatus.PAUSED,
ReaderStatus.READING,
ReaderStatus.TRANSIENT_ERROR,
ReaderStatus.IRRECOVERABLE_ERROR,
ReaderStatus.WAIT_FOR_PUSH,
},
ReaderStatus.SHUTDOWN: {
ReaderStatus.SHUTDOWN,
},
}
disconnected_states = set((
ReaderStatus.CLIENT_ERROR, ReaderStatus.IRRECOVERABLE_ERROR,
ReaderStatus.CLOSED, ReaderStatus.SHUTDOWN))
# Connection constants
QUEUE_NAME = "source_reader"
ROUTING_KEY = QUEUE_NAME
class ReaderError(RuntimeError):
status = ReaderStatus.TRANSIENT_ERROR
pass
class ClientError(ReaderError):
status = ReaderStatus.CLIENT_ERROR
pass
class IrrecoverableError(ClientError):
status = ReaderStatus.IRRECOVERABLE_ERROR
pass
class ReadingForTooLong(ClientError):
pass
class SourceReader(Thread):
""" """
__metaclass__ = ABCMeta
deamon = True
# Timings. Those should vary per source type, maybe even by source?
min_time_between_reads = timedelta(minutes=1)
time_between_reads = timedelta(minutes=10)
max_idle_period = timedelta(hours=3)
transient_error_backoff = timedelta(seconds=10)
transient_error_numlimit = 10
client_error_backoff = timedelta(minutes=15)
client_error_numlimit = 3
irrecoverable_error_backoff = timedelta(days=1)
reading_takes_too_long = timedelta(minutes=15)
def __init__(self, source_id):
super(SourceReader, self).__init__()
self.source_id = source_id
self.source = None
self.status = ReaderStatus.CREATED
self.last_prod = datetime.utcnow()
self.last_read_started = datetime.fromtimestamp(0)
self.last_read = datetime.fromtimestamp(0)
self.last_successful_login = datetime.fromtimestamp(0)
self.last_error_status = None
self.reimporting = False
self.can_push = False # Set to true for, eg, imap with polling.
self.event = Event()
def set_status(self, status):
lvl = logging.INFO if status in known_transitions[self.status] else logging.ERROR
log.log(lvl, "%s %d: %s -> %s" % (
self.__class__.__name__, self.source_id, self.status.name,
status.name))
if status == ReaderStatus.READING and self.status != ReaderStatus.READING:
self.source.connection_error = self.status.value
self.source.db.commit()
self.refresh_source()
self.status = status
def successful_login(self):
self.last_successful_login = datetime.utcnow()
self.source.db.commit()
self.after_login = True
def successful_read(self):
self.last_read = datetime.utcnow()
self.reset_errors()
self.reimporting = False
self.source.db.commit()
self.refresh_source()
def reset_errors(self):
self.error_count = 0
self.last_error_status = None
self.error_backoff_until = None
self.source.reset_errors()
def new_error(self, reader_error, status=None, expected=True):
import traceback
log.error(traceback.format_exc())
if not expected:
capture_exception()
status = status or reader_error.status
if status != self.last_error_status:
# Counter-intuitive, but either lighter or more severe errors
# reset the count.
self.error_count = 1
elif status == self.last_error_status:
self.error_count += 1
# escalate errors with repetition
if status == ReaderStatus.TRANSIENT_ERROR:
if self.error_count > self.transient_error_numlimit:
status = ReaderStatus.CLIENT_ERROR
self.error_count = 1
elif status == ReaderStatus.CLIENT_ERROR:
if self.error_count > self.client_error_numlimit:
status = ReaderStatus.IRRECOVERABLE_ERROR
self.error_count = 1
else:
assert False
if status == ReaderStatus.TRANSIENT_ERROR:
error_backoff = self.transient_error_backoff
elif status == ReaderStatus.CLIENT_ERROR:
error_backoff = self.client_error_backoff
elif status == ReaderStatus.IRRECOVERABLE_ERROR:
error_backoff = self.irrecoverable_error_backoff
else:
assert False
# double backoff every time
error_backoff *= 2 ** (self.error_count - 1)
self.last_error_status = status
self.source.db.rollback()
self.refresh_source()
self.source.connection_error = status.value
self.source.error_description = str(reader_error)
if (status > ReaderStatus.TRANSIENT_ERROR and
self.status != ReaderStatus.SHUTDOWN):
self.set_status(status)
self.reimporting = False
self.error_backoff_until = datetime.utcnow() + error_backoff
self.source.error_backoff_until = self.error_backoff_until
self.source.db.commit()
self.refresh_source()
def refresh_source(self):
# after a commit, refresh the source so it's still usable
self.source.db.query(
self.source.__class__
).populate_existing().get(self.source.id)
def is_in_error(self):
return self.last_error_status is not None
def is_connected(self):
return self.status not in disconnected_states
def setup_read(self, reimport, **kwargs):
self.reimporting = reimport
self.extra_args = kwargs
def handle_new_content(self, content):
from .translate import translate_content
translate_content(content) # should delay
def wake(self):
print("SourceReader.wake")
if self.status in (ReaderStatus.PAUSED, ReaderStatus.CLOSED) and (
datetime.utcnow() - max(self.last_prod, self.last_read) >
self.min_time_between_reads):
self.event.set()
elif self.status == ReaderStatus.TRANSIENT_ERROR and (
datetime.utcnow() - max(self.last_prod, self.last_error_status) >
self.transient_error_backoff):
# Exception: transient backoff escalation can be cancelled by wake
self.event.set()
elif (self.status == ReaderStatus.READING and (
datetime.utcnow() - self.last_read_started) >
self.reading_takes_too_long):
try:
self.do_close()
self.new_error(ReadingForTooLong())
except ReaderError as e:
self.new_error(e)
self.last_prod = datetime.utcnow()
def run(self):
self.setup()
while self.status not in (
ReaderStatus.SHUTDOWN, ReaderStatus.IRRECOVERABLE_ERROR):
if self.error_backoff_until:
interval = (self.error_backoff_until - datetime.utcnow()).total_seconds()
if interval > 0:
self.event.wait(interval)
self.event.clear()
try:
self.login()
self.successful_login()
except ReaderError as e:
self.new_error(e)
if self.status > ReaderStatus.TRANSIENT_ERROR:
self.try_close()
continue
except Exception as e:
self.new_error(e, ReaderStatus.CLIENT_ERROR, expected=False)
self.try_close()
break
while self.after_login or self.is_connected():
if self.status == ReaderStatus.SHUTDOWN:
break
self.after_login = False
# Read in all cases
try:
self.read()
except ReaderError as e:
self.new_error(e)
if self.status > ReaderStatus.TRANSIENT_ERROR:
self.try_close()
except Exception as e:
self.new_error(e, ReaderStatus.CLIENT_ERROR, expected=False)
self.try_close()
break
if not self.is_connected():
break
if self.can_push:
self.set_status(ReaderStatus.WAIT_FOR_PUSH)
while self.status == ReaderStatus.WAIT_FOR_PUSH:
try:
# This is not a final close, but sends it back to the QueuePool
self.source.db.close()
self.wait_for_push()
except ReaderError as e:
self.new_error(e)
if self.status > ReaderStatus.TRANSIENT_ERROR:
self.try_close()
else:
self.end_wait_for_push()
break
except Exception as e:
self.new_error(e, ReaderStatus.CLIENT_ERROR, expected=False)
self.try_close()
break
if not self.is_connected():
break
if self.status == ReaderStatus.READING:
self.set_status(ReaderStatus.WAIT_FOR_PUSH)
if self.status == ReaderStatus.PAUSED:
# If wait_for_push leaves us in PAUSED state,
# restart reading cycle
break
if not self.is_connected():
break
continue # to next read cycle
if not self.is_connected():
break
if (self.last_read - self.last_prod >
self.max_idle_period):
# Nobody cares, I can stop reading
try:
if self.status == ReaderStatus.WAIT_FOR_PUSH:
self.end_wait_for_push()
finally:
self.close()
if self.status != ReaderStatus.SHUTDOWN:
self.event.wait(0)
self.event.clear()
else:
self.event.wait(self.time_between_reads.total_seconds())
self.event.clear()
if self.source and not inspect(self.source).detached:
self.source.db.close()
@abstractmethod
def login(self):
pass
@abstractmethod
def wait_for_push(self):
# redefine in push-capable readers
assert self.can_push
# Leave a non-error status as either WAIT_FOR_PAUSE
# or READING; the latter will loop.
@abstractmethod
def end_wait_for_push(self):
# redefine in push-capable readers
if (self.status in (ReaderStatus.READING, ReaderStatus.WAIT_FOR_PUSH)):
self.set_status(ReaderStatus.PAUSED)
self.source.db.close()
def close(self):
if self.status == ReaderStatus.WAIT_FOR_PUSH:
try:
self.end_wait_for_push()
except ReaderError as e:
self.new_error(e, min(e.status, ReaderStatus.CLIENT_ERROR))
self.set_status(ReaderStatus.CLOSED)
try:
self.do_close()
except ReaderError as e:
self.new_error(e, min(e.status, ReaderStatus.CLIENT_ERROR))
finally:
self.set_status(ReaderStatus.SHUTDOWN)
self.source.db.close()
def try_close(self):
try:
self.do_close()
except ReaderError as e:
self.new_error(e, min(e.status, ReaderStatus.CLIENT_ERROR))
@abstractmethod
def do_close(self):
pass
def setup(self):
from ..models import ContentSource
backoff = 0.5 + uniform(0, 0.5)
while self.source is None:
try:
self.source = ContentSource.get(self.source_id)
except TimeoutError:
# Ran out of connection pool
log.error("TimeoutError for " + self.source_id)
sleep(backoff)
backoff *= 2
connection_error = (
ReaderStatus(self.source.connection_error)
if self.source.connection_error else None)
self.error_backoff_until = self.source.error_backoff_until
if connection_error:
self.status = connection_error
def read(self):
self.set_status(ReaderStatus.READING)
self.last_read_started = datetime.utcnow()
self.do_read()
self.successful_read()
if (self.status in (ReaderStatus.READING, ReaderStatus.WAIT_FOR_PUSH)):
self.set_status(ReaderStatus.PAUSED) # or WAIT_FOR_PUSH
@abstractmethod
def do_read(self):
pass
def shutdown(self):
# TODO: lock.
if self.is_connected():
self.close()
self.set_status(ReaderStatus.SHUTDOWN)
self.event.set()
def __repr__(self):
return "<%s.%d in %s>" % (
self.__class__.__name__, self.source_id, self._Thread__name)
class PullSourceReader(SourceReader):
# Simple reader, no wait for push, just redefine do_read
def login(self):
pass
def wait_for_push(self):
assert False, "This reader cannot wait for push"
def end_wait_for_push(self):
assert False, "This reader cannot wait for push"
def do_close(self):
pass
# Kombu communication. Does not work yet.
_exchange = Exchange(QUEUE_NAME)
_queue = Queue(
QUEUE_NAME, exchange=_exchange, routing_key=ROUTING_KEY)
_producer_connection = None
def wake(source_id, reimport=False, force_restart=False, **kwargs):
global _producer_connection
from kombu.common import maybe_declare
from kombu.pools import producers
with producers[_producer_connection].acquire(block=True) as producer:
maybe_declare(_exchange, producer.channel)
kwargs.update(dict(source=source_id, reimport=reimport, force_restart=force_restart))
producer.publish(kwargs, serializer="json", routing_key=ROUTING_KEY)
def external_shutdown():
global _producer_connection
from kombu.common import maybe_declare
from kombu.pools import producers
with producers[_producer_connection].acquire(block=True) as producer:
maybe_declare(_exchange, producer.channel)
producer.publish(
{"shutdown": True}, serializer="json", routing_key=ROUTING_KEY)
class SourceDispatcher(ConsumerMixin):
def __init__(self, connection):
super(SourceDispatcher, self).__init__()
self.connection = connection
self.readers = {}
def get_consumers(self, Consumer, channel):
global _queue
return [Consumer(queues=(_queue,),
callbacks=[self.callback])]
def callback(self, body, message):
if isinstance(body, list):
message.ack()
return # old message
if body.get("shutdown", False):
self.shutdown()
else:
source_id = body.get("source", None)
if not source_id:
raise ValueError("source not defined in " + repr(body))
self.read(source_id, **body)
message.ack()
def read(self, source_id, reimport=False, force_restart=False, **kwargs):
from assembl.models import ContentSource
reader = self.readers.get(source_id, None)
if force_restart and reader is not None:
reader.shutdown()
reader = None
if not (reader and reader.is_connected()):
source = ContentSource.get(source_id)
if not source:
return False
if source.connection_error == ReaderStatus.IRRECOVERABLE_ERROR and not force_restart:
return False
if force_restart:
source.reset_errors()
reader = source.make_reader()
self.readers[source_id] = reader
if reader is None:
return False
reader.setup_read(reimport, **kwargs)
reader.start()
return True
if reader is None:
return False
# We know it is connected by now.
reader.setup_read(reimport, **kwargs)
reader.wake()
return True
def shutdown(self):
self.should_stop = True
for reader in self.readers.itervalues():
if reader is not None:
reader.shutdown()
def includeme(config):
global _producer_connection, _exchange
setup_logging(loglevel='DEBUG')
url = (config.registry.settings.get('celery_tasks.broker') or
config.registry.settings.get('celery_tasks.imap.broker'))
_producer_connection = BrokerConnection(url)
if __name__ == '__main__':
if len(sys.argv) != 2:
print("usage: python source_reader.py configuration.ini")
config_file_name = sys.argv[-1]
settings = get_appsettings(config_file_name, 'assembl')
registry = getGlobalSiteManager()
registry.settings = settings
set_config(settings)
fileConfig(config_file_name)
# set the basic session maker without zope or autoflush
engine = configure_engine(settings, False, autoflush=False, max_overflow=20)
# @event.listens_for(engine, "checkin")
def show_checkin(*args):
global pool_counter
pool_counter -= 1
print("checkin pool: %d in %s" % (pool_counter, currentThread()))
print_stack()
# @event.listens_for(engine, "checkout")
def show_checkout(*args):
global pool_counter
pool_counter += 1
print("checkout pool: %d in %s" % (pool_counter, currentThread()))
print_stack()
configure(registry, 'source_reader')
url = (settings.get('celery_tasks.broker') or
settings.get('celery_tasks.imap.broker'))
with BrokerConnection(url) as conn:
sourcedispatcher = SourceDispatcher(conn)
def shutdown(*args):
sourcedispatcher.shutdown()
signal.signal(signal.SIGTERM, shutdown)
try:
sourcedispatcher.run()
except KeyboardInterrupt:
shutdown()