Coverage for portality/tasks/request_es_backup.py: 0%
45 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-22 15:59 +0100
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-22 15:59 +0100
1from portality import models, app_email
2from portality.core import app
4from esprit.raw import Connection
5from esprit.snapshot import ESSnapshotsClient
7from portality.tasks.redis_huey import main_queue, schedule
8from portality.decorators import write_required
9from portality.background import BackgroundTask, BackgroundApi
10# FIXME: update for index-per-type
13class RequestESBackupBackgroundTask(BackgroundTask):
15 __action__ = "request_es_backup"
17 def run(self):
18 """
19 Execute the task as specified by the background_job
20 :return:
21 """
23 # Connection to the ES index
24 conn = Connection(app.config.get("ELASTIC_SEARCH_HOST"), index='_snapshot')
26 try:
27 client = ESSnapshotsClient(conn, app.config['ELASTIC_SEARCH_SNAPSHOT_REPOSITORY'])
28 resp = client.request_snapshot()
29 if resp.status_code == 200:
30 job = self.background_job
31 job.add_audit_message("ElasticSearch backup requested. Response: " + resp.text)
32 else:
33 raise Exception("Status code {0} received from snapshots plugin.".format(resp.text))
35 except Exception as e:
36 app_email.send_mail(
37 to=[app.config.get('ADMIN_EMAIL', 'sysadmin@cottagelabs.com')],
38 fro=app.config.get('SYSTEM_EMAIL_FROM', 'helpdesk@doaj.org'),
39 subject='Alert: DOAJ ElasticSearch backup failure',
40 msg_body="The ElasticSearch snapshot could not requested. Error: \n" + str(e)
41 )
42 raise e
44 def cleanup(self):
45 """
46 Cleanup after a successful OR failed run of the task
47 :return:
48 """
49 pass
51 @classmethod
52 def prepare(cls, username, **kwargs):
53 """
54 Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob,
55 or fail with a suitable exception
57 :param kwargs: arbitrary keyword arguments pertaining to this task type
58 :return: a BackgroundJob instance representing this task
59 """
61 # first prepare a job record
62 job = models.BackgroundJob()
63 job.user = username
64 job.action = cls.__action__
65 return job
67 @classmethod
68 def submit(cls, background_job):
69 """
70 Submit the specified BackgroundJob to the background queue
72 :param background_job: the BackgroundJob instance
73 :return:
74 """
75 background_job.save()
76 request_es_backup.schedule(args=(background_job.id,), delay=10)
79@main_queue.periodic_task(schedule("request_es_backup"))
80@write_required(script=True)
81def scheduled_request_es_backup():
82 user = app.config.get("SYSTEM_USERNAME")
83 job = RequestESBackupBackgroundTask.prepare(user)
84 RequestESBackupBackgroundTask.submit(job)
87@main_queue.task()
88@write_required(script=True)
89def request_es_backup(job_id):
90 job = models.BackgroundJob.pull(job_id)
91 task = RequestESBackupBackgroundTask(job)
92 BackgroundApi.execute(task)