refactor(backups): async quota setting

This commit is contained in:
Houkime 2023-11-17 15:22:21 +00:00 committed by Inex Code
parent 35a7497855
commit e0b04217cd
2 changed files with 41 additions and 3 deletions

View file

@ -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,

View file

@ -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="",