-
Notifications
You must be signed in to change notification settings - Fork 10
/
cron-scheduler.py
369 lines (330 loc) · 12.8 KB
/
cron-scheduler.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
import os
from datetime import datetime, timezone
from logging import getLogger
import django
import environ
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local')
django.setup()
from django.conf import settings
from datahub.company.tasks.adviser import schedule_automatic_adviser_deactivate
from datahub.company.tasks.company import schedule_automatic_company_archive
from datahub.company.tasks.contact import (
schedule_automatic_contact_archive,
)
from datahub.company.tasks.export_potential import update_company_export_potential_from_csv
from datahub.company_activity.tasks.ingest_company_activity import ingest_activity_data
from datahub.core.queues.constants import (
EVERY_EIGHT_AM,
EVERY_EIGHT_THIRTY_AM_ON_FIRST_EACH_MONTH,
EVERY_ELEVEN_PM,
EVERY_HOUR,
EVERY_MIDNIGHT,
EVERY_NINE_THIRTY_AM_ON_FIRST_SECOND_THIRD_FOURTH_OF_EACH_MONTH,
EVERY_ONE_AM,
EVERY_SEVEN_PM,
EVERY_TEN_AM,
EVERY_TEN_MINUTES,
EVERY_TEN_PM,
EVERY_THREE_AM,
# EVERY_THREE_AM_ON_TWENTY_EIGHTH_EACH_MONTH,
EVERY_TWO_AM,
HALF_DAY_IN_SECONDS,
)
from datahub.core.queues.health_check import queue_health_check
from datahub.core.queues.job_scheduler import job_scheduler
from datahub.core.queues.scheduler import DataHubScheduler, LONG_RUNNING_QUEUE
from datahub.dnb_api.tasks.sync import schedule_sync_outdated_companies_with_dnb
from datahub.dnb_api.tasks.update import schedule_get_company_updates
from datahub.email_ingestion.tasks import process_mailbox_emails
from datahub.export_win.tasks import (
auto_resend_client_email_from_unconfirmed_win,
update_notify_email_delivery_status_for_customer_response,
update_notify_email_delivery_status_for_customer_response_token,
)
# from datahub.investment.project.tasks import (
# schedule_refresh_gross_value_added_value_for_fdi_investment_projects,
# )
from datahub.investment_lead.tasks.ingest_eyb_triage import ingest_eyb_triage_file
from datahub.omis.payment.tasks import refresh_pending_payment_gateway_sessions
from datahub.reminder.migration_tasks import run_ita_users_migration, run_post_users_migration
from datahub.reminder.tasks import (
generate_new_export_interaction_reminders,
generate_no_recent_export_interaction_reminders,
generate_no_recent_interaction_reminders,
schedule_generate_estimated_land_date_reminders,
update_notify_email_delivery_status_for_estimated_land_date,
update_notify_email_delivery_status_for_new_export_interaction,
update_notify_email_delivery_status_for_no_recent_export_interaction,
update_notify_email_delivery_status_for_no_recent_interaction,
)
from datahub.search.tasks import sync_all_models
from datahub.task.tasks import schedule_reminders_tasks_overdue, schedule_reminders_upcoming_tasks
env = environ.Env()
logger = getLogger(__name__)
def schedule_jobs():
cancel_existing_cron_jobs()
logger.info('Scheduling jobs that run on a cron')
job_scheduler(
function=queue_health_check,
cron=EVERY_TEN_MINUTES,
)
job_scheduler(
function=refresh_pending_payment_gateway_sessions,
function_kwargs={
'age_check': 60, # in minutes
},
cron=EVERY_HOUR,
description='Refresh pending payment gateway sessions :0',
)
job_scheduler(
function=schedule_reminders_upcoming_tasks,
cron=EVERY_EIGHT_AM,
description='Schedule reminders upcoming tasks',
)
job_scheduler(
function=schedule_reminders_tasks_overdue,
cron=EVERY_EIGHT_AM,
description='Schedule reminders tasks overdue',
)
job_scheduler(
function=schedule_automatic_company_archive,
function_kwargs={
'limit': 20000,
'simulate': False,
},
cron=EVERY_SEVEN_PM,
description='Automatic Company Archive',
)
job_scheduler(
function=schedule_automatic_adviser_deactivate,
function_kwargs={
'limit': 20000,
'simulate': False,
},
cron=EVERY_SEVEN_PM,
description='Automatic Adviser Deactivate',
)
job_scheduler(
function=schedule_automatic_contact_archive,
function_kwargs={
'limit': 20000,
'simulate': False,
},
cron=EVERY_SEVEN_PM,
description='Automatic Contact Archive',
)
job_scheduler(
function=schedule_get_company_updates,
cron=EVERY_MIDNIGHT,
description='Update companies from dnb service',
)
job_scheduler(
function=ingest_activity_data,
cron=EVERY_HOUR,
description='Check S3 for new Company Activity data files and schedule ingestion',
)
job_scheduler(
function=ingest_eyb_triage_file,
cron=EVERY_HOUR,
description='Check S3 for new EYB triage data files and schedule ingestion',
)
if settings.ENABLE_ESTIMATED_LAND_DATE_REMINDERS:
job_scheduler(
function=schedule_generate_estimated_land_date_reminders,
cron=EVERY_EIGHT_THIRTY_AM_ON_FIRST_EACH_MONTH,
description='schedule_generate_estimated_land_date_reminders',
)
# TODO: uncomment this once the infinite loop issue (when refreshing GVA) has been fixed
# job_scheduler(
# function=schedule_refresh_gross_value_added_value_for_fdi_investment_projects,
# cron=EVERY_THREE_AM_ON_TWENTY_EIGHTH_EACH_MONTH,
# description='schedule_refresh_gross_value_added_value_for_fdi_investment_projects',
# )
if settings.ENABLE_ESTIMATED_LAND_DATE_REMINDERS_EMAIL_DELIVERY_STATUS:
job_scheduler(
function=update_notify_email_delivery_status_for_estimated_land_date,
max_retries=5,
queue_name=LONG_RUNNING_QUEUE,
retry_backoff=True,
retry_intervals=30,
job_timeout=HALF_DAY_IN_SECONDS,
cron=EVERY_NINE_THIRTY_AM_ON_FIRST_SECOND_THIRD_FOURTH_OF_EACH_MONTH,
description='Start of month update notify email delivery status for estimated land '
'date',
)
if settings.ENABLE_NO_RECENT_INTERACTION_EMAIL_DELIVERY_STATUS:
job_scheduler(
function=update_notify_email_delivery_status_for_no_recent_interaction,
max_retries=5,
queue_name=LONG_RUNNING_QUEUE,
retry_backoff=True,
retry_intervals=30,
job_timeout=HALF_DAY_IN_SECONDS,
cron=EVERY_TEN_AM,
description='Daily update notify email delivery status for no recent interaction',
)
if settings.ENABLE_DAILY_OPENSEARCH_SYNC:
job_scheduler(
function=sync_all_models,
cron=EVERY_ONE_AM,
description='Daily OpenSearch sync',
)
if settings.ENABLE_DAILY_HIERARCHY_ROLLOUT:
dnb_modified_on_before = datetime(
year=2019,
month=10,
day=24,
hour=23,
minute=59,
second=59,
tzinfo=timezone.utc,
)
job_scheduler(
function=schedule_sync_outdated_companies_with_dnb,
function_kwargs={
'dnb_modified_on_before': dnb_modified_on_before,
'fields_to_update': ['global_ultimate_duns_number'],
'limit': settings.DAILY_HIERARCHY_ROLLOUT_LIMIT,
'simulate': False,
'max_requests': 5,
},
cron=EVERY_ONE_AM,
description='dnb hierarchies backfill',
)
if settings.ENABLE_NO_RECENT_EXPORT_INTERACTION_REMINDERS:
job_scheduler(
function=generate_no_recent_export_interaction_reminders,
max_retries=5,
queue_name=LONG_RUNNING_QUEUE,
retry_backoff=True,
retry_intervals=30,
cron=EVERY_EIGHT_AM,
description='Daily generate no recent export interaction reminders',
)
if settings.ENABLE_NO_RECENT_INTERACTION_REMINDERS:
job_scheduler(
function=generate_no_recent_interaction_reminders,
max_retries=5,
queue_name=LONG_RUNNING_QUEUE,
retry_backoff=True,
retry_intervals=30,
cron=EVERY_EIGHT_AM,
job_timeout=HALF_DAY_IN_SECONDS,
description='Daily generate no recent interaction reminders',
)
if settings.ENABLE_NO_RECENT_EXPORT_INTERACTION_REMINDERS_EMAIL_DELIVERY_STATUS:
job_scheduler(
function=update_notify_email_delivery_status_for_no_recent_export_interaction,
max_retries=5,
queue_name=LONG_RUNNING_QUEUE,
retry_backoff=True,
retry_intervals=30,
cron=EVERY_TEN_AM,
description='Daily update of no recent export interaction reminder email status',
)
schedule_email_ingestion_tasks()
schedule_new_export_interaction_jobs()
schedule_export_win_customer_response_token_jobs()
schedule_export_win_auto_resend_client_email()
schedule_user_reminder_migration()
schedule_update_company_export_potential_from_csv()
def schedule_email_ingestion_tasks():
if settings.ENABLE_MAILBOX_PROCESSING:
job_scheduler(
function=process_mailbox_emails,
cron=EVERY_TEN_MINUTES,
description='DataHub Email ingestion tasks process mailbox emails',
)
def schedule_new_export_interaction_jobs():
"""Schedule new export interaction jobs."""
if settings.ENABLE_NEW_EXPORT_INTERACTION_REMINDERS:
job_scheduler(
function=generate_new_export_interaction_reminders,
max_retries=5,
queue_name=LONG_RUNNING_QUEUE,
retry_backoff=True,
retry_intervals=30,
cron=EVERY_EIGHT_AM,
description='Daily generate new export interaction reminders',
)
if settings.ENABLE_NEW_EXPORT_INTERACTION_REMINDERS_EMAIL_DELIVERY_STATUS:
job_scheduler(
function=update_notify_email_delivery_status_for_new_export_interaction,
max_retries=5,
queue_name=LONG_RUNNING_QUEUE,
retry_backoff=True,
retry_intervals=30,
cron=EVERY_TEN_AM,
description='Daily update of new export interaction reminder email status',
)
def schedule_user_reminder_migration():
job_scheduler(
function=run_ita_users_migration,
max_retries=5,
queue_name=LONG_RUNNING_QUEUE,
retry_backoff=True,
retry_intervals=30,
cron=EVERY_TEN_PM,
description='Daily migrate ITA users to receive notifications',
)
job_scheduler(
function=run_post_users_migration,
max_retries=5,
queue_name=LONG_RUNNING_QUEUE,
retry_backoff=True,
retry_intervals=30,
cron=EVERY_ELEVEN_PM,
description='Daily migrate post users to receive notifications',
)
def schedule_export_win_customer_response_token_jobs():
"""Schedule update export win customer response token jobs."""
job_scheduler(
function=update_notify_email_delivery_status_for_customer_response_token,
max_retries=5,
queue_name=LONG_RUNNING_QUEUE,
retry_backoff=True,
retry_intervals=30,
cron=EVERY_TWO_AM,
description='Scheduled update of export win customer response email delivery status',
)
job_scheduler(
function=update_notify_email_delivery_status_for_customer_response,
max_retries=5,
queue_name=LONG_RUNNING_QUEUE,
retry_backoff=True,
retry_intervals=30,
cron=EVERY_THREE_AM,
description='Scheduled update of export win lead officer email delivery status',
)
def schedule_export_win_auto_resend_client_email():
"""Schedule auto resend client email from unconfirmed win"""
job_scheduler(
function=auto_resend_client_email_from_unconfirmed_win,
max_retries=5,
queue_name=LONG_RUNNING_QUEUE,
retry_backoff=True,
retry_intervals=30,
cron=EVERY_TWO_AM,
description='Scheduled auto resend client email from unconfirmed win',
)
def schedule_update_company_export_potential_from_csv():
"""Schedule ingestion of export potential data"""
job_scheduler(
function=update_company_export_potential_from_csv,
function_kwargs={
'bucket': 'upload.datahub.trade.gov.uk',
'object_key': f'export_propensity_{datetime.now().strftime("%Y_%m_%d")}.csv',
},
queue_name=LONG_RUNNING_QUEUE,
cron=EVERY_ELEVEN_PM,
description='Scheduled ingestion of export potential data',
)
def cancel_existing_cron_jobs():
logger.info('Cancel any existing rq scheduled cron jobs')
with DataHubScheduler() as scheduler:
scheduler.cancel_cron_jobs()
def create_rqscheduler_command():
command = f'rqscheduler --url {settings.REDIS_BASE_URL} --verbose --interval 60'
return command
schedule_jobs()
os.system(create_rqscheduler_command())