Coverage for portality / tasks / prune_es_backups.py: 49%
43 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 datetime import timedelta
3from portality.lib.es_snapshot import ESSnapshotsClient
5from portality import models
6from portality.background import BackgroundTask, BackgroundApi
7from portality.core import app, es_connection
8from portality.lib import dates
9from portality.tasks.helpers import background_helper
10from portality.tasks.redis_huey import scheduled_short_queue as queue
13class PruneESBackupsBackgroundTask(BackgroundTask):
15 __action__ = "prune_es_backups"
17 def run(self):
18 """
19 Execute the task as specified by the background_job
20 :return:
21 """
22 job = self.background_job
24 snap_ttl = app.config.get('ELASTIC_SEARCH_SNAPSHOT_TTL', 366)
25 snap_thresh = dates.now() - timedelta(days=snap_ttl)
26 job.add_audit_message('Deleting backups older than {}'.format(dates.format(snap_thresh)))
28 client = ESSnapshotsClient(es_connection, app.config['ELASTIC_SEARCH_SNAPSHOT_REPOSITORY'])
29 client.prune_snapshots(snap_ttl, self.report_deleted_closure(job))
31 def cleanup(self):
32 """
33 Cleanup after a successful OR failed run of the task
34 :return:
35 """
36 pass
38 @staticmethod
39 def report_deleted_closure(job):
40 """ Report the deletion to the background task audit log """
41 def _report_deleted_callback(snapshot, status_code, result):
42 job.add_audit_message('Deleted snapshot {0}, Status code: {1}, Successful: {2}'.format(snapshot.name,
43 status_code, result))
45 return _report_deleted_callback
47 @classmethod
48 def prepare(cls, username, **kwargs):
49 """
50 Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob,
51 or fail with a suitable exception
53 :param username: The user this job will run under
54 :param kwargs: arbitrary keyword arguments pertaining to this task type
55 :return: a BackgroundJob instance representing this task
56 """
58 # first prepare a job record
59 job = background_helper.create_job(username, cls.__action__,
60 queue_id=huey_helper.queue_id)
61 return job
63 @classmethod
64 def submit(cls, background_job):
65 """
66 Submit the specified BackgroundJob to the background queue
68 :param background_job: the BackgroundJob instance
69 :return:
70 """
71 background_job.save()
72 prune_es_backups.schedule(args=(background_job.id,), delay=app.config.get('HUEY_ASYNC_DELAY', 10))
75huey_helper = PruneESBackupsBackgroundTask.create_huey_helper(queue)
78@huey_helper.register_schedule
79def scheduled_prune_es_backups():
80 user = app.config.get("SYSTEM_USERNAME")
81 job = PruneESBackupsBackgroundTask.prepare(user)
82 PruneESBackupsBackgroundTask.submit(job)
85@huey_helper.register_execute(is_load_config=False)
86def prune_es_backups(job_id):
87 job = models.BackgroundJob.pull(job_id)
88 task = PruneESBackupsBackgroundTask(job)
89 BackgroundApi.execute(task)