-
Notifications
You must be signed in to change notification settings - Fork 222
/
crontabber.py
executable file
·814 lines (714 loc) · 29 KB
/
crontabber.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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
CronTabber is a configman app for executing all Socorro cron jobs.
"""
import os
import traceback
import inspect
import datetime
import sys
import json
import copy
from configman import Namespace, RequiredConfig
from configman.converters import class_converter
from socorro.database.transaction_executor import TransactionExecutor
from socorro.external.postgresql.connection_context import ConnectionContext
from socorro.app.generic_app import App, main
from socorro.lib.datetimeutil import utc_now, UTC
from socorro.cron.base import (
convert_frequency,
FrequencyDefinitionError,
BaseBackfillCronApp
)
DEFAULT_JOBS = '''
socorro.cron.jobs.weekly_reports_partitions.WeeklyReportsPartitionsCronApp|7d
socorro.cron.jobs.matviews.ProductVersionsCronApp|1d|10:00
socorro.cron.jobs.matviews.SignaturesCronApp|1d|10:00
socorro.cron.jobs.matviews.TCBSCronApp|1d|10:00
socorro.cron.jobs.matviews.ADUCronApp|1d|10:00
socorro.cron.jobs.matviews.NightlyBuildsCronApp|1d|10:00
socorro.cron.jobs.matviews.DuplicatesCronApp|1h
socorro.cron.jobs.matviews.ReportsCleanCronApp|1h
socorro.cron.jobs.bugzilla.BugzillaCronApp|1h
socorro.cron.jobs.matviews.BuildADUCronApp|1d|10:00
socorro.cron.jobs.matviews.CrashesByUserCronApp|1d|10:00
socorro.cron.jobs.matviews.CrashesByUserBuildCronApp|1d|10:00
socorro.cron.jobs.matviews.CorrelationsCronApp|1d|10:00
socorro.cron.jobs.matviews.HomePageGraphCronApp|1d|10:00
socorro.cron.jobs.matviews.HomePageGraphBuildCronApp|1d|10:00
socorro.cron.jobs.matviews.TCBSBuildCronApp|1d|10:00
socorro.cron.jobs.matviews.ExplosivenessCronApp|1d|10:00
socorro.cron.jobs.ftpscraper.FTPScraperCronApp|1h
socorro.cron.jobs.automatic_emails.AutomaticEmailsCronApp|1h
'''
class JobNotFoundError(Exception):
pass
class TimeDefinitionError(Exception):
pass
class JobDescriptionError(Exception):
pass
class BrokenJSONError(ValueError):
pass
class JSONJobDatabase(dict):
_date_fmt = '%Y-%m-%d %H:%M:%S.%f'
_day_fmt = '%Y-%m-%d'
def __init__(self, config=None):
self.config = config
def load(self, file_path):
try:
self.update(self._recurse_load(json.load(open(file_path))))
except IOError:
pass
except ValueError, msg:
raise BrokenJSONError(msg)
def _recurse_load(self, struct):
for key, value in struct.items():
if isinstance(value, dict):
self._recurse_load(value)
else:
try:
value = (datetime.datetime
.strptime(value, self._date_fmt)
.replace(tzinfo=UTC))
struct[key] = value
except (ValueError, TypeError):
try:
value = (datetime.datetime
.strptime(value, self._day_fmt).date())
struct[key] = value
except (ValueError, TypeError):
pass
return struct
def save(self, file_path):
with open(file_path, 'w') as f:
json.dump(self._recurse_serialize(copy.deepcopy(dict(self))),
f, indent=2)
def _recurse_serialize(self, struct):
for key, value in struct.items():
if isinstance(value, dict):
self._recurse_serialize(value)
elif isinstance(value, datetime.datetime):
struct[key] = value.strftime(self._date_fmt)
elif isinstance(value, (int, long, float)):
pass
elif not isinstance(value, basestring):
struct[key] = unicode(value)
return struct
class JSONAndPostgresJobDatabase(JSONJobDatabase):
def load(self, file_path):
if not os.path.isfile(file_path):
# try to read from postgres instead
self._load_from_postgres(file_path)
super(JSONAndPostgresJobDatabase, self).load(file_path)
def _load_from_postgres(self, file_path):
database_class = self.config.database.database_class(
self.config.database
)
with database_class() as connection:
cursor = connection.cursor()
cursor.execute('SELECT state FROM crontabber_state')
try:
json_dump, = cursor.fetchone()
if json_dump != '{}':
with open(file_path, 'w') as f:
f.write(json_dump)
except ValueError:
pass
def save(self, file_path):
super(JSONAndPostgresJobDatabase, self).save(file_path)
try:
self._save_to_postgres()
except Exception:
#raise # for desperate debugging
logger = self.config.logger
logger.error("Unable to save JSON to postgres",
exc_info=True)
def _save_to_postgres(self):
json_data = json.dumps(
self._recurse_serialize(copy.deepcopy(dict(self))),
indent=2
)
database = self.config.database.database_class(self.config.database)
with database() as connection:
cursor = connection.cursor()
cursor.execute('UPDATE crontabber_state SET state=%s',
(json_data,))
connection.commit()
def timesince(d, now): # pragma: no cover
"""
Taken from django.utils.timesince
"""
def ungettext(a, b, n):
if n == 1:
return a
return b
def ugettext(s):
return s
def is_aware(v):
return v.tzinfo is not None and v.tzinfo.utcoffset(v) is not None
chunks = (
(60 * 60 * 24 * 365, lambda n: ungettext('year', 'years', n)),
(60 * 60 * 24 * 30, lambda n: ungettext('month', 'months', n)),
(60 * 60 * 24 * 7, lambda n: ungettext('week', 'weeks', n)),
(60 * 60 * 24, lambda n: ungettext('day', 'days', n)),
(60 * 60, lambda n: ungettext('hour', 'hours', n)),
(60, lambda n: ungettext('minute', 'minutes', n))
)
# Convert datetime.date to datetime.datetime for comparison.
if not isinstance(d, datetime.datetime):
d = datetime.datetime(d.year, d.month, d.day)
if now and not isinstance(now, datetime.datetime):
now = datetime.datetime(now.year, now.month, now.day)
if not now:
now = datetime.datetime.utcnow()
delta = now - d
# ignore microseconds
since = delta.days * 24 * 60 * 60 + delta.seconds
if since <= 0:
# d is in the future compared to now, stop processing.
return u'0 ' + ugettext('minutes')
for i, (seconds, name) in enumerate(chunks):
count = since // seconds
if count != 0:
break
s = ugettext('%(number)d %(type)s') % {
'number': count, 'type': name(count)
}
if i + 1 < len(chunks):
# Now get the second item
seconds2, name2 = chunks[i + 1]
count2 = (since - (seconds * count)) // seconds2
if count2 != 0:
s += ugettext(', %(number)d %(type)s') % {
'number': count2, 'type': name2(count2)
}
return s
#------------------------------------------------------------------------------
def _default_list_splitter(class_list_str):
return [x.strip() for x in class_list_str.split(',')]
def _default_class_extractor(list_element):
return list_element
def _default_extra_extractor(list_element):
raise NotImplementedError()
def classes_in_namespaces_converter_with_compression(
reference_namespace={},
template_for_namespace="class-%(name)s",
list_splitter_fn=_default_list_splitter,
class_extractor=_default_class_extractor,
extra_extractor=_default_extra_extractor):
"""
parameters:
template_for_namespace - a template for the names of the namespaces
that will contain the classes and their
associated required config options. There are
two template variables available: %(name)s -
the name of the class to be contained in the
namespace; %(index)d - the sequential index
number of the namespace.
list_converter - a function that will take the string list of classes
and break it up into a sequence if individual elements
class_extractor - a function that will return the string version of
a classname from the result of the list_converter
extra_extractor - a function that will return a Namespace of options
created from any extra information associated with
the classes returned by the list_converter function
"""
#--------------------------------------------------------------------------
def class_list_converter(class_list_str):
"""This function becomes the actual converter used by configman to
take a string and convert it into the nested sequence of Namespaces,
one for each class in the list. It does this by creating a proxy
class stuffed with its own 'required_config' that's dynamically
generated."""
if isinstance(class_list_str, basestring):
class_str_list = list_splitter_fn(class_list_str)
else:
raise TypeError('must be derivative of a basestring')
#======================================================================
class InnerClassList(RequiredConfig):
"""This nested class is a proxy list for the classes. It collects
all the config requirements for the listed classes and places them
each into their own Namespace.
"""
# we're dynamically creating a class here. The following block of
# code is actually adding class level attributes to this new class
# 1st requirement for configman
required_config = Namespace()
# to help the programmer know what Namespaces we added
subordinate_namespace_names = []
# save the template for future reference
namespace_template = template_for_namespace
# for display
original_input = class_list_str.replace('\n', '\\n')
# for each class in the class list
class_list = []
for namespace_index, class_list_element in enumerate(
class_str_list
):
try:
a_class = class_converter(
class_extractor(class_list_element)
)
except AttributeError:
raise JobNotFoundError(class_list_element)
class_list.append((a_class.__name__, a_class))
# figure out the Namespace name
namespace_name_dict = {
'name': a_class.__name__,
'index': namespace_index
}
namespace_name = template_for_namespace % namespace_name_dict
subordinate_namespace_names.append(namespace_name)
# create the new Namespace
required_config.namespace(namespace_name)
a_class_namespace = required_config[namespace_name]
# add options for the 'extra data'
try:
extra_options = extra_extractor(class_list_element)
a_class_namespace.update(extra_options)
except NotImplementedError:
pass
# add options frr the classes required config
try:
for k, v in a_class.get_required_config().iteritems():
if k not in reference_namespace:
a_class_namespace[k] = v
except AttributeError: # a_class has no get_required_config
pass
@classmethod
def to_str(cls):
"""this method takes this inner class object and turns it back
into the original string of classnames. This is used
primarily as for the output of the 'help' option"""
return cls.original_input
return InnerClassList # result of class_list_converter
return class_list_converter # result of classes_in_namespaces_converter
def get_extra_as_options(input_str):
if '|' not in input_str:
raise JobDescriptionError('No frequency and/or time defined')
metadata = input_str.split('|')[1:]
if len(metadata) == 1:
if ':' in metadata[0]:
frequency = '1d'
time_ = metadata[0]
else:
frequency = metadata[0]
time_ = None
else:
frequency, time_ = metadata
n = Namespace()
n.add_option(
'frequency',
doc='frequency',
default=frequency,
#from_string_converter=int
exclude_from_print_conf=True,
exclude_from_dump_conf=True
)
n.add_option(
'time',
doc='time',
default=time_,
exclude_from_print_conf=True,
exclude_from_dump_conf=True
)
return n
def check_time(value):
"""check that it's a value like 03:45 or 1:1"""
try:
h, m = value.split(':')
h = int(h)
m = int(m)
if h >= 24 or h < 0:
raise ValueError
if m >= 60 or m < 0:
raise ValueError
except ValueError:
raise TimeDefinitionError("Invalid definition of time %r" % value)
def line_splitter(text):
return [x.strip() for x in text.splitlines()
if x.strip() and not x.strip().startswith('#')]
def pipe_splitter(text):
return text.split('|', 1)[0]
class CronTabber(App):
app_name = 'crontabber'
app_version = '1.1'
app_description = __doc__
required_config = Namespace()
# the most important option, 'jobs', is defined last
required_config.namespace('crontabber')
required_config.crontabber.add_option(
name='database_file',
default='./crontabbers.json',
doc='Location of file where job execution logs are stored',
)
required_config.crontabber.add_option(
name='json_database_class',
default=JSONAndPostgresJobDatabase,
doc='Class to load and save the JSON database',
)
required_config.crontabber.add_option(
'jobs',
default=DEFAULT_JOBS,
from_string_converter=classes_in_namespaces_converter_with_compression(
reference_namespace=required_config.crontabber,
list_splitter_fn=line_splitter,
class_extractor=pipe_splitter,
extra_extractor=get_extra_as_options
)
)
required_config.namespace('database')
required_config.database.add_option(
'database_class',
default=ConnectionContext,
from_string_converter=class_converter
)
required_config.database.add_option(
'transaction_executor_class',
default=TransactionExecutor,
doc='a class that will execute transactions'
)
required_config.add_option(
name='job',
default='',
doc='Run a specific job',
short_form='j',
exclude_from_print_conf=True,
exclude_from_dump_conf=True,
)
required_config.add_option(
name='list-jobs',
default=False,
doc='List all jobs',
short_form='l',
exclude_from_print_conf=True,
exclude_from_dump_conf=True,
)
required_config.add_option(
name='force',
default=False,
doc='Force running a job despite dependencies',
short_form='f',
exclude_from_print_conf=True,
exclude_from_dump_conf=True,
)
required_config.add_option(
name='configtest',
default=False,
doc='Check that all configured jobs are OK',
exclude_from_print_conf=True,
exclude_from_dump_conf=True,
)
required_config.add_option(
name='reset-job',
default='',
doc='Pretend a job has never been run',
short_form='r',
exclude_from_print_conf=True,
exclude_from_dump_conf=True,
)
required_config.add_option(
name='nagios',
default=False,
doc='Exits with 0, 1 or 2 with a message on stdout if errors have '
'happened.',
short_form='n',
exclude_from_print_conf=True,
exclude_from_dump_conf=True,
)
def main(self):
if self.config.get('list-jobs'):
self.list_jobs()
elif self.config.get('nagios'):
return self.nagios()
elif self.config.get('reset-job'):
self.reset_job(self.config.get('reset-job'))
elif self.config.get('job'):
self.run_one(self.config['job'], self.config.get('force'))
elif self.config.get('configtest'):
if not self.configtest():
return 1
else:
self.run_all()
return 0
@property
def database(self):
if not getattr(self, '_database', None):
self._database = self.config.crontabber.json_database_class(
self.config
)
self._database.load(self.config.crontabber.database_file)
return self._database
def nagios(self, stream=sys.stdout):
"""
return 0 (OK) if there are no errors in the state.
return 1 (WARNING) if a backfill app only has 1 error.
return 2 (CRITICAL) if a backfill app has > 1 error.
return 2 (CRITICAL) if a non-backfill app has 1 error.
"""
warnings = []
criticals = []
for class_name, job_class in self.config.crontabber.jobs.class_list:
if job_class.app_name in self.database:
info = self.database.get(job_class.app_name)
if not info.get('error_count', 0):
continue
error_count = info['error_count']
# trouble!
serialized = (
'%s (%s) | %s | %s' %
(job_class.app_name,
class_name,
info['last_error']['type'],
info['last_error']['value'])
)
if (
error_count == 1 and
issubclass(job_class, BaseBackfillCronApp)
):
# just a warning for now
warnings.append(serialized)
else:
# anything worse than that is critical
criticals.append(serialized)
if criticals:
stream.write('CRITICAL - ')
for each in criticals:
stream.write(each)
stream.write('\n')
elif warnings:
stream.write('WARNING - ')
for each in warnings:
stream.write(each)
stream.write('\n')
if criticals:
return 2
elif warnings:
return 1
return 0
def list_jobs(self, stream=None):
if not stream:
stream = sys.stdout
_fmt = '%Y-%m-%d %H:%M:%S'
_now = utc_now()
PAD = 15
for class_name, job_class in self.config.crontabber.jobs.class_list:
class_config = self.config.crontabber['class-%s' % class_name]
freq = class_config.frequency
if class_config.time:
freq += ' @ %s' % class_config.time
class_name = job_class.__module__ + '.' + job_class.__name__
print >>stream, '=== JOB ' + '=' * 72
print >>stream, 'Class:'.ljust(PAD), class_name
print >>stream, 'App name:'.ljust(PAD), job_class.app_name
print >>stream, 'Frequency:'.ljust(PAD), freq
try:
info = self.database[job_class.app_name]
except KeyError:
print >>stream, '*NO PREVIOUS RUN INFO*'
continue
print >>stream, 'Last run:'.ljust(PAD),
print >>stream, info['last_run'].strftime(_fmt).ljust(20),
print >>stream, '(%s ago)' % timesince(info['last_run'], _now)
print >>stream, 'Last success:'.ljust(PAD),
if info.get('last_success'):
print >>stream, info['last_success'].strftime(_fmt).ljust(20),
print >>stream, ('(%s ago)' %
timesince(info['last_success'], _now))
else:
print >>stream, 'no previous successful run'
print >>stream, 'Next run:'.ljust(PAD),
print >>stream, info['next_run'].strftime(_fmt).ljust(20),
if _now > info['next_run']:
print >>stream, ('(was %s ago)' %
timesince(info['next_run'], _now))
else:
print >>stream, '(in %s)' % timesince(_now, info['next_run'])
if info.get('last_error'):
print >>stream, 'Error!!'.ljust(PAD),
print >>stream, '(%s times)' % info['error_count']
print >>stream, info['last_error']['traceback']
print >>stream, ''
def reset_job(self, description):
"""remove the job from the state.
if means that next time we run, this job will start over from scratch.
"""
for class_name, job_class in self.config.crontabber.jobs.class_list:
if (
job_class.app_name == description or
description == job_class.__module__ + '.' + job_class.__name__
):
if job_class.app_name in self.database:
self.config.logger.info('App reset')
self.database.pop(job_class.app_name)
self.database.save(self.config.crontabber.database_file)
else:
self.config.logger.warning('App already reset')
return
raise JobNotFoundError(description)
def run_all(self):
for class_name, job_class in self.config.crontabber.jobs.class_list:
class_config = self.config.crontabber['class-%s' % class_name]
self._run_one(job_class, class_config)
def run_one(self, description, force=False):
# the description in this case is either the app_name or the full
# module/class reference
for class_name, job_class in self.config.crontabber.jobs.class_list:
if (
job_class.app_name == description or
description == job_class.__module__ + '.' + job_class.__name__
):
class_config = self.config.crontabber['class-%s' % class_name]
self._run_one(job_class, class_config, force=force)
return
raise JobNotFoundError(description)
def _run_one(self, job_class, config, force=False):
_debug = self.config.logger.debug
seconds = convert_frequency(config.frequency)
time_ = config.time
if not force:
if not self.time_to_run(job_class, time_):
_debug("skipping %r because it's not time to run", job_class)
return
ok, dependency_error = self.check_dependencies(job_class)
if not ok:
_debug(
"skipping %r dependencies aren't met [%s]",
job_class, dependency_error
)
return
_debug('about to run %r', job_class)
app_name = job_class.app_name
info = self.database.get(app_name)
last_success = None
now = utc_now()
try:
for last_success in self._run_job(job_class, config, info):
_debug('successfully ran %r on %s', job_class, last_success)
exc_type = exc_value = exc_tb = None
except:
exc_type, exc_value, exc_tb = sys.exc_info()
# when debugging tests that mock logging, uncomment this otherwise
# the exc_info=True doesn't compute and record what the exception
# was
#raise
_debug('error when running %r on %s',
job_class, last_success, exc_info=True)
finally:
self._log_run(job_class, seconds, time_, last_success, now,
exc_type, exc_value, exc_tb)
def check_dependencies(self, class_):
try:
depends_on = class_.depends_on
except AttributeError:
# that's perfectly fine
return True, None
if isinstance(depends_on, basestring):
depends_on = [depends_on]
for dependency in depends_on:
try:
job_info = self.database[dependency]
except KeyError:
# the job this one depends on hasn't been run yet!
return False, "%r hasn't been run yet" % dependency
if job_info.get('last_error'):
# errored last time it ran
return False, "%r errored last time it ran" % dependency
if job_info['next_run'] < utc_now():
# the dependency hasn't recently run
return False, "%r hasn't recently run" % dependency
# no reason not to stop this class
return True, None
def time_to_run(self, class_, time_):
"""return true if it's time to run the job.
This is true if there is no previous information about its last run
or if the last time it ran and set its next_run to a date that is now
past.
"""
app_name = class_.app_name
try:
info = self.database[app_name]
except KeyError:
if time_:
h, m = [int(x) for x in time_.split(':')]
# only run if this hour and minute is < now
now = utc_now()
if now.hour > h:
return True
elif now.hour == h and now.minute >= m:
return True
return False
else:
# no past information, run now
return True
next_run = info['next_run']
if next_run < utc_now():
return True
return False
def _run_job(self, class_, config, info):
# here we go!
instance = class_(config, info)
return instance.main()
def _log_run(self, class_, seconds, time_, last_success, now,
exc_type, exc_value, exc_tb):
assert inspect.isclass(class_)
app_name = class_.app_name
info = self.database.get(app_name, {})
if 'first_run' not in info:
info['first_run'] = now
info['last_run'] = now
info['next_run'] = now + datetime.timedelta(seconds=seconds)
if last_success:
info['last_success'] = last_success
if time_:
h, m = [int(x) for x in time_.split(':')]
info['next_run'] = info['next_run'].replace(hour=h,
minute=m,
second=0,
microsecond=0)
if exc_type:
tb = ''.join(traceback.format_tb(exc_tb))
info['last_error'] = {
'type': exc_type,
'value': str(exc_value),
'traceback': tb,
}
info['error_count'] = info.get('error_count', 0) + 1
else:
info['last_error'] = {}
info['error_count'] = 0
self.database[app_name] = info
self.database.save(self.config.crontabber.database_file)
def configtest(self):
"""return true if all configured jobs are configured OK"""
# similar to run_all() but don't actually run them
failed = 0
for class_name, __ in self.config.crontabber.jobs.class_list:
class_config = self.config.crontabber['class-%s' % class_name]
if not self._configtest_one(class_config):
failed += 1
return not failed
def _configtest_one(self, config):
try:
seconds = convert_frequency(config.frequency)
time_ = config.time
if time_:
check_time(time_)
# if less than 1 day, it doesn't make sense to specify hour
if seconds < 60 * 60 * 24:
raise FrequencyDefinitionError(config.time)
return True
except (JobNotFoundError,
JobDescriptionError,
FrequencyDefinitionError,
TimeDefinitionError):
exc_type, exc_value, exc_tb = sys.exc_info()
print >>sys.stderr, "Error type:", exc_type
print >>sys.stderr, "Error value:", exc_value
print >>sys.stderr, ''.join(traceback.format_tb(exc_tb))
return False
if __name__ == '__main__': # pragma: no cover
sys.exit(main(CronTabber))