From fa86c45bd089d5d6593f40c6038601b80cca03bc Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 21 Apr 2023 11:59:15 +0000 Subject: [PATCH] feature(backups): simplest jobs intergration in tasks: created and finished --- selfprivacy_api/backup/jobs.py | 31 +++++++++++++++++++++++++++++++ selfprivacy_api/backup/tasks.py | 5 +++++ 2 files changed, 36 insertions(+) create mode 100644 selfprivacy_api/backup/jobs.py diff --git a/selfprivacy_api/backup/jobs.py b/selfprivacy_api/backup/jobs.py new file mode 100644 index 0000000..a90c4bd --- /dev/null +++ b/selfprivacy_api/backup/jobs.py @@ -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)) diff --git a/selfprivacy_api/backup/tasks.py b/selfprivacy_api/backup/tasks.py index 3236029..111f255 100644 --- a/selfprivacy_api/backup/tasks.py +++ b/selfprivacy_api/backup/tasks.py @@ -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