From e0b04217cd25af2944000c34cb01e10235e1ad35 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 17 Nov 2023 15:22:21 +0000 Subject: [PATCH] refactor(backups): async quota setting --- selfprivacy_api/backup/tasks.py | 27 ++++++++++++++++++- .../graphql/mutations/backup_mutations.py | 17 ++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/selfprivacy_api/backup/tasks.py b/selfprivacy_api/backup/tasks.py index bdf6c9f..f3de55a 100644 --- a/selfprivacy_api/backup/tasks.py +++ b/selfprivacy_api/backup/tasks.py @@ -3,13 +3,19 @@ The tasks module contains the worker tasks that are used to back up and restore """ from datetime import datetime, timezone -from selfprivacy_api.graphql.common_types.backup import RestoreStrategy, BackupReason +from selfprivacy_api.graphql.common_types.backup import ( + RestoreStrategy, + BackupReason, + AutobackupQuotas, +) from selfprivacy_api.models.backup.snapshot import Snapshot from selfprivacy_api.utils.huey import huey from huey import crontab from selfprivacy_api.services.service import Service from selfprivacy_api.backup import Backups +from selfprivacy_api.jobs import Jobs, JobStatus, Job + SNAPSHOT_CACHE_TTL_HOURS = 6 @@ -36,6 +42,25 @@ def start_backup( return True +@huey.task() +def set_autobackup_quotas(quotas: AutobackupQuotas, job: Job) -> bool: + job = Jobs.add( + name="trimming autobackup snapshots", + type_id="backups.autobackup_trimming", + description="Pruning the excessive snapshots after the new autobackup quotas are set", + status=JobStatus.RUNNING, + ) + Jobs.update(job, JobStatus.RUNNING) + try: + Backups.set_autobackup_quotas(quotas) + except Exception as e: + Jobs.update(job, JobStatus.ERROR, error=type(e).__name__ + ":" + str(e)) + return False + + Jobs.update(job, JobStatus.FINISHED) + return True + + @huey.task() def restore_snapshot( snapshot: Snapshot, diff --git a/selfprivacy_api/graphql/mutations/backup_mutations.py b/selfprivacy_api/graphql/mutations/backup_mutations.py index 954d0d4..41f37b5 100644 --- a/selfprivacy_api/graphql/mutations/backup_mutations.py +++ b/selfprivacy_api/graphql/mutations/backup_mutations.py @@ -1,6 +1,8 @@ import typing import strawberry +from selfprivacy_api.jobs import Jobs + from selfprivacy_api.graphql import IsAuthenticated from selfprivacy_api.graphql.mutations.mutation_interface import ( GenericMutationReturn, @@ -18,7 +20,11 @@ from selfprivacy_api.graphql.common_types.backup import ( from selfprivacy_api.backup import Backups from selfprivacy_api.services import get_service_by_id -from selfprivacy_api.backup.tasks import start_backup, restore_snapshot +from selfprivacy_api.backup.tasks import ( + start_backup, + restore_snapshot, + set_autobackup_quotas, +) from selfprivacy_api.backup.jobs import add_backup_job, add_restore_job @@ -103,8 +109,15 @@ class BackupMutations: To disable autobackup use autobackup period setting, not this mutation. """ + job = Jobs.add( + name="trimming autobackup snapshots", + type_id="backups.autobackup_trimming", + description="Pruning the excessive snapshots after the new autobackup quotas are set", + ) + try: - Backups.set_autobackup_quotas(quotas) + # this is async and can fail with only a job to report the error + set_autobackup_quotas(quotas, job) return GenericBackupConfigReturn( success=True, message="",