feature(backups): integration between restore and jobs

This commit is contained in:
Houkime 2023-06-07 15:05:58 +00:00
parent 98de85e569
commit 7f984b678f
3 changed files with 45 additions and 5 deletions

View file

@ -13,7 +13,12 @@ from selfprivacy_api.graphql.queries.providers import BackupProvider
from selfprivacy_api.backup.providers.provider import AbstractBackupProvider from selfprivacy_api.backup.providers.provider import AbstractBackupProvider
from selfprivacy_api.backup.providers import get_provider from selfprivacy_api.backup.providers import get_provider
from selfprivacy_api.backup.storage import Storage from selfprivacy_api.backup.storage import Storage
from selfprivacy_api.backup.jobs import get_backup_job, add_backup_job from selfprivacy_api.backup.jobs import (
get_backup_job,
add_backup_job,
get_restore_job,
add_restore_job,
)
from selfprivacy_api.jobs import Jobs, JobStatus from selfprivacy_api.jobs import Jobs, JobStatus
@ -285,6 +290,7 @@ class Backups:
for snapshot in upstream_snapshots: for snapshot in upstream_snapshots:
Storage.cache_snapshot(snapshot) Storage.cache_snapshot(snapshot)
# to be deprecated/internalized in favor of restore_snapshot()
@staticmethod @staticmethod
def restore_service_from_snapshot(service: Service, snapshot_id: str): def restore_service_from_snapshot(service: Service, snapshot_id: str):
repo_name = service.get_id() repo_name = service.get_id()
@ -294,9 +300,21 @@ class Backups:
@staticmethod @staticmethod
def restore_snapshot(snapshot: Snapshot): def restore_snapshot(snapshot: Snapshot):
Backups.restore_service_from_snapshot( service = get_service_by_id(snapshot.service_name)
get_service_by_id(snapshot.service_name), snapshot.id
) job = get_restore_job(service)
if job is None:
job = add_restore_job(snapshot)
Jobs.update(job, status=JobStatus.RUNNING)
try:
Backups.restore_service_from_snapshot(service, snapshot.id)
service.post_restore()
except Exception as e:
Jobs.update(job, status=JobStatus.ERROR)
raise e
Jobs.update(job, status=JobStatus.FINISHED)
@staticmethod @staticmethod
def service_snapshot_size(service: Service, snapshot_id: str) -> float: def service_snapshot_size(service: Service, snapshot_id: str) -> float:

View file

@ -1,7 +1,9 @@
from typing import Optional, List from typing import Optional, List
from selfprivacy_api.models.backup.snapshot import Snapshot
from selfprivacy_api.jobs import Jobs, Job, JobStatus from selfprivacy_api.jobs import Jobs, Job, JobStatus
from selfprivacy_api.services.service import Service from selfprivacy_api.services.service import Service
from selfprivacy_api.services import get_service_by_id
def job_type_prefix(service: Service) -> str: def job_type_prefix(service: Service) -> str:
@ -47,6 +49,23 @@ def add_backup_job(service: Service) -> Job:
return job return job
def add_restore_job(snapshot: Snapshot) -> Job:
service = get_service_by_id(snapshot.service_name)
if is_something_queued_for(service):
message = (
f"Cannot start a restore of {service.get_id()}, another operation is queued: "
+ get_jobs_by_service(service)[0].type_id
)
raise ValueError(message)
display_name = service.get_display_name()
job = Jobs.add(
type_id=restore_job_type(service),
name=f"Restore {display_name}",
description=f"restoring {display_name} from {snapshot.id}",
)
return job
def get_job_by_type(type_id: str) -> Optional[Job]: def get_job_by_type(type_id: str) -> Optional[Job]:
for job in Jobs.get_jobs(): for job in Jobs.get_jobs():
if job.type_id == type_id and job.status in [ if job.type_id == type_id and job.status in [

View file

@ -2,9 +2,10 @@ from datetime import datetime
from selfprivacy_api.models.backup.snapshot import Snapshot from selfprivacy_api.models.backup.snapshot import Snapshot
from selfprivacy_api.utils.huey import huey from selfprivacy_api.utils.huey import huey
from selfprivacy_api.services import get_service_by_id
from selfprivacy_api.services.service import Service from selfprivacy_api.services.service import Service
from selfprivacy_api.backup import Backups from selfprivacy_api.backup import Backups
from selfprivacy_api.backup.jobs import add_backup_job from selfprivacy_api.backup.jobs import add_backup_job, add_restore_job
def validate_datetime(dt: datetime): def validate_datetime(dt: datetime):
@ -32,6 +33,8 @@ def start_backup(service: Service) -> bool:
@huey.task() @huey.task()
def restore_snapshot(snapshot: Snapshot) -> bool: def restore_snapshot(snapshot: Snapshot) -> bool:
add_restore_job(snapshot)
Backups.restore_snapshot(snapshot) Backups.restore_snapshot(snapshot)
return True return True