2022-07-30 15:24:21 +00:00
|
|
|
"""
|
|
|
|
Jobs controller. It handles the jobs that are created by the user.
|
|
|
|
This is a singleton class holding the jobs list.
|
|
|
|
Jobs can be added and removed.
|
|
|
|
A single job can be updated.
|
|
|
|
A job is a dictionary with the following keys:
|
|
|
|
- id: unique identifier of the job
|
|
|
|
- name: name of the job
|
|
|
|
- description: description of the job
|
|
|
|
- status: status of the job
|
|
|
|
- created_at: date of creation of the job
|
|
|
|
- updated_at: date of last update of the job
|
|
|
|
- finished_at: date of finish of the job
|
|
|
|
- error: error message if the job failed
|
|
|
|
- result: result of the job
|
|
|
|
"""
|
|
|
|
import typing
|
|
|
|
import datetime
|
2022-08-25 17:03:56 +00:00
|
|
|
from uuid import UUID
|
2022-07-30 15:24:21 +00:00
|
|
|
import json
|
|
|
|
import uuid
|
|
|
|
from enum import Enum
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
from selfprivacy_api.utils import ReadUserData, UserDataFiles, WriteUserData
|
|
|
|
|
2022-07-30 15:24:21 +00:00
|
|
|
|
|
|
|
class JobStatus(Enum):
|
|
|
|
"""
|
|
|
|
Status of a job.
|
|
|
|
"""
|
|
|
|
|
|
|
|
CREATED = "CREATED"
|
|
|
|
RUNNING = "RUNNING"
|
|
|
|
FINISHED = "FINISHED"
|
|
|
|
ERROR = "ERROR"
|
|
|
|
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
class Job(BaseModel):
|
2022-07-30 15:24:21 +00:00
|
|
|
"""
|
|
|
|
Job class.
|
|
|
|
"""
|
|
|
|
|
2022-09-09 14:42:40 +00:00
|
|
|
uid: UUID
|
2022-08-25 17:03:56 +00:00
|
|
|
type_id: str
|
|
|
|
name: str
|
|
|
|
description: str
|
|
|
|
status: JobStatus
|
|
|
|
status_text: typing.Optional[str]
|
|
|
|
progress: typing.Optional[int]
|
|
|
|
created_at: datetime.datetime
|
|
|
|
updated_at: datetime.datetime
|
|
|
|
finished_at: typing.Optional[datetime.datetime]
|
|
|
|
error: typing.Optional[str]
|
|
|
|
result: typing.Optional[str]
|
2022-07-30 15:24:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Jobs:
|
|
|
|
"""
|
|
|
|
Jobs class.
|
|
|
|
"""
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def reset() -> None:
|
|
|
|
"""
|
|
|
|
Reset the jobs list.
|
|
|
|
"""
|
|
|
|
with WriteUserData(UserDataFiles.JOBS) as user_data:
|
|
|
|
user_data["jobs"] = []
|
|
|
|
|
|
|
|
@staticmethod
|
2022-07-30 15:24:21 +00:00
|
|
|
def add(
|
2022-08-25 17:03:56 +00:00
|
|
|
name: str,
|
|
|
|
type_id: str,
|
|
|
|
description: str,
|
|
|
|
status: JobStatus = JobStatus.CREATED,
|
|
|
|
status_text: str = "",
|
|
|
|
progress: int = 0,
|
2022-07-30 15:24:21 +00:00
|
|
|
) -> Job:
|
|
|
|
"""
|
|
|
|
Add a job to the jobs list.
|
|
|
|
"""
|
|
|
|
job = Job(
|
2022-09-09 14:42:40 +00:00
|
|
|
uid=uuid.uuid4(),
|
2022-07-30 15:24:21 +00:00
|
|
|
name=name,
|
2022-08-25 17:03:56 +00:00
|
|
|
type_id=type_id,
|
2022-07-30 15:24:21 +00:00
|
|
|
description=description,
|
|
|
|
status=status,
|
2022-08-25 17:03:56 +00:00
|
|
|
status_text=status_text,
|
|
|
|
progress=progress,
|
2022-07-30 15:24:21 +00:00
|
|
|
created_at=datetime.datetime.now(),
|
|
|
|
updated_at=datetime.datetime.now(),
|
|
|
|
finished_at=None,
|
|
|
|
error=None,
|
|
|
|
result=None,
|
|
|
|
)
|
2022-08-25 17:03:56 +00:00
|
|
|
with WriteUserData(UserDataFiles.JOBS) as user_data:
|
|
|
|
try:
|
|
|
|
if "jobs" not in user_data:
|
|
|
|
user_data["jobs"] = []
|
|
|
|
user_data["jobs"].append(json.loads(job.json()))
|
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
user_data["jobs"] = [json.loads(job.json())]
|
2022-07-30 15:24:21 +00:00
|
|
|
return job
|
|
|
|
|
2022-10-27 14:01:11 +00:00
|
|
|
@staticmethod
|
|
|
|
def remove(job: Job) -> None:
|
2022-07-30 15:24:21 +00:00
|
|
|
"""
|
|
|
|
Remove a job from the jobs list.
|
|
|
|
"""
|
2022-10-27 14:01:11 +00:00
|
|
|
Jobs.remove_by_uid(str(job.uid))
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2022-10-27 14:01:11 +00:00
|
|
|
@staticmethod
|
|
|
|
def remove_by_uid(job_uuid: str) -> bool:
|
2022-08-25 17:03:56 +00:00
|
|
|
"""
|
|
|
|
Remove a job from the jobs list.
|
|
|
|
"""
|
|
|
|
with WriteUserData(UserDataFiles.JOBS) as user_data:
|
|
|
|
if "jobs" not in user_data:
|
|
|
|
user_data["jobs"] = []
|
|
|
|
for i, j in enumerate(user_data["jobs"]):
|
|
|
|
if j["uid"] == job_uuid:
|
|
|
|
del user_data["jobs"][i]
|
|
|
|
return True
|
|
|
|
return False
|
2022-07-30 15:24:21 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
2022-07-30 15:24:21 +00:00
|
|
|
def update(
|
|
|
|
job: Job,
|
|
|
|
status: JobStatus,
|
2022-08-25 17:03:56 +00:00
|
|
|
status_text: typing.Optional[str] = None,
|
|
|
|
progress: typing.Optional[int] = None,
|
|
|
|
name: typing.Optional[str] = None,
|
|
|
|
description: typing.Optional[str] = None,
|
|
|
|
error: typing.Optional[str] = None,
|
|
|
|
result: typing.Optional[str] = None,
|
2022-07-30 15:24:21 +00:00
|
|
|
) -> Job:
|
|
|
|
"""
|
|
|
|
Update a job in the jobs list.
|
|
|
|
"""
|
|
|
|
if name is not None:
|
|
|
|
job.name = name
|
|
|
|
if description is not None:
|
|
|
|
job.description = description
|
2022-08-25 17:03:56 +00:00
|
|
|
if status_text is not None:
|
|
|
|
job.status_text = status_text
|
|
|
|
if progress is not None:
|
|
|
|
job.progress = progress
|
2022-07-30 15:24:21 +00:00
|
|
|
job.status = status
|
|
|
|
job.updated_at = datetime.datetime.now()
|
|
|
|
job.error = error
|
|
|
|
job.result = result
|
2022-08-25 17:03:56 +00:00
|
|
|
if status in (JobStatus.FINISHED, JobStatus.ERROR):
|
|
|
|
job.finished_at = datetime.datetime.now()
|
|
|
|
|
|
|
|
with WriteUserData(UserDataFiles.JOBS) as user_data:
|
|
|
|
if "jobs" not in user_data:
|
|
|
|
user_data["jobs"] = []
|
|
|
|
for i, j in enumerate(user_data["jobs"]):
|
|
|
|
if j["uid"] == str(job.uid):
|
|
|
|
user_data["jobs"][i] = json.loads(job.json())
|
|
|
|
break
|
|
|
|
|
2022-07-30 15:24:21 +00:00
|
|
|
return job
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_job(uid: str) -> typing.Optional[Job]:
|
2022-07-30 15:24:21 +00:00
|
|
|
"""
|
|
|
|
Get a job from the jobs list.
|
|
|
|
"""
|
2022-08-25 17:03:56 +00:00
|
|
|
with ReadUserData(UserDataFiles.JOBS) as user_data:
|
|
|
|
if "jobs" not in user_data:
|
|
|
|
user_data["jobs"] = []
|
|
|
|
for job in user_data["jobs"]:
|
|
|
|
if job["uid"] == uid:
|
|
|
|
return Job(**job)
|
2022-07-30 15:24:21 +00:00
|
|
|
return None
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_jobs() -> typing.List[Job]:
|
2022-07-30 15:24:21 +00:00
|
|
|
"""
|
|
|
|
Get the jobs list.
|
|
|
|
"""
|
2022-08-25 17:03:56 +00:00
|
|
|
with ReadUserData(UserDataFiles.JOBS) as user_data:
|
|
|
|
try:
|
|
|
|
if "jobs" not in user_data:
|
|
|
|
user_data["jobs"] = []
|
|
|
|
return [Job(**job) for job in user_data["jobs"]]
|
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
return []
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_busy() -> bool:
|
|
|
|
"""
|
|
|
|
Check if there is a job running.
|
|
|
|
"""
|
|
|
|
with ReadUserData(UserDataFiles.JOBS) as user_data:
|
|
|
|
if "jobs" not in user_data:
|
|
|
|
user_data["jobs"] = []
|
|
|
|
for job in user_data["jobs"]:
|
|
|
|
if job["status"] == JobStatus.RUNNING.value:
|
|
|
|
return True
|
|
|
|
return False
|