forked from datalogics/server_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
442 lines (371 loc) · 14.7 KB
/
monitor.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
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
from nose.tools import set_trace
import datetime
import os
import logging
import time
import traceback
from sqlalchemy.sql.functions import func
from sqlalchemy.sql.expression import (
or_,
)
import log # This sets the appropriate log format and level.
from config import Configuration
from coverage import CoverageFailure
from model import (
get_one_or_create,
CoverageRecord,
Edition,
CustomListEntry,
Identifier,
LicensePool,
PresentationCalculationPolicy,
Subject,
Timestamp,
Work,
)
class Monitor(object):
ONE_MINUTE_AGO = datetime.timedelta(seconds=60)
ONE_YEAR_AGO = datetime.timedelta(seconds=60*60*24*365)
NEVER = object()
def __init__(
self, _db, name, interval_seconds=1*60,
default_start_time=None, keep_timestamp=True):
self._db = _db
self.service_name = name
self.interval_seconds = interval_seconds
self.stop_running = False
self.keep_timestamp = keep_timestamp
if not default_start_time:
default_start_time = (
datetime.datetime.utcnow() - self.ONE_MINUTE_AGO)
if default_start_time is self.NEVER:
default_start_time = None
self.default_start_time = default_start_time
@property
def log(self):
if not hasattr(self, '_log'):
self._log = logging.getLogger(self.service_name)
return self._log
def run(self):
if self.keep_timestamp:
self.timestamp, new = get_one_or_create(
self._db, Timestamp,
service=self.service_name,
create_method_kwargs=dict(
timestamp=self.default_start_time
)
)
start = self.timestamp.timestamp or self.default_start_time
else:
start = self.default_start_time
self.timestamp = None
while not self.stop_running:
cutoff = datetime.datetime.utcnow()
new_timestamp = self.run_once(start, cutoff) or cutoff
duration = datetime.datetime.utcnow() - cutoff
to_sleep = self.interval_seconds-duration.seconds-1
self.cleanup()
if self.keep_timestamp:
self.timestamp.timestamp = new_timestamp
self._db.commit()
# TODO: This could be a little nicer, but basically we now
# want monitors to run through once and then stop.
if True:
self.stop_running = True
elif to_sleep > 0:
self.log.debug("Sleeping for %.1f", to_sleep)
time.sleep(to_sleep)
start = new_timestamp
def run_once(self, start, cutoff):
raise NotImplementedError()
def cleanup(self):
pass
class IdentifierSweepMonitor(Monitor):
# The completion of each individual item should be logged at
# this log level.
COMPLETION_LOG_LEVEL = logging.INFO
def __init__(self, _db, name, interval_seconds=3600,
default_counter=0, batch_size=100):
super(IdentifierSweepMonitor, self).__init__(
_db, name, interval_seconds)
self.default_counter = default_counter
self.batch_size = batch_size
def run(self):
self.timestamp, new = get_one_or_create(
self._db, Timestamp,
service=self.service_name,
create_method_kwargs=dict(
counter=self.default_counter
)
)
offset = self.timestamp.counter or self.default_counter
started_at = datetime.datetime.utcnow()
while not self.stop_running:
a = time.time()
old_offset = offset
try:
new_offset = self.run_once(offset)
except Exception, e:
self.log.error("Error during run: %s", e, exc_info=e)
break
to_sleep = 0
if new_offset == 0:
# We completed a sweep. We're done.
self.stop_running = True
self.cleanup()
self.counter = new_offset
self.timestamp.counter = self.counter
self._db.commit()
if old_offset != new_offset:
self.log.debug("Old offset: %s" % offset)
self.log.debug("New offset: %s", new_offset)
b = time.time()
self.log.debug("Elapsed: %.2f sec" % (b-a))
if to_sleep > 0:
if old_offset != new_offset:
self.log.debug("Sleeping for %.1f", to_sleep)
time.sleep(to_sleep)
offset = new_offset
def run_once(self, offset):
q = self.identifier_query().filter(
Identifier.id > offset).order_by(
Identifier.id).limit(self.batch_size)
identifiers = q.all()
if identifiers:
self.process_batch(identifiers)
return identifiers[-1].id
else:
return 0
def identifier_query(self):
return self._db.query(Identifier)
def process_batch(self, identifiers):
for i in identifiers:
self.process_identifier(i)
def process_identifier(self, identifier):
raise NotImplementedError()
class SubjectSweepMonitor(IdentifierSweepMonitor):
def __init__(self, _db, name, subject_type=None, filter_string=None,
batch_size=500):
super(SubjectSweepMonitor, self).__init__(
_db, name, batch_size=batch_size
)
self.subject_type = subject_type
self.filter_string = filter_string
def run_once(self, offset):
q = self.subject_query().filter(
Subject.id > offset).order_by(
Subject.id).limit(self.batch_size)
subjects = q.all()
if subjects:
self.process_batch(subjects)
return subjects[-1].id
else:
return 0
def subject_query(self):
qu = self._db.query(Subject)
if self.subject_type:
qu = qu.filter(Subject.type==self.subject_type)
if self.filter_string:
filter_string = '%' + self.filter_string + '%'
or_clause = or_(
Subject.identifier.ilike(filter_string),
Subject.name.ilike(filter_string)
)
qu = qu.filter(or_clause)
return qu
class CustomListEntrySweepMonitor(IdentifierSweepMonitor):
def run_once(self, offset):
q = self.custom_list_entry_query().filter(
CustomListEntry.id > offset).order_by(
CustomListEntry.id).limit(self.batch_size)
entries = q.all()
if entries:
self.process_batch(entries)
return entries[-1].id
else:
return 0
def process_batch(self, entries):
for entry in entries:
self.process_entry(entry)
def custom_list_entry_query(self):
return self._db.query(CustomListEntry)
class EditionSweepMonitor(IdentifierSweepMonitor):
def run_once(self, offset):
if offset is None:
offset = 0
q = self.edition_query().filter(
Edition.id > offset).order_by(
Edition.id).limit(self.batch_size)
editions = q.all()
if editions:
self.process_batch(editions)
return editions[-1].id
else:
return 0
def edition_query(self):
return self._db.query(Edition)
def process_batch(self, batch):
for edition in batch:
self.process_edition(edition)
self.log.log(self.COMPLETION_LOG_LEVEL, "Completed %r", edition)
def process_edition(self, work):
raise NotImplementedError()
class WorkSweepMonitor(IdentifierSweepMonitor):
def run_once(self, offset):
if offset is None:
offset = 0
q = self.work_query().filter(
Work.id > offset).order_by(
Work.id).limit(self.batch_size)
works = q.all()
if works:
self.process_batch(works)
return works[-1].id
else:
return 0
def work_query(self):
return self._db.query(Work)
def process_batch(self, batch):
for work in batch:
self.process_work(work)
self.log.log(self.COMPLETION_LOG_LEVEL, "Completed %r", work)
def process_work(self, work):
raise NotImplementedError()
class PresentationReadyWorkSweepMonitor(WorkSweepMonitor):
def work_query(self):
return self._db.query(Work).filter(Work.presentation_ready==True)
class OPDSEntryCacheMonitor(PresentationReadyWorkSweepMonitor):
def __init__(self, _db, interval_seconds=None,
include_verbose_entry=True):
super(OPDSEntryCacheMonitor, self).__init__(
_db, "ODPS Entry Cache Monitor", interval_seconds)
self.include_verbose_entry=include_verbose_entry
def process_work(self, work):
work.calculate_opds_entries(verbose=self.include_verbose_entry)
class SimpleOPDSEntryCacheMonitor(OPDSEntryCacheMonitor):
def __init__(self, _db, interval_seconds=None):
super(SimpleOPDSEntryCacheMonitor, self).__init__(
_db, interval_seconds, False)
class SubjectAssignmentMonitor(SubjectSweepMonitor):
def __init__(self, _db, subject_type=None, filter_string=None,
interval_seconds=None):
super(SubjectAssignmentMonitor, self).__init__(
_db, "Subject assignment monitor", subject_type, filter_string,
interval_seconds
)
def process_batch(self, subjects):
highest_id = 0
for subject in subjects:
if subject.id > highest_id:
highest_id = subject.id
subject.assign_to_genre()
self.log.log(self.COMPLETION_LOG_LEVEL, "Completed %r", subject)
return highest_id
class PermanentWorkIDRefreshMonitor(EditionSweepMonitor):
"""Recalculate the permanent work ID for every edition."""
def __init__(self, _db, interval_seconds=None):
super(PermanentWorkIDRefreshMonitor, self).__init__(
_db, "Permanent Work ID refresh", interval_seconds)
def process_edition(self, edition):
edition.calculate_permanent_work_id()
class PresentationReadyMonitor(WorkSweepMonitor):
"""A monitor that makes works presentation ready.
By default this works by passing the work's active edition into
ensure_coverage() for each of a list of CoverageProviders. If all
the ensure_coverage() calls succeed, presentation of the work is
calculated and the work is marked presentation ready.
"""
def __init__(self, _db, coverage_providers,
calculate_work_even_if_no_author=False):
super(PresentationReadyMonitor, self).__init__(
_db, "Make Works Presentation Ready")
self.coverage_providers = coverage_providers
self.calculate_work_even_if_no_author = calculate_work_even_if_no_author
def work_query(self):
not_presentation_ready = or_(
Work.presentation_ready==False,
Work.presentation_ready==None)
return self._db.query(Work).filter(not_presentation_ready)
def run_once(self, offset):
# Consolidate works.
LicensePool.consolidate_works(
self._db,
calculate_work_even_if_no_author=self.calculate_work_even_if_no_author)
return super(PresentationReadyMonitor, self).run_once(offset)
def process_batch(self, batch):
max_id = 0
one_success = False
for work in batch:
failures = None
exception = None
if work.id > max_id:
max_id = work.id
try:
failures = self.prepare(work)
except Exception, e:
self.log.error(
"Exception processing work %r", work, exc_info=e
)
failures = e
if failures and failures not in (None, True):
if isinstance(failures, list):
# This is a list of providers that failed.
if len(failures):
provider_names = ", ".join(
[x.service_name for x in failures])
exception = "Provider(s) failed: %s" % provider_names
else:
# Just kidding, the list is empty, there were
# no failures.
pass
else:
exception = str(failures)
if exception:
work.presentation_ready_exception = exception
else:
policy = PresentationCalculationPolicy(
choose_edition=False
)
work.calculate_presentation(policy)
work.set_presentation_ready()
one_success = True
self.finalize_batch()
return max_id
def prepare(self, work):
edition = work.presentation_edition
if not edition:
work = work.calculate_presentation()
identifier = edition.primary_identifier
overall_success = True
failures = []
for provider in self.coverage_providers:
if identifier.type in provider.input_identifier_types:
coverage_record = provider.ensure_coverage(identifier)
if (not isinstance(coverage_record, CoverageRecord)
or coverage_record.exception is not None):
failures.append(provider)
return failures
def finalize_batch(self):
self._db.commit()
class WorkRandomnessUpdateMonitor(WorkSweepMonitor):
def __init__(self, _db, interval_seconds=3600*24,
default_counter=0, batch_size=1000):
super(WorkRandomnessUpdateMonitor, self).__init__(
_db, "Work Randomness Updater", interval_seconds, default_counter, batch_size)
def run_once(self, offset):
new_offset = offset + self.batch_size
text = "update works set random=random() where id >= :offset and id < :new_offset;"
self._db.execute(text, dict(offset=offset, new_offset=new_offset))
[[self.max_work_id]] = self._db.execute('select max(id) from works')
if self.max_work_id < new_offset:
return 0
return new_offset
class CustomListEntryLicensePoolUpdateMonitor(CustomListEntrySweepMonitor):
def __init__(self, _db, interval_seconds=3600*24,
default_counter=0, batch_size=100):
super(CustomListEntryLicensePoolUpdateMonitor, self).__init__(
_db, "Custom List Entry License Pool Update Monitor",
interval_seconds, default_counter, batch_size
)
def process_entry(self, entry):
entry.set_license_pool()