feature(backups): simplest jobs intergration in tasks: created and finished

This commit is contained in:
Houkime 2023-04-21 11:59:15 +00:00 committed by Inex Code
parent 4572c00640
commit fa86c45bd0
2 changed files with 36 additions and 0 deletions

View file

@ -0,0 +1,31 @@
from typing import Optional
from selfprivacy_api.jobs import Jobs, Job, JobStatus
from selfprivacy_api.services.service import Service
def backup_job_type(service: Service):
return f"services.{service.get_id()}.backup"
def add_backup_job(service: Service) -> Job:
display_name = service.get_display_name()
job = Jobs.add(
type_id=backup_job_type(service),
name=f"Backup {display_name}",
description=f"Backing up {display_name}",
)
return job
def get_job_by_type(type_id: str) -> Optional[Job]:
for job in Jobs.get_jobs():
if job.type_id == type_id and job.status in [
JobStatus.CREATED,
JobStatus.RUNNING,
]:
return job
def get_backup_job(service: Service) -> Optional[Job]:
return get_job_by_type(backup_job_type(service))

View file

@ -4,6 +4,8 @@ from selfprivacy_api.models.backup.snapshot import Snapshot
from selfprivacy_api.utils.huey import huey
from selfprivacy_api.services.service import Service
from selfprivacy_api.backup import Backups
from selfprivacy_api.backup.jobs import get_backup_job, add_backup_job
from selfprivacy_api.jobs import Jobs, JobStatus
def validate_datetime(dt: datetime):
@ -21,7 +23,10 @@ def validate_datetime(dt: datetime):
# huey tasks need to return something
@huey.task()
def start_backup(service: Service) -> bool:
add_backup_job(service)
Backups.back_up(service)
job = get_backup_job(service)
Jobs.update(job, status=JobStatus.FINISHED)
return True