Coverage for portality / tasks / check_latest_es_backup.py: 77%
35 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-04 09:41 +0100
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-04 09:41 +0100
1from portality import models, app_email
2from portality.background import BackgroundTask, BackgroundApi
3from portality.core import app, es_connection
4from portality.lib.es_snapshot import ESSnapshotsClient
5from portality.tasks.helpers import background_helper
6from portality.tasks.redis_huey import scheduled_short_queue as queue
9class CheckLatestESBackupBackgroundTask(BackgroundTask):
11 __action__ = "check_latest_es_backup"
13 def run(self):
14 """
15 Execute the task as specified by the background_job
16 :return:
17 """
19 try:
20 client = ESSnapshotsClient(es_connection, app.config['ELASTIC_SEARCH_SNAPSHOT_REPOSITORY'])
21 client.check_today_snapshot()
22 except Exception as e:
23 app_email.send_mail(
24 to=[app.config.get('ADMIN_EMAIL', 'sysadmin@cottagelabs.com')],
25 fro=app.config.get('SYSTEM_EMAIL_FROM', 'helpdesk@doaj.org'),
26 subject='Alert: DOAJ ElasticSearch backup failure',
27 msg_body="Today's ES snapshot has not been found by the checking task. Error: \n" + str(e)
28 )
29 raise e
31 def cleanup(self):
32 """
33 Cleanup after a successful OR failed run of the task
34 :return:
35 """
36 pass
38 @classmethod
39 def prepare(cls, username, **kwargs):
40 """
41 Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob,
42 or fail with a suitable exception
44 :param kwargs: arbitrary keyword arguments pertaining to this task type
45 :return: a BackgroundJob instance representing this task
46 """
48 # first prepare a job record
49 return background_helper.create_job(username, cls.__action__, queue_id=huey_helper.queue_id)
51 @classmethod
52 def submit(cls, background_job):
53 """
54 Submit the specified BackgroundJob to the background queue
56 :param background_job: the BackgroundJob instance
57 :return:
58 """
59 background_job.save()
60 check_latest_es_backup.schedule(args=(background_job.id,), delay=app.config.get('HUEY_ASYNC_DELAY', 10))
63huey_helper = CheckLatestESBackupBackgroundTask.create_huey_helper(queue)
66@huey_helper.register_schedule
67def scheduled_check_latest_es_backup():
68 user = app.config.get("SYSTEM_USERNAME")
69 job = CheckLatestESBackupBackgroundTask.prepare(user)
70 CheckLatestESBackupBackgroundTask.submit(job)
73@huey_helper.register_execute(is_load_config=False)
74def check_latest_es_backup(job_id):
75 job = models.BackgroundJob.pull(job_id)
76 task = CheckLatestESBackupBackgroundTask(job)
77 BackgroundApi.execute(task)