-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitiator.py
More file actions
254 lines (235 loc) · 8.3 KB
/
Copy pathinitiator.py
File metadata and controls
254 lines (235 loc) · 8.3 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python3
import shutil
import time
import zipfile
from pathlib import Path
from db_broker_client import broker_enabled, broker_query
from orchestration_common import (
BUNDLE_ROOT,
POLL_SECONDS,
claim_role_lock,
db_conn,
ensure_run_plan,
ensure_tables,
fetch_active_run,
fetch_next_run,
get_queue_mode,
get_worker_status,
pid_alive,
append_job_log,
log_error,
log,
mark_run_status,
set_plan_step,
set_queue_mode,
update_worker_status,
)
ROLE = "initiator"
INPUT_ROOT = BUNDLE_ROOT / "INBOX"
LOG_PATH = BUNDLE_ROOT / "_system" / "logs" / "initiator.service.log"
ORCHESTRATOR_ROLE = "orchestrator"
ORCHESTRATOR_STALE_SECONDS = 120
ORCHESTRATOR_QUEUE_STUCK_SECONDS = 60
ORCHESTRATOR_RUNNING_STUCK_SECONDS = 300
PLAN_STEPS = [
"unzip",
"rename",
"cap",
"archive",
"move_capped",
"merge_inputs",
"select",
"cropflip",
"manual_pause",
"manual_edit",
"manual_done",
"move_final",
"autotag",
"train_plan",
"train_stage",
"train_run",
"collect_training",
"package_dataset",
"package_lora",
"cleanup",
]
def unzip_run(run: dict) -> Path:
dest = INPUT_ROOT / run["runName"]
if dest.exists():
shutil.rmtree(dest, ignore_errors=True)
dest.mkdir(parents=True, exist_ok=True)
zip_path = Path(run["uploadPath"])
with zipfile.ZipFile(zip_path, "r") as zf:
zf.extractall(dest)
return dest
def _orchestrator_down(conn) -> bool:
status = get_worker_status(conn, ORCHESTRATOR_ROLE)
if not status:
return True
pid_ok = pid_alive(status.get("pid"))
heartbeat = status.get("heartbeat") or 0
if pid_ok:
return False
return int(time.time()) - int(heartbeat) > ORCHESTRATOR_STALE_SECONDS
def _mark_orchestrator_down_runs(conn) -> None:
if not _orchestrator_down(conn):
return
sql = (
"SELECT r.id, r.runId, r.runName, r.status, "
"CAST(strftime('%s','now') AS INTEGER) - "
"CAST(strftime('%s', COALESCE(tp.updatedAt, rp.updatedAt, r.startedAt, r.createdAt)) AS INTEGER) AS age "
"FROM Run r "
"LEFT JOIN (SELECT runId, MAX(updatedAt) AS updatedAt FROM TrainProgress GROUP BY runId) tp "
"ON tp.runId = r.runId "
"LEFT JOIN (SELECT runId, MAX(updatedAt) AS updatedAt FROM RunPlan GROUP BY runId) rp "
"ON rp.runId = r.runId "
"WHERE r.status IN ('queued_initiated', 'ready_to_train', 'running')"
)
if broker_enabled():
try:
resp = broker_query("sql_query", {"sql": sql, "params": []})
rows = resp.get("data") or []
except Exception:
return
else:
cur = conn.cursor()
cur.execute(sql)
rows = cur.fetchall()
for row in rows:
if broker_enabled():
run_id_db = row.get("id")
run_id = row.get("runId")
run_name = row.get("runName") or run_id
status = row.get("status") or ""
age = row.get("age") or 0
else:
run_id_db, run_id, run_name, status, age = row
if not run_id_db or not run_id:
continue
threshold = ORCHESTRATOR_QUEUE_STUCK_SECONDS
if str(status).lower() == "running":
threshold = ORCHESTRATOR_RUNNING_STUCK_SECONDS
if int(age) < threshold:
continue
msg = "orchestrator inactive; job stuck in queue"
error_type = "service_down"
if str(status).lower() == "running":
msg = "orchestrator inactive; run stalled"
error_type = "stalled_run"
log_path = BUNDLE_ROOT / "_system" / "logs" / f"orchestrator_{run_id}.log"
append_job_log(log_path, msg)
mark_run_status(conn, run_id_db, "failed_worker", "orchestrator_down", error=msg, finished=True)
log_error(
conn,
run_id_db=run_id_db,
component=ORCHESTRATOR_ROLE,
stage="orchestrator",
step="orchestrator_down",
error_type=error_type,
error_code="orchestrator_down",
error_message=msg,
log_path=log_path,
)
def main() -> None:
while True:
try:
conn = db_conn()
ensure_tables(conn)
except Exception as exc:
log(ROLE, f"DB unavailable: {exc}")
time.sleep(POLL_SECONDS)
continue
try:
if not claim_role_lock(conn, ROLE):
log(ROLE, "another initiator is active; exiting")
if conn:
conn.close()
return
break
except Exception:
if conn:
conn.close()
time.sleep(POLL_SECONDS)
while True:
try:
mode = get_queue_mode(conn)
if mode in {"paused", "stopped"}:
update_worker_status(conn, ROLE, mode, message=f"queue {mode}")
time.sleep(POLL_SECONDS)
continue
_mark_orchestrator_down_runs(conn)
# Allow only one active prep pipeline at a time.
# ready_to_train should not block prep; only running/queued_initiated/ready_for_finish.
active = fetch_active_run(conn)
if active and active.get("status") not in {"ready_to_train"}:
update_worker_status(
conn,
ROLE,
"blocked",
run_id=active["runId"],
message=f"active run {active['runId']} ({active['status']})",
)
time.sleep(POLL_SECONDS)
continue
run = fetch_next_run(conn, "queued")
if not run:
update_worker_status(conn, ROLE, "idle")
time.sleep(POLL_SECONDS)
continue
update_worker_status(conn, ROLE, "busy", run_id=run["runId"])
if not run.get("uploadPath") or not Path(run["uploadPath"]).exists():
msg = f"missing upload at {run.get('uploadPath')}"
log(ROLE, f"{run['runId']} {msg}")
log_path = BUNDLE_ROOT / "_system" / "logs" / f"initiator_{run['runId']}.log"
append_job_log(log_path, msg)
mark_run_status(conn, run["id"], "failed_initiator", "missing_upload", error=msg, finished=True)
log_error(
conn,
run_id_db=run["id"],
component=ROLE,
stage="unzip",
step="missing_upload",
error_type="invalid_input",
error_code="missing_upload",
error_message=msg,
log_path=log_path,
)
set_queue_mode(conn, "paused")
time.sleep(POLL_SECONDS)
continue
try:
unzip_run(run)
ensure_run_plan(conn, run["runId"], PLAN_STEPS)
set_plan_step(conn, run["runId"], "unzip", "done")
mark_run_status(conn, run["id"], "queued_initiated", "unpacked")
log(ROLE, f"prepared {run['runId']} -> queued_initiated")
except Exception as exc:
msg = f"unzip failed: {exc}"
log(ROLE, f"{run['runId']} {msg}")
log_path = BUNDLE_ROOT / "_system" / "logs" / f"initiator_{run['runId']}.log"
append_job_log(log_path, msg)
mark_run_status(conn, run["id"], "failed_initiator", "unzip_failed", error=msg, finished=True)
log_error(
conn,
run_id_db=run["id"],
component=ROLE,
stage="unzip",
step="unzip_failed",
error_type="invalid_input",
error_code="unzip_failed",
error_message=msg,
error_detail=str(exc),
log_path=log_path,
)
set_queue_mode(conn, "paused")
time.sleep(POLL_SECONDS)
except Exception as exc:
log(ROLE, f"error: {exc}")
finally:
try:
update_worker_status(conn, ROLE, "idle")
except Exception:
pass
time.sleep(POLL_SECONDS)
if __name__ == "__main__":
main()