-
Notifications
You must be signed in to change notification settings - Fork 710
/
cron.py
360 lines (308 loc) · 7.91 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
import datetime
import os
import sys
from subprocess import check_call
import babis
from apscheduler.schedulers.blocking import BlockingScheduler
from django.conf import settings
from django.utils import timezone
MANAGE = os.path.join(settings.ROOT, "manage.py")
schedule = BlockingScheduler()
def call_command(command):
check_call("python {0} {1}".format(MANAGE, command), shell=True)
class scheduled_job(object):
"""Decorator for scheduled jobs. Takes same args as apscheduler.schedule_job."""
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.skip = self.kwargs.pop("skip", False)
def __call__(self, fn):
self.name = fn.__name__
if self.skip:
self.log("Skipped, not registered.")
return None
self.callback = fn
schedule.add_job(self.run, id=self.name, *self.args, **self.kwargs)
self.log("Registered.")
return self.run
def run(self):
self.log("starting")
try:
self.callback()
except Exception as e:
self.log("CRASHED: {}".format(e))
raise
else:
self.log("finished successfully")
def log(self, message):
msg = "[{}] Clock job {}@{}: {}".format(
datetime.datetime.utcnow(), self.name, settings.PLATFORM_NAME, message
)
print(msg, file=sys.stderr)
# Every 10 minutes.
@scheduled_job("cron", month="*", day="*", hour="*", minute="*/10", max_instances=1, coalesce=True)
@babis.decorator(ping_after=settings.DMS_ENQUEUE_LAG_MONITOR_TASK)
def job_enqueue_lag_monitor_task():
call_command("enqueue_lag_monitor_task")
# Every hour.
@scheduled_job(
"cron",
month="*",
day="*",
hour="*",
minute="30",
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_SEND_WELCOME_EMAILS)
def job_send_welcome_emails():
call_command("send_welcome_emails")
@scheduled_job(
"cron",
month="*",
day="*",
hour="*",
minute="45",
max_instances=1,
coalesce=True,
# only run on readonly clusters, where no signals will be triggered:
skip=(not settings.READ_ONLY),
)
@babis.decorator(ping_after=settings.DMS_REINDEX_ES)
def job_reindex_es():
# Index items newer than 90 minutes old in ES
after = (timezone.now() - datetime.timedelta(minutes=90)).isoformat()
call_command("es_reindex --updated-after {}".format(after))
# Every 6 hours.
@scheduled_job(
"cron",
month="*",
day="*",
hour="*/6",
minute="00",
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_UPDATE_PRODUCT_DETAILS)
def job_update_product_details():
call_command("update_product_details")
@scheduled_job(
"cron",
month="*",
day="*",
hour="*/6",
minute="20",
max_instances=1,
coalesce=True,
skip=(settings.READ_ONLY or settings.STAGE),
)
@babis.decorator(ping_after=settings.DMS_GENERATE_MISSING_SHARE_LINKS)
def job_generate_missing_share_links():
call_command("generate_missing_share_links")
# Once per day.
@scheduled_job(
"cron",
month="*",
day="*",
hour="00",
minute="00",
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_REBUILD_KB)
def job_rebuild_kb():
call_command("rebuild_kb")
@scheduled_job(
"cron",
month="*",
day="*",
hour="00",
minute="42",
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_UPDATE_TOP_CONTRIBUTORS)
def job_update_top_contributors():
call_command("update_top_contributors")
@scheduled_job(
"cron",
month="*",
day="*",
hour="01",
minute="00",
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_UPDATE_L10N_COVERAGE_METRICS)
def job_update_l10n_coverage_metrics():
call_command("update_l10n_coverage_metrics")
@scheduled_job(
"cron",
month="*",
day="*",
hour="01",
minute="11",
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_REPORT_EMPLOYEE_ANSWERS)
def job_report_employee_answers():
call_command("report_employee_answers")
@scheduled_job(
"cron",
month="*",
day="*",
hour="01",
minute="40",
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_UPDATE_WEEKLY_VOTES)
def job_update_weekly_votes():
call_command("update_weekly_votes")
@scheduled_job(
"cron",
month="*",
day="*",
hour="03",
minute="00",
max_instances=1,
coalesce=True,
skip=(settings.READ_ONLY or settings.STAGE),
)
@babis.decorator(ping_after=settings.DMS_UPDATE_CONTRIBUTOR_METRICS)
def job_update_contributor_metrics():
call_command("update_contributor_metrics")
@scheduled_job(
"cron",
month="*",
day="*",
hour="04",
minute="00",
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_AUTO_ARCHIVE_OLD_QUESTIONS)
def job_auto_archive_old_questions():
call_command("auto_archive_old_questions")
@scheduled_job(
"cron",
month="*",
day="*",
hour="10",
minute="00",
max_instances=1,
coalesce=True,
skip=(settings.READ_ONLY or settings.STAGE),
)
@babis.decorator(ping_after=settings.DMS_UPDATE_L10N_METRIC)
def job_update_l10n_metric():
call_command("update_l10n_metric")
@scheduled_job(
"cron",
month="*",
day="*",
hour="16",
minute="00",
max_instances=1,
coalesce=True,
skip=(settings.READ_ONLY or settings.STAGE),
)
@babis.decorator(ping_after=settings.DMS_RELOAD_WIKI_TRAFFIC_STATS)
def job_reload_wiki_traffic_stats():
call_command("reload_wiki_traffic_stats")
@scheduled_job(
"cron",
month="*",
day="*",
hour="21",
minute="00",
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_CACHE_MOST_UNHELPFUL_KB_ARTICLES)
def job_cache_most_unhelpful_kb_articles():
call_command("cache_most_unhelpful_kb_articles")
@scheduled_job(
"cron",
month="*",
day="*",
hour="23",
minute="00",
max_instances=1,
coalesce=True,
skip=(settings.READ_ONLY or settings.STAGE),
)
@babis.decorator(ping_after=settings.DMS_RELOAD_QUESTION_TRAFFIC_STATS)
def job_reload_question_traffic_stats():
call_command("reload_question_traffic_stats")
@scheduled_job(
"cron",
month="*",
day="*",
hour="04",
minute="00",
day_of_week=5,
max_instances=1,
coalesce=True,
skip=(settings.READ_ONLY or settings.STAGE),
)
@babis.decorator(ping_after=settings.DMS_SEND_WEEKLY_READY_FOR_REVIEW_DIGEST)
def job_send_weekly_ready_for_review_digest():
call_command("send_weekly_ready_for_review_digest")
@scheduled_job(
"cron",
month="*",
day="*",
hour="00",
minute="00",
day_of_week=0,
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_FIX_CURRENT_REVISIONS)
def job_fix_current_revisions():
call_command("fix_current_revisions")
@scheduled_job(
"cron",
month="*",
day="*",
hour="00",
minute="30",
day_of_week=1,
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_COHORT_ANALYSIS)
def job_cohort_analysis():
call_command("cohort_analysis")
# Once per month
@scheduled_job(
"cron",
month="*",
day="1",
hour="00",
minute="30",
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_UPDATE_L10N_CONTRIBUTOR_METRICS)
def job_update_l10n_contributor_metrics():
call_command("update_l10n_contributor_metrics")
def run():
try:
schedule.start()
except (KeyboardInterrupt, SystemExit):
pass