-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob.py
More file actions
163 lines (143 loc) · 4.98 KB
/
job.py
File metadata and controls
163 lines (143 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
from typing import Callable
from fastapi import HTTPException, status
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
from api import models, settings
from api.core.filesystem import FileSystem
from api.schemas import job as schemas
def enqueue_job(
job: models.Job,
filesystem: FileSystem,
enqueueing_func: Callable[[schemas.QueueJob], None],
) -> None:
app = job.application
job_config = settings.application_config.config[app["application"]][app["version"]][
app["entrypoint"]
]
# Handler parameters
handler_config = job_config["handler"]
def prepare_files(root_in: str, root_out: str, fs: FileSystem) -> dict[str, str]:
root_in_dir = root_in + ("/" if not root_in[-1] == "/" else "")
out_files = {}
if not fs.isdir(root_in_dir):
in_files = [root_in]
else:
in_files = [
f.path
for f in fs.list_directory(root_in_dir, dirs=False, recursive=True)
]
for in_f in in_files:
out_files[f"{root_out}/{os.path.relpath(in_f, root_in)}"] = (
fs.full_path_uri(in_f)
)
return out_files
config_path = f"config/{job.attributes['files_down']['config_id']}"
data_paths = [
f"data/{data_id}" for data_id in job.attributes["files_down"]["data_ids"]
]
artifact_paths = [
f"artifact/{artifact_id}"
for artifact_id in job.attributes["files_down"]["artifact_ids"]
]
_validate_files(filesystem, [config_path] + data_paths + artifact_paths)
roots_down = handler_config["files_down"]
files_down = prepare_files(config_path, roots_down["config_id"], filesystem)
for data_path in data_paths:
files_down.update(prepare_files(data_path, roots_down["data_ids"], filesystem))
for artifact_path in artifact_paths:
files_down.update(
prepare_files(artifact_path, roots_down["artifact_ids"], filesystem)
)
app_specs = schemas.AppSpecs(
cmd=job_config["app"]["cmd"],
env=job.attributes["env_vars"],
)
handler_specs = schemas.HandlerSpecs(
image_name=app["application"],
image_version=app["version"],
entrypoint=app["entrypoint"],
image_url=handler_config["image_url"],
files_down=files_down,
files_up=handler_config["files_up"],
)
meta_specs = schemas.MetaSpecs(job_id=job.id, date_created=job.date_created)
hardware_specs = schemas.HardwareSpecs(**job.hardware)
job_specs = schemas.JobSpecs(
app=app_specs, handler=handler_specs, meta=meta_specs, hardware=hardware_specs
)
paths_upload = {
"output": filesystem.full_path_uri(job.paths_out["output"]),
"log": filesystem.full_path_uri(job.paths_out["log"]),
"artifact": filesystem.full_path_uri(job.paths_out["artifact"]),
}
queue_item = schemas.QueueJob(
job=job_specs,
environment=(
job.environment if job.environment else models.EnvironmentTypes.any
),
group=None, # TODO
priority=job.priority,
paths_upload=schemas.PathsUploadSpecs(**paths_upload),
)
enqueueing_func(queue_item)
def get_jobs(
db: Session, user_id: int, offset: int = 0, limit: int = 100
) -> list[models.Job]:
return (
db.query(models.Job)
.filter(models.Job.user_id == user_id)
.offset(offset)
.limit(limit)
.all()
)
def get_job(db: Session, job_id: int) -> models.Job | None:
return db.query(models.Job).get(job_id)
def _validate_files(filesystem: FileSystem, paths: list[str]) -> None:
for _file in paths:
if not filesystem.exists(_file):
raise FileNotFoundError()
def create_job(
db: Session,
filesystem: FileSystem,
enqueueing_func: Callable[[schemas.QueueJob], None],
job: schemas.JobCreate,
user_id: int,
user_email: str | None = None,
) -> models.Job:
try:
paths_out = {
"output": f"output/{job.job_name}",
"log": f"log/{job.job_name}",
"artifact": f"artifact/{job.job_name}",
}
db_job = models.Job(
**job.model_dump(),
user_id=user_id,
user_email=user_email,
paths_out=paths_out,
)
db.add(db_job)
db.flush()
except IntegrityError:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Job name must be unique",
)
except ValueError as ve:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ve,
)
enqueue_job(db_job, filesystem, enqueueing_func)
db.commit()
db.refresh(db_job)
return db_job
def delete_job(db: Session, filesystem: FileSystem, db_job: models.Job) -> models.Job:
db.delete(db_job)
for path in db_job.paths_out.values():
if path[-1] != "/":
path += "/"
filesystem.delete(path)
db.commit()
return db_job