Coverage for portality/tasks/check_latest_es_backup.py: 80%
41 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-19 18:38 +0100
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-19 18:38 +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
12class CheckLatestESBackupBackgroundTask(BackgroundTask):
14 __action__ = "check_latest_es_backup"
16 def run(self):
17 """
18 Execute the task as specified by the background_job
19 :return:
20 """
22 # Connection to the ES index
23 conn = Connection(app.config["ELASTIC_SEARCH_HOST"], index='_snapshot')
25 try:
26 client = ESSnapshotsClient(conn, app.config['ELASTIC_SEARCH_SNAPSHOT_REPOSITORY'])
27 client.check_today_snapshot()
28 except Exception as e:
29 app_email.send_mail(
30 to=[app.config.get('ADMIN_EMAIL', 'sysadmin@cottagelabs.com')],
31 fro=app.config.get('SYSTEM_EMAIL_FROM', 'helpdesk@doaj.org'),
32 subject='Alert: DOAJ ElasticSearch backup failure',
33 msg_body="Today's ES snapshot has not been found by the checking task. Error: \n" + str(e)
34 )
35 raise e
37 def cleanup(self):
38 """
39 Cleanup after a successful OR failed run of the task
40 :return:
41 """
42 pass
44 @classmethod
45 def prepare(cls, username, **kwargs):
46 """
47 Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob,
48 or fail with a suitable exception
50 :param kwargs: arbitrary keyword arguments pertaining to this task type
51 :return: a BackgroundJob instance representing this task
52 """
54 # first prepare a job record
55 job = models.BackgroundJob()
56 job.user = username
57 job.action = cls.__action__
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 check_latest_es_backup.schedule(args=(background_job.id,), delay=10)
72@main_queue.periodic_task(schedule("check_latest_es_backup"))
73@write_required(script=True)
74def scheduled_check_latest_es_backup():
75 user = app.config.get("SYSTEM_USERNAME")
76 job = CheckLatestESBackupBackgroundTask.prepare(user)
77 CheckLatestESBackupBackgroundTask.submit(job)
80@main_queue.task()
81@write_required(script=True)
82def check_latest_es_backup(job_id):
83 job = models.BackgroundJob.pull(job_id)
84 task = CheckLatestESBackupBackgroundTask(job)
85 BackgroundApi.execute(task)