mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-29 07:21:27 +00:00
feature(backups): deny adding a backup job if another one is already queued
This commit is contained in:
parent
e25aa2cb33
commit
a6b3a5e590
|
@ -1,14 +1,39 @@
|
|||
from typing import Optional
|
||||
from typing import Optional, List
|
||||
|
||||
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 job_type_prefix(service: Service) -> str:
|
||||
return f"services.{service.get_id()}"
|
||||
|
||||
|
||||
def backup_job_type(service: Service) -> str:
|
||||
return f"{job_type_prefix(service)}.backup"
|
||||
|
||||
|
||||
def get_jobs_by_service(service: Service) -> List[Job]:
|
||||
result = []
|
||||
for job in Jobs.get_jobs():
|
||||
if job.type_id.startswith(job_type_prefix(service)) and job.status in [
|
||||
JobStatus.CREATED,
|
||||
JobStatus.RUNNING,
|
||||
]:
|
||||
result.append(job)
|
||||
return result
|
||||
|
||||
|
||||
def is_something_queued_for(service: Service) -> bool:
|
||||
return len(get_jobs_by_service(service)) != 0
|
||||
|
||||
|
||||
def add_backup_job(service: Service) -> Job:
|
||||
if is_something_queued_for(service):
|
||||
message = (
|
||||
f"Cannot start a backup 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=backup_job_type(service),
|
||||
|
|
Loading…
Reference in a new issue