Coverage for portality/tasks/prune_es_backups.py: 52%
48 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
2from portality.core import app
3from portality.lib import dates
5from portality.tasks.redis_huey import long_running, schedule
6from portality.decorators import write_required
7from portality.background import BackgroundTask, BackgroundApi
9from esprit.raw import Connection
10from esprit.snapshot import ESSnapshotsClient
12from datetime import datetime, timedelta
13# FIXME: update for index-per-type
16class PruneESBackupsBackgroundTask(BackgroundTask):
18 __action__ = "prune_es_backups"
20 def run(self):
21 """
22 Execute the task as specified by the background_job
23 :return:
24 """
25 job = self.background_job
27 # Connection to the ES index
28 conn = Connection(app.config.get("ELASTIC_SEARCH_HOST"), index='_snapshot')
30 snap_ttl = app.config.get('ELASTIC_SEARCH_SNAPSHOT_TTL', 366)
31 snap_thresh = datetime.utcnow() - timedelta(days=snap_ttl)
32 job.add_audit_message('Deleting backups older than {}'.format(dates.format(snap_thresh)))
34 client = ESSnapshotsClient(conn, app.config['ELASTIC_SEARCH_SNAPSHOT_REPOSITORY'])
35 client.prune_snapshots(snap_ttl, self.report_deleted_closure(job))
37 def cleanup(self):
38 """
39 Cleanup after a successful OR failed run of the task
40 :return:
41 """
42 pass
44 @staticmethod
45 def report_deleted_closure(job):
46 """ Report the deletion to the background task audit log """
47 def _report_deleted_callback(snapshot, status_code, result):
48 job.add_audit_message('Deleted snapshot {0}, Status code: {1}, Successful: {2}'.format(snapshot.name, status_code, result))
50 return _report_deleted_callback
52 @classmethod
53 def prepare(cls, username, **kwargs):
54 """
55 Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob,
56 or fail with a suitable exception
58 :param username: The user this job will run under
59 :param kwargs: arbitrary keyword arguments pertaining to this task type
60 :return: a BackgroundJob instance representing this task
61 """
63 # first prepare a job record
64 job = models.BackgroundJob()
65 job.user = username
66 job.action = cls.__action__
67 return job
69 @classmethod
70 def submit(cls, background_job):
71 """
72 Submit the specified BackgroundJob to the background queue
74 :param background_job: the BackgroundJob instance
75 :return:
76 """
77 background_job.save()
78 prune_es_backups.schedule(args=(background_job.id,), delay=10)
81@long_running.periodic_task(schedule("prune_es_backups"))
82@write_required(script=True)
83def scheduled_prune_es_backups():
84 user = app.config.get("SYSTEM_USERNAME")
85 job = PruneESBackupsBackgroundTask.prepare(user)
86 PruneESBackupsBackgroundTask.submit(job)
89@long_running.task()
90@write_required(script=True)
91def prune_es_backups(job_id):
92 job = models.BackgroundJob.pull(job_id)
93 task = PruneESBackupsBackgroundTask(job)
94 BackgroundApi.execute(task)