-
Notifications
You must be signed in to change notification settings - Fork 10
/
cron.py
443 lines (380 loc) · 14.7 KB
/
cron.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
import calendar
import datetime
import time
import importlib
from collections import OrderedDict
from pytz import timezone
import log
import provider.swfmeta as swfmetalib
from provider import utils
"""
SWF cron
"""
TIMEZONE = timezone("Europe/London")
IDENTITY = log.identity("cron")
LOGGER = log.logger("cron.log", "INFO", IDENTITY)
def run_cron(settings):
current_datetime = utils.get_current_datetime()
LOGGER.info("current_datetime: %s" % current_datetime)
for conditional_start in conditional_starts(current_datetime):
do_start = workflow_conditional_start(
settings=settings,
workflow_id=conditional_start.get("workflow_id"),
start_seconds=conditional_start.get("start_seconds"),
)
if do_start:
LOGGER.info(
"starting %s, workflow_id %s"
% (
conditional_start.get("starter_name"),
conditional_start.get("workflow_id"),
)
)
start_workflow(
settings=settings,
starter_name=conditional_start.get("starter_name"),
workflow_id=conditional_start.get("workflow_id"),
)
def get_local_datetime(current_datetime, timezone_object):
"""apply the timezone delta to the datetime and remove the tzinfo"""
new_current_datetime = current_datetime
# create new datetime with no timezone prior to localize call
current_datetime_without_tz = datetime.datetime(
current_datetime.year,
current_datetime.month,
current_datetime.day,
current_datetime.hour,
current_datetime.minute,
current_datetime.second,
)
localized_current_datetime = timezone_object.localize(
current_datetime_without_tz, is_dst=False
)
if localized_current_datetime.utcoffset():
new_current_datetime = current_datetime + localized_current_datetime.utcoffset()
new_current_datetime = new_current_datetime.replace(tzinfo=None)
return new_current_datetime
def conditional_starts(current_datetime):
"""given the current time in UTC, return a list of workflows for conditional start"""
conditional_start_list = []
current_time = current_datetime.utctimetuple()
# localised time
local_current_datetime = get_local_datetime(current_datetime, TIMEZONE)
local_current_time = local_current_datetime.utctimetuple()
# Based on the minutes of the current time, run certain starters
if current_time.tm_min >= 0 and current_time.tm_min <= 59:
# Jobs to start at any time during the hour
conditional_start_list.append(
OrderedDict(
[
("starter_name", "cron_FiveMinute"),
("workflow_id", "cron_FiveMinute"),
("start_seconds", 60 * 3),
]
)
)
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_DepositCrossref"),
("workflow_id", "DepositCrossref"),
("start_seconds", 60 * 3),
]
)
)
# Based on the minutes of the current time, run certain starters
if current_time.tm_min >= 0 and current_time.tm_min <= 14:
# Jobs to start at the top of the hour
LOGGER.info("Top of the hour")
# POA Publish at specific hours of the day UK time
if local_current_time.tm_hour in (10, 12, 14, 16):
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PublishPOA"),
("workflow_id", "PublishPOA"),
("start_seconds", 60 * 31),
]
)
)
# CLOCKSS deposits once per day 22:00 UTC
if current_time.tm_hour == 22:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubRouterDeposit"),
("workflow_id", "PubRouterDeposit_CLOCKSS"),
("start_seconds", 60 * 31),
]
)
)
# CNKI deposits once per day 23:00 UTC
if current_time.tm_hour == 23:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubRouterDeposit"),
("workflow_id", "PubRouterDeposit_CNKI"),
("start_seconds", 60 * 31),
]
)
)
elif current_time.tm_min >= 15 and current_time.tm_min <= 19:
# Jobs to start at quarter past the hour
LOGGER.info("Quarter past the hour")
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_DepositCrossrefPendingPublication"),
("workflow_id", "DepositCrossrefPendingPublication"),
("start_seconds", 60 * 31),
]
)
)
# Zendy deposits once per day 21:15 UTC
if current_time.tm_hour == 21:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubRouterDeposit"),
("workflow_id", "PubRouterDeposit_Zendy"),
("start_seconds", 60 * 31),
]
)
)
# OVID deposits once per day 22:15 UTC
if current_time.tm_hour == 22:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubRouterDeposit"),
("workflow_id", "PubRouterDeposit_OVID"),
("start_seconds", 60 * 31),
]
)
)
# Scilit deposits once per day 23:15 UTC
if current_time.tm_hour == 23:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubRouterDeposit"),
("workflow_id", "PubRouterDeposit_Scilit"),
("start_seconds", 60 * 31),
]
)
)
elif current_time.tm_min >= 20 and current_time.tm_min <= 29:
# Jobs to start at 20 minutes past the hour
LOGGER.info("Twenty minutes past the hour")
# POA Packaging at UK local time, run between 6:20 and 15:30
if local_current_time.tm_hour >= 6 and local_current_time.tm_hour <= 15:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "cron_NewS3POA"),
("workflow_id", "cron_NewS3POA"),
("start_seconds", 60 * 3),
]
)
)
# Check for new preprints at 20 minutes past the hour
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_FindNewPreprints"),
("workflow_id", "FindNewPreprints"),
("start_seconds", 60 * 31),
]
)
)
elif current_time.tm_min >= 30 and current_time.tm_min <= 44:
# Jobs to start at the half past to quarter to the hour
LOGGER.info("half past to quarter to the hour")
# POA Packaging at UK local time, run between 6:20 and 15:30
if local_current_time.tm_hour >= 6 and local_current_time.tm_hour <= 15:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "cron_NewS3POA"),
("workflow_id", "cron_NewS3POA"),
("start_seconds", 60 * 31),
]
)
)
# Check for new docmaps at 30 minutes past the hour
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_FindNewDocmaps"),
("workflow_id", "FindNewDocmaps"),
("start_seconds", 60 * 31),
]
)
)
# PMC deposits once per day 20:30 UTC
if current_time.tm_hour == 20:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubRouterDeposit"),
("workflow_id", "PubRouterDeposit_PMC"),
("start_seconds", 60 * 31),
]
)
)
# Web of Science deposits once per day 21:30 UTC
if current_time.tm_hour == 21:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubRouterDeposit"),
("workflow_id", "PubRouterDeposit_WoS"),
("start_seconds", 60 * 31),
]
)
)
# OA Switchboard deposits once per day 22:30 UTC
if current_time.tm_hour == 22:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubRouterDeposit"),
("workflow_id", "PubRouterDeposit_OASwitchboard"),
("start_seconds", 60 * 31),
]
)
)
# CNPIEC deposits once per day 23:30 UTC
if current_time.tm_hour == 23:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubRouterDeposit"),
("workflow_id", "PubRouterDeposit_CNPIEC"),
("start_seconds", 60 * 31),
]
)
)
elif current_time.tm_min >= 45 and current_time.tm_min <= 59:
# Bottom quarter of the hour
# Pub router deposits once per day 23:45 UTC
if current_time.tm_hour == 23:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubRouterDeposit"),
("workflow_id", "PubRouterDeposit_HEFCE"),
("start_seconds", 60 * 31),
]
)
)
# Cengage deposits once per day 22:45 UTC
if current_time.tm_hour == 22:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubRouterDeposit"),
("workflow_id", "PubRouterDeposit_Cengage"),
("start_seconds", 60 * 31),
]
)
)
# Author emails at 22:45 local time
if local_current_time.tm_hour == 22:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PublicationEmail"),
("workflow_id", "PublicationEmail"),
("start_seconds", 60 * 31),
]
)
)
# GoOA / CAS deposits once per day 21:45 UTC
if current_time.tm_hour == 21:
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubRouterDeposit"),
("workflow_id", "PubRouterDeposit_GoOA"),
("start_seconds", 60 * 31),
]
)
)
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_PubmedArticleDeposit"),
("workflow_id", "PubmedArticleDeposit"),
("start_seconds", 60 * 31),
]
)
)
conditional_start_list.append(
OrderedDict(
[
("starter_name", "starter_AdminEmail"),
("workflow_id", "AdminEmail"),
("start_seconds", (60 * 60 * 4) - (14 * 60)),
]
)
)
return conditional_start_list
def workflow_conditional_start(
settings, start_seconds, workflow_id=None, workflow_name=None, workflow_version=None
):
"""
Given workflow criteria, check the workflow completion history for the last time run
If it last run more than start_seconds ago, start a new workflow
"""
diff_seconds = None
last_startTimestamp = None
swfmeta = swfmetalib.SWFMeta(settings)
swfmeta.connect()
last_startTimestamp = swfmeta.get_last_completed_workflow_execution_startTimestamp(
workflow_id=workflow_id,
workflow_name=workflow_name,
workflow_version=workflow_version,
)
current_timestamp = calendar.timegm(time.gmtime())
if last_startTimestamp is not None:
diff_seconds = current_timestamp - start_seconds - last_startTimestamp
if diff_seconds >= 0 or last_startTimestamp is None:
return True
LOGGER.info(
"workflow name %s, id %s, ran previously at %s, %s seconds short to start again"
% (workflow_name, workflow_id, last_startTimestamp, diff_seconds)
)
def start_workflow(settings, starter_name, workflow_id=None):
"""start the workflow using the starter"""
# Start a new workflow
# Load the starter module
module_name = "starter." + starter_name
module_object = importlib.import_module(module_name)
starter_class = getattr(module_object, starter_name)
starter_object = starter_class()
# Customised start functions
if starter_name == "starter_AdminEmail":
starter_object.start(settings=settings)
elif starter_name == "starter_PubRouterDeposit":
# PubRouterDeposit has different variants specified by the workflow variable
workflow = workflow_id.split("_")[-1]
starter_object.start(settings=settings, workflow=workflow)
elif starter_name in [
"cron_FiveMinute",
"starter_PublishPOA",
"cron_NewS3POA",
"starter_PublicationEmail",
"starter_DepositCrossref",
"starter_DepositCrossrefPeerReview",
"starter_DepositCrossrefPendingPublication",
"starter_PubmedArticleDeposit",
"starter_FindNewPreprints",
"starter_FindNewDocmaps",
]:
starter_object.start(settings=settings)
if __name__ == "__main__":
ENV = utils.console_start_env()
SETTINGS = utils.get_settings(ENV)
run_cron(settings=SETTINGS)