refactor(backups): only async snapshot trimming

This commit is contained in:
Houkime 2023-11-17 15:39:21 +00:00 committed by Inex Code
parent e0b04217cd
commit d549e29304
4 changed files with 13 additions and 11 deletions

View file

@ -425,7 +425,10 @@ class Backups:
yearly=Backups._standardize_quotas(quotas.yearly), # type: ignore yearly=Backups._standardize_quotas(quotas.yearly), # type: ignore
) )
) )
# do not prune all autosnaps right away, this will be done by an async task
@staticmethod
def prune_all_autosnaps() -> None:
for service in get_all_services(): for service in get_all_services():
Backups._prune_auto_snaps(service) Backups._prune_auto_snaps(service)

View file

@ -43,16 +43,13 @@ def start_backup(
@huey.task() @huey.task()
def set_autobackup_quotas(quotas: AutobackupQuotas, job: Job) -> bool: def prune_autobackup_snapshots(quotas: AutobackupQuotas, job: Job) -> bool:
job = Jobs.add( """
name="trimming autobackup snapshots", Remove all autobackup snapshots that do not fit into quotas set
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) Jobs.update(job, JobStatus.RUNNING)
try: try:
Backups.set_autobackup_quotas(quotas) Backups.prune_all_autosnaps()
except Exception as e: except Exception as e:
Jobs.update(job, JobStatus.ERROR, error=type(e).__name__ + ":" + str(e)) Jobs.update(job, JobStatus.ERROR, error=type(e).__name__ + ":" + str(e))
return False return False

View file

@ -23,7 +23,7 @@ from selfprivacy_api.services import get_service_by_id
from selfprivacy_api.backup.tasks import ( from selfprivacy_api.backup.tasks import (
start_backup, start_backup,
restore_snapshot, restore_snapshot,
set_autobackup_quotas, prune_autobackup_snapshots,
) )
from selfprivacy_api.backup.jobs import add_backup_job, add_restore_job from selfprivacy_api.backup.jobs import add_backup_job, add_restore_job
@ -116,8 +116,9 @@ class BackupMutations:
) )
try: try:
# this is async and can fail with only a job to report the error Backups.set_autobackup_quotas(quotas)
set_autobackup_quotas(quotas, job) # this task is async and can fail with only a job to report the error
prune_autobackup_snapshots(job)
return GenericBackupConfigReturn( return GenericBackupConfigReturn(
success=True, success=True,
message="", message="",

View file

@ -651,6 +651,7 @@ def test_too_many_auto(backups, dummy_service):
# Retroactivity # Retroactivity
quota.last = 1 quota.last = 1
Backups.set_autobackup_quotas(quota) Backups.set_autobackup_quotas(quota)
Backups.prune_all_autosnaps()
snaps = Backups.get_snapshots(dummy_service) snaps = Backups.get_snapshots(dummy_service)
assert len(snaps) == 1 assert len(snaps) == 1