Coverage for portality / tasks / request_es_backup.py: 42%
40 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.lib.es_snapshot import ESSnapshotsClient
3from portality import models, app_email
4from portality.background import BackgroundTask, BackgroundApi
5from portality.core import app, es_connection
6from portality.tasks.helpers import background_helper
7from portality.tasks.redis_huey import scheduled_short_queue as queue
10class RequestESBackupBackgroundTask(BackgroundTask):
12 __action__ = "request_es_backup"
14 def run(self):
15 """
16 Execute the task as specified by the background_job
17 :return:
18 """
20 try:
21 client = ESSnapshotsClient(es_connection, app.config['ELASTIC_SEARCH_SNAPSHOT_REPOSITORY'])
22 resp, success = client.request_snapshot()
23 if success:
24 job = self.background_job
25 job.add_audit_message("ElasticSearch backup requested. Response: " + str(resp))
26 else:
27 raise Exception("Exception {0} received from snapshots plugin.".format(resp))
29 except Exception as e:
30 app_email.send_mail(
31 to=[app.config.get('ADMIN_EMAIL', 'sysadmin@cottagelabs.com')],
32 fro=app.config.get('SYSTEM_EMAIL_FROM', 'helpdesk@doaj.org'),
33 subject='Alert: DOAJ ElasticSearch backup failure',
34 msg_body="The ElasticSearch snapshot could not requested. Error: \n" + str(e)
35 )
36 raise e
38 def cleanup(self):
39 """
40 Cleanup after a successful OR failed run of the task
41 :return:
42 """
43 pass
45 @classmethod
46 def prepare(cls, username, **kwargs):
47 """
48 Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob,
49 or fail with a suitable exception
51 :param kwargs: arbitrary keyword arguments pertaining to this task type
52 :return: a BackgroundJob instance representing this task
53 """
55 # first prepare a job record
56 job = background_helper.create_job(username, cls.__action__,
57 queue_id=huey_helper.queue_id, )
58 return job
60 @classmethod
61 def submit(cls, background_job):
62 """
63 Submit the specified BackgroundJob to the background queue
65 :param background_job: the BackgroundJob instance
66 :return:
67 """
68 background_job.save()
69 request_es_backup.schedule(args=(background_job.id,), delay=app.config.get('HUEY_ASYNC_DELAY', 10))
72huey_helper = RequestESBackupBackgroundTask.create_huey_helper(queue)
75@huey_helper.register_schedule
76def scheduled_request_es_backup():
77 user = app.config.get("SYSTEM_USERNAME")
78 job = RequestESBackupBackgroundTask.prepare(user)
79 RequestESBackupBackgroundTask.submit(job)
82@huey_helper.register_execute(is_load_config=False)
83def request_es_backup(job_id):
84 job = models.BackgroundJob.pull(job_id)
85 task = RequestESBackupBackgroundTask(job)
86 BackgroundApi.execute(task)