-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_core.py
More file actions
1408 lines (1309 loc) · 58.8 KB
/
Copy pathqueue_core.py
File metadata and controls
1408 lines (1309 loc) · 58.8 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Durable, bounded single-device job queue internals.
The HTTP and MCP surfaces intentionally live elsewhere. This module owns the
state machine, persistence, scheduling, process lifecycle, and log bounds.
"""
from __future__ import annotations
import contextlib
import json
import os
import select
import shutil
import signal
import sqlite3
import subprocess
import threading
import time
import uuid
from collections import deque
from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import Any, Iterator
DEFAULT_CLIENT_ID = "anon"
CURRENT_SCHEMA_VERSION = 2
MAX_CLIENT_ID_LEN = 128
DEFAULT_ITER_ESTIMATE_SEC = 10.0
STOP_GRACE_SEC = 8.0
REBOOT_REQUIRED_MSG = (
"DEVICE UNRECOVERABLE: reset and health verification failed. "
"A host reboot or operator recovery is required. All queued jobs were aborted."
)
def format_timestamp(value: float | None) -> str | None:
if value is None:
return None
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(value))
def current_boot_id() -> str:
try:
return Path("/proc/sys/kernel/random/boot_id").read_text().strip()
except OSError:
# Non-Linux/test fallback. Stable enough for one process, deliberately
# not persisted as proof of a reboot.
return "unknown"
@dataclass(frozen=True)
class QueueConfig:
log_dir: Path
default_timeout: int = 3600
max_timeout: int = 86400
max_repeat: int = 10_000
max_queued_jobs: int = 1_000
max_request_bytes: int = 1 << 20
max_log_read: int = 64 << 10
max_log_bytes: int = 16 << 20
max_cmd_chars: int = 64 << 10
max_cwd_chars: int = 4096
max_env_entries: int = 256
max_env_chars: int = 256 << 10
process_poll_interval: float = 0.2
stop_grace_sec: float = STOP_GRACE_SEC
reset_preempts_current: bool = True
reset_cmd: str = ""
deep_reset_cmd: str = ""
health_check_cmd: str = ""
reset_retries: int = 1
control_cmd_timeout: int = 60
retention_days: int = 30
max_completed_jobs: int = 10_000
def __post_init__(self) -> None:
if not 1 <= self.default_timeout <= self.max_timeout:
raise ValueError("default timeout must be between 1 and max timeout")
for name in (
"max_repeat", "max_queued_jobs", "max_request_bytes", "max_log_read",
"max_log_bytes", "max_cmd_chars", "max_cwd_chars", "max_env_entries", "max_env_chars",
"control_cmd_timeout",
):
if getattr(self, name) < 1:
raise ValueError(f"{name} must be positive")
if self.process_poll_interval <= 0 or self.stop_grace_sec < 0:
raise ValueError("poll interval must be positive and stop grace non-negative")
if self.reset_retries < 0 or self.retention_days < 0 or self.max_completed_jobs < 0:
raise ValueError("retry and retention values must be non-negative")
@property
def db_path(self) -> Path:
return self.log_dir / "jobs.sqlite3"
@classmethod
def from_env(cls, repo_root: Path) -> "QueueConfig":
tt_smi = os.path.expanduser("~/tenstorrent/.venv/bin/tt-smi")
def integer(name: str, default: int) -> int:
return int(os.environ.get(name, str(default)))
def floating(name: str, default: float) -> float:
return float(os.environ.get(name, str(default)))
def boolean(name: str, default: bool) -> bool:
raw = os.environ.get(name)
return default if raw is None else raw.lower() not in {"0", "false", "no"}
return cls(
log_dir=Path(os.environ.get("TT_DEVICE_LOG_DIR", repo_root / "logs")).resolve(),
default_timeout=integer("TT_DEVICE_DEFAULT_TIMEOUT", 3600),
max_timeout=integer("TT_DEVICE_MAX_TIMEOUT", 86400),
max_repeat=integer("TT_DEVICE_MAX_REPEAT", 10_000),
max_queued_jobs=integer("TT_DEVICE_MAX_QUEUED_JOBS", 1_000),
max_request_bytes=integer("TT_DEVICE_MAX_REQUEST_BYTES", 1 << 20),
max_log_read=integer("TT_DEVICE_MAX_LOG_READ", 64 << 10),
max_log_bytes=integer("TT_DEVICE_MAX_LOG_BYTES", 16 << 20),
max_cmd_chars=integer("TT_DEVICE_MAX_CMD_CHARS", 64 << 10),
process_poll_interval=floating("TT_DEVICE_PROCESS_POLL_INTERVAL", 0.2),
stop_grace_sec=floating("TT_DEVICE_STOP_GRACE_SEC", STOP_GRACE_SEC),
reset_preempts_current=boolean("TT_DEVICE_RESET_PREEMPTS_CURRENT", True),
reset_cmd=os.environ.get("TT_DEVICE_RESET_CMD", f"{tt_smi} -r"),
# Privileged deep reset is deliberately opt-in. Running arbitrary
# jobs and a sudo-capable reset broker under one Unix identity is
# not a security boundary.
deep_reset_cmd=os.environ.get("TT_DEVICE_DEEP_RESET_CMD", ""),
health_check_cmd=os.environ.get("TT_DEVICE_HEALTH_CHECK_CMD", f"{tt_smi} -s"),
reset_retries=integer("TT_DEVICE_RESET_RETRIES", 1),
control_cmd_timeout=integer("TT_DEVICE_HEALTH_CMD_TIMEOUT", 60),
retention_days=integer("TT_DEVICE_RETENTION_DAYS", 30),
max_completed_jobs=integer("TT_DEVICE_MAX_COMPLETED_JOBS", 10_000),
)
@dataclass
class Job:
id: str
cmd: str
cwd: str
timeout: int
repeat: int
env: dict[str, str] = field(default_factory=dict)
mode: str = "run"
client_id: str = DEFAULT_CLIENT_ID
reset_epoch: int = 0
submitted: float = field(default_factory=time.time)
status: str = "queued"
exit_code: int | None = None
elapsed: float | None = None
output_file: str = ""
started_at: float | None = None
finished_at: float | None = None
repeat_current: int = 0
repeat_completed: int = 0
current_iteration_started_at: float | None = None
first_iteration_elapsed: float | None = None
per_iter_estimate_sec: float = DEFAULT_ITER_ESTIMATE_SEC
stop_requested_at: float | None = None
stop_escalated_at: float | None = None
log_size: int = 0
log_truncated: bool = False
dropped_log_bytes: int = 0
timed_out: bool = False
error: str | None = None
@dataclass
class DeviceState:
state: str = "healthy"
reset_epoch: int = 0
reset_pending: bool = False
boot_id: str = ""
last_reset_at: float | None = None
dead_since: float | None = None
dead_reason: str | None = None
class QueueUnavailable(RuntimeError):
pass
class DeviceDeadError(QueueUnavailable):
pass
class JobStore:
"""SQLite metadata store. Logs intentionally remain canonical files."""
def __init__(self, db_path: Path):
self.db_path = db_path
self.db_path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
self._initialize()
try:
self.db_path.chmod(0o600)
except OSError:
pass
@contextlib.contextmanager
def connect(self) -> Iterator[sqlite3.Connection]:
conn = sqlite3.connect(self.db_path, timeout=5)
conn.row_factory = sqlite3.Row
try:
conn.execute("PRAGMA foreign_keys=ON")
conn.execute("PRAGMA busy_timeout=5000")
with conn:
yield conn
finally:
conn.close()
@staticmethod
def _create_device_state_table(conn: sqlite3.Connection) -> None:
conn.execute("""
CREATE TABLE IF NOT EXISTS device_state (
singleton INTEGER PRIMARY KEY CHECK(singleton = 1),
state TEXT NOT NULL,
reset_epoch INTEGER NOT NULL,
reset_pending INTEGER NOT NULL,
boot_id TEXT NOT NULL,
last_reset_at REAL,
dead_since REAL,
dead_reason TEXT,
updated_at REAL NOT NULL
)
""")
def _initialize(self) -> None:
with self.connect() as conn:
conn.execute("PRAGMA journal_mode=WAL")
conn.execute("PRAGMA synchronous=NORMAL")
version = conn.execute("PRAGMA user_version").fetchone()[0]
if version > CURRENT_SCHEMA_VERSION:
raise RuntimeError(f"database schema version {version} is newer than this server")
existing_jobs = conn.execute(
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='jobs'"
).fetchone()
if existing_jobs:
columns = {
row[1] for row in conn.execute("PRAGMA table_info(jobs)").fetchall()
}
required = {"seq", "id", "status", "log_truncated", "dropped_log_bytes"}
if not required.issubset(columns):
raise RuntimeError(
"incompatible jobs database; use a separate TT_DEVICE_LOG_DIR "
"instead of an original v1 queue database"
)
conn.execute("""
CREATE TABLE IF NOT EXISTS jobs (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
cmd TEXT NOT NULL,
cwd TEXT NOT NULL,
timeout INTEGER NOT NULL,
repeat INTEGER NOT NULL,
env_json TEXT NOT NULL,
mode TEXT NOT NULL,
client_id TEXT NOT NULL,
reset_epoch INTEGER NOT NULL,
submitted REAL NOT NULL,
status TEXT NOT NULL,
exit_code INTEGER,
elapsed REAL,
output_file TEXT NOT NULL,
started_at REAL,
finished_at REAL,
repeat_current INTEGER NOT NULL,
repeat_completed INTEGER NOT NULL,
first_iteration_elapsed REAL,
per_iter_estimate_sec REAL NOT NULL,
stop_requested_at REAL,
stop_escalated_at REAL,
log_size INTEGER NOT NULL,
log_truncated INTEGER NOT NULL,
dropped_log_bytes INTEGER NOT NULL,
timed_out INTEGER NOT NULL,
error TEXT,
updated_at REAL NOT NULL
)
""")
self._create_device_state_table(conn)
if version == 1:
columns = (
"singleton", "state", "reset_epoch", "reset_pending", "boot_id",
"last_reset_at", "dead_since", "dead_reason", "updated_at",
)
projection = ", ".join(columns)
conn.execute("ALTER TABLE device_state RENAME TO device_state_v1")
self._create_device_state_table(conn)
conn.execute(
f"INSERT INTO device_state ({projection}) "
f"SELECT {projection} FROM device_state_v1"
)
conn.execute("DROP TABLE device_state_v1")
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_jobs_status_seq ON jobs(status, seq)"
)
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_jobs_finished ON jobs(finished_at DESC)"
)
conn.execute(f"PRAGMA user_version={CURRENT_SCHEMA_VERSION}")
@staticmethod
def _values(job: Job) -> tuple[Any, ...]:
return (
job.id, job.cmd, job.cwd, job.timeout, job.repeat,
json.dumps(job.env, sort_keys=True), job.mode, job.client_id,
job.reset_epoch, job.submitted, job.status, job.exit_code,
job.elapsed, job.output_file, job.started_at, job.finished_at,
job.repeat_current, job.repeat_completed, job.first_iteration_elapsed,
job.per_iter_estimate_sec, job.stop_requested_at, job.stop_escalated_at,
job.log_size, int(job.log_truncated), job.dropped_log_bytes,
int(job.timed_out), job.error, time.time(),
)
def create_job(self, job: Job) -> None:
with self.connect() as conn:
conn.execute("""
INSERT INTO jobs (
id, cmd, cwd, timeout, repeat, env_json, mode, client_id,
reset_epoch, submitted, status, exit_code, elapsed, output_file,
started_at, finished_at, repeat_current, repeat_completed,
first_iteration_elapsed, per_iter_estimate_sec,
stop_requested_at, stop_escalated_at, log_size, log_truncated,
dropped_log_bytes, timed_out, error, updated_at
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
""", self._values(job))
def update_job(self, job: Job) -> None:
values = self._values(job)
with self.connect() as conn:
cursor = conn.execute("""
UPDATE jobs SET
cmd=?, cwd=?, timeout=?, repeat=?, env_json=?, mode=?, client_id=?,
reset_epoch=?, submitted=?, status=?, exit_code=?, elapsed=?,
output_file=?, started_at=?, finished_at=?, repeat_current=?,
repeat_completed=?, first_iteration_elapsed=?, per_iter_estimate_sec=?,
stop_requested_at=?, stop_escalated_at=?, log_size=?, log_truncated=?,
dropped_log_bytes=?, timed_out=?, error=?, updated_at=?
WHERE id=?
""", (*values[1:], values[0]))
if cursor.rowcount != 1:
raise KeyError(job.id)
def load_job(self, job_id: str) -> Job | None:
with self.connect() as conn:
row = conn.execute("SELECT * FROM jobs WHERE id=?", (job_id,)).fetchone()
return self._row_to_job(row) if row else None
def probe(self) -> None:
with self.connect() as conn:
conn.execute("SELECT 1").fetchone()
def load_queued(self) -> list[Job]:
with self.connect() as conn:
rows = conn.execute(
"SELECT * FROM jobs WHERE status='queued' ORDER BY seq"
).fetchall()
return [self._row_to_job(row) for row in rows]
def load_running(self) -> list[Job]:
with self.connect() as conn:
rows = conn.execute(
"SELECT * FROM jobs WHERE status='running' ORDER BY seq"
).fetchall()
return [self._row_to_job(row) for row in rows]
def recent_completed(self, limit: int = 10) -> list[dict[str, Any]]:
with self.connect() as conn:
rows = conn.execute("""
SELECT * FROM jobs WHERE status='done'
ORDER BY finished_at DESC, seq DESC LIMIT ?
""", (limit,)).fetchall()
result = []
for row in reversed(rows):
result.append({
"id": row["id"],
"cmd": row["cmd"][:120],
"exit_code": row["exit_code"],
"elapsed": row["elapsed"],
"finished": format_timestamp(row["finished_at"]),
"output_file": row["output_file"],
"repeat": row["repeat"],
"mode": row["mode"],
"client": row["client_id"],
"repeat_completed": row["repeat_completed"],
"per_iter_estimate_sec": round(row["per_iter_estimate_sec"], 2),
"timed_out": bool(row["timed_out"]),
"log_truncated": bool(row["log_truncated"]),
})
return result
def load_device_state(self) -> DeviceState | None:
with self.connect() as conn:
row = conn.execute(
"SELECT * FROM device_state WHERE singleton=1"
).fetchone()
if not row:
return None
return DeviceState(
state=row["state"], reset_epoch=row["reset_epoch"],
reset_pending=bool(row["reset_pending"]), boot_id=row["boot_id"],
last_reset_at=row["last_reset_at"], dead_since=row["dead_since"],
dead_reason=row["dead_reason"],
)
def save_device_state(self, state: DeviceState) -> None:
with self.connect() as conn:
conn.execute("""
INSERT INTO device_state (
singleton, state, reset_epoch, reset_pending, boot_id,
last_reset_at, dead_since, dead_reason, updated_at
) VALUES (1,?,?,?,?,?,?,?,?)
ON CONFLICT(singleton) DO UPDATE SET
state=excluded.state, reset_epoch=excluded.reset_epoch,
reset_pending=excluded.reset_pending, boot_id=excluded.boot_id,
last_reset_at=excluded.last_reset_at, dead_since=excluded.dead_since,
dead_reason=excluded.dead_reason, updated_at=excluded.updated_at
""", (
state.state, state.reset_epoch, int(state.reset_pending), state.boot_id,
state.last_reset_at, state.dead_since, state.dead_reason, time.time(),
))
def prune_completed(self, cutoff: float, keep: int) -> list[str]:
with self.connect() as conn:
rows = conn.execute("""
SELECT id, output_file FROM jobs WHERE status='done'
AND (finished_at < ? OR id NOT IN (
SELECT id FROM jobs WHERE status='done'
ORDER BY finished_at DESC, seq DESC LIMIT ?
))
""", (cutoff, max(0, keep))).fetchall()
if rows:
conn.executemany("DELETE FROM jobs WHERE id=?", [(r["id"],) for r in rows])
return [row["output_file"] for row in rows]
@staticmethod
def _row_to_job(row: sqlite3.Row) -> Job:
try:
env = json.loads(row["env_json"] or "{}")
except json.JSONDecodeError:
env = {}
return Job(
id=row["id"], cmd=row["cmd"], cwd=row["cwd"], timeout=row["timeout"],
repeat=row["repeat"], env=env, mode=row["mode"],
client_id=row["client_id"], reset_epoch=row["reset_epoch"],
submitted=row["submitted"], status=row["status"],
exit_code=row["exit_code"], elapsed=row["elapsed"],
output_file=row["output_file"], started_at=row["started_at"],
finished_at=row["finished_at"], repeat_current=row["repeat_current"],
repeat_completed=row["repeat_completed"],
first_iteration_elapsed=row["first_iteration_elapsed"],
per_iter_estimate_sec=row["per_iter_estimate_sec"],
stop_requested_at=row["stop_requested_at"],
stop_escalated_at=row["stop_escalated_at"], log_size=row["log_size"],
log_truncated=bool(row["log_truncated"]),
dropped_log_bytes=row["dropped_log_bytes"],
timed_out=bool(row["timed_out"]), error=row["error"],
)
class BoundedJobLog:
def __init__(self, job: Job, maximum: int, mode: str = "wb"):
self.job = job
self.maximum = max(1024, maximum)
self._file = open(job.output_file, mode, buffering=0)
if "a" in mode:
self.job.log_size = self._file.tell()
def __enter__(self) -> "BoundedJobLog":
return self
def __exit__(self, exc_type, exc, tb) -> None:
self.finish()
self._file.close()
def append(self, data: bytes) -> None:
if not data:
return
remaining = max(0, self.maximum - self.job.log_size)
if remaining:
written = data[:remaining]
self._file.write(written)
self.job.log_size += len(written)
dropped = len(data) - min(len(data), remaining)
if dropped:
self.job.log_truncated = True
self.job.dropped_log_bytes += dropped
def finish(self) -> None:
if not self.job.log_truncated:
return
marker = (
f"\n[tt-device-queue] Output truncated; dropped at least "
f"{self.job.dropped_log_bytes} bytes\n"
).encode()
# Preserve the bound by replacing the tail with the marker.
start = max(0, self.maximum - len(marker))
self._file.seek(start)
self._file.write(marker[-self.maximum:])
self._file.truncate(self.maximum)
self.job.log_size = self.maximum
class DeviceQueue:
def __init__(self, config: QueueConfig, store: JobStore):
self.config = config
self.store = store
self.config.log_dir.mkdir(parents=True, exist_ok=True)
self._jobs: dict[str, Job] = {} # active jobs only
self._pending: dict[str, deque[str]] = {}
self._rr_clients: deque[str] = deque()
self._current: Job | None = None
self._current_proc: subprocess.Popen[bytes] | None = None
self._lock = threading.RLock()
self._cond = threading.Condition(self._lock)
self._shutdown = threading.Event()
self._worker: threading.Thread | None = None
self._worker_heartbeat = time.monotonic()
self._degraded_reason: str | None = None
self._maintenance_warning: str | None = None
self._dirty_jobs: dict[str, Job] = {}
self._dirty_state = False
self._last_prune = 0.0
self._state = self._recover_state()
def _recover_state(self) -> DeviceState:
boot = current_boot_id()
state = self.store.load_device_state()
if state is None:
state = DeviceState(boot_id=boot)
self.store.save_device_state(state)
elif state.boot_id != boot and boot != "unknown":
state = DeviceState(
state="healthy", reset_epoch=state.reset_epoch + 1,
boot_id=boot, last_reset_at=state.last_reset_at,
)
self.store.save_device_state(state)
elif state.state == "resetting":
# Same-boot crash during reset: rerun recovery before dispatch.
state.reset_pending = True
now = time.time()
for job in self.store.load_running():
self._append_recovery_message(job)
job.status = "done"
job.exit_code = -1
job.finished_at = now
job.elapsed = round(now - job.started_at, 2) if job.started_at else None
job.error = "server restarted while job was running"
self.store.update_job(job)
queued = self.store.load_queued()
for job in queued:
if state.state == "dead":
self._append_terminal_message(job, state.dead_reason or REBOOT_REQUIRED_MSG)
job.status = "done"
job.exit_code = -1
job.finished_at = now
job.error = state.dead_reason or REBOOT_REQUIRED_MSG
self.store.update_job(job)
continue
self._jobs[job.id] = job
if job.client_id not in self._pending:
self._pending[job.client_id] = deque()
self._rr_clients.append(job.client_id)
self._pending[job.client_id].append(job.id)
return state
def _append_recovery_message(self, job: Job) -> None:
self._append_terminal_message(
job, "Server restarted while this job was running", leading_newline=True
)
def _append_terminal_message(
self, job: Job, message: str, *, leading_newline: bool = False
) -> None:
try:
path = Path(job.output_file)
path.parent.mkdir(parents=True, exist_ok=True)
with BoundedJobLog(job, self.config.max_log_bytes, "ab") as log:
prefix = "\n" if leading_newline else ""
log.append(f"{prefix}[tt-device-queue] {message}\n".encode())
except OSError:
pass
def start(self) -> None:
with self._lock:
if self._worker and self._worker.is_alive():
return
self._worker = threading.Thread(
target=self._worker_loop, name="device-queue-worker", daemon=False
)
self._worker.start()
def initiate_shutdown(self) -> None:
self._shutdown.set()
with self._cond:
proc = self._current_proc
job = self._current
if proc and job and job.mode == "run" and job.stop_requested_at is None:
job.stop_requested_at = time.time()
self._cond.notify_all()
if proc:
self._signal_group(proc, signal.SIGINT)
def join(self, timeout: float = 15.0) -> None:
if self._worker:
self._worker.join(timeout)
def _set_degraded(self, reason: str) -> None:
with self._cond:
self._degraded_reason = reason
self._cond.notify_all()
def _persist_job(self, job: Job) -> bool:
try:
self.store.update_job(job)
with self._lock:
self._dirty_jobs.pop(job.id, None)
return True
except Exception as exc:
with self._lock:
self._dirty_jobs[job.id] = job
self._set_degraded(f"persistence failure: {type(exc).__name__}: {exc}")
return False
def _persist_state(self) -> bool:
try:
self.store.save_device_state(self._state)
with self._lock:
self._dirty_state = False
return True
except Exception as exc:
with self._lock:
self._dirty_state = True
self._set_degraded(f"device-state persistence failure: {type(exc).__name__}: {exc}")
return False
def _try_recover_persistence(self) -> bool:
"""Flush failed metadata transitions before dispatching more work."""
with self._lock:
dirty_jobs = list(self._dirty_jobs.values())
dirty_state = self._dirty_state
try:
self.store.probe()
for job in dirty_jobs:
self.store.update_job(job)
if dirty_state:
self.store.save_device_state(self._state)
except Exception:
return False
with self._cond:
for job in dirty_jobs:
self._dirty_jobs.pop(job.id, None)
self._dirty_state = False
if self._degraded_reason and (
self._degraded_reason.startswith("persistence failure")
or self._degraded_reason.startswith("device-state persistence failure")
):
self._degraded_reason = None
self._cond.notify_all()
return True
def _validate_submission(
self, cmd: Any, cwd: Any, timeout: Any, repeat: Any,
env: Any, client_id: Any, mode: Any,
) -> tuple[str, str, int, int, dict[str, str], str]:
if not isinstance(cmd, str) or not cmd.strip():
raise ValueError("cmd must be a non-empty string")
cmd = cmd.strip()
if len(cmd) > self.config.max_cmd_chars:
raise ValueError(f"cmd must be at most {self.config.max_cmd_chars} characters")
if not isinstance(cwd, str):
raise ValueError("cwd must be a string")
if len(cwd) > self.config.max_cwd_chars:
raise ValueError(f"cwd must be at most {self.config.max_cwd_chars} characters")
if cwd and (not os.path.isdir(cwd) or not os.access(cwd, os.X_OK)):
raise ValueError(f"cwd is not an accessible directory: {cwd}")
if isinstance(repeat, bool) or not isinstance(repeat, int):
raise ValueError("repeat must be an integer")
if not 1 <= repeat <= self.config.max_repeat:
raise ValueError(f"repeat must be between 1 and {self.config.max_repeat}")
if timeout is None or timeout == 0:
timeout = self.config.default_timeout
if isinstance(timeout, bool) or not isinstance(timeout, int):
raise ValueError("timeout must be an integer")
if not 1 <= timeout <= self.config.max_timeout:
raise ValueError(f"timeout must be between 1 and {self.config.max_timeout}")
if mode != "run":
raise ValueError("mode must be 'run'")
if env is None:
env = {}
if not isinstance(env, dict):
raise ValueError("env must be an object mapping names to values")
if len(env) > self.config.max_env_entries:
raise ValueError(f"env may contain at most {self.config.max_env_entries} entries")
env_chars = 0
checked_env: dict[str, str] = {}
for key, value in env.items():
if not isinstance(key, str) or not key or "=" in key or "\0" in key:
raise ValueError("env names must be non-empty strings without '=' or NUL")
if not isinstance(value, str) or "\0" in value:
raise ValueError("env values must be strings without NUL")
env_chars += len(key) + len(value)
checked_env[key] = value
if env_chars > self.config.max_env_chars:
raise ValueError(f"env is larger than {self.config.max_env_chars} characters")
if not isinstance(client_id, str) or not client_id.strip():
raise ValueError("client_id must be a non-empty string")
client_id = client_id.strip()
if len(client_id) > MAX_CLIENT_ID_LEN:
raise ValueError(f"client_id must be at most {MAX_CLIENT_ID_LEN} characters")
return cmd, cwd, timeout, repeat, checked_env, client_id
def submit(
self, cmd: Any, cwd: Any = "", timeout: Any = None, repeat: Any = 1,
mode: Any = "run", env: Any = None, client_id: Any = DEFAULT_CLIENT_ID,
) -> tuple[Job, int, int]:
cmd, cwd, timeout, repeat, env, client_id = self._validate_submission(
cmd, cwd, timeout, repeat, env, client_id, mode
)
with self._lock:
self._ensure_available_locked()
if sum(map(len, self._pending.values())) >= self.config.max_queued_jobs:
raise QueueUnavailable("queue is full")
epoch = self._state.reset_epoch
job_id = uuid.uuid4().hex
output_dir = self.config.log_dir / job_id
output_dir.mkdir(mode=0o700, parents=True, exist_ok=False)
job = Job(
id=job_id, cmd=cmd, cwd=cwd, timeout=timeout, repeat=repeat,
env=env, client_id=client_id, reset_epoch=epoch,
output_file=str(output_dir / "output"),
)
try:
self.store.create_job(job)
except Exception as exc:
shutil.rmtree(output_dir, ignore_errors=True)
raise QueueUnavailable(
f"could not persist job submission: {type(exc).__name__}: {exc}"
) from exc
with self._cond:
try:
self._ensure_available_locked()
if sum(map(len, self._pending.values())) >= self.config.max_queued_jobs:
raise QueueUnavailable("queue is full")
except QueueUnavailable as exc:
job.status = "done"
job.exit_code = -1
job.finished_at = time.time()
job.error = str(exc)
self._persist_job(job)
raise
self._jobs[job.id] = job
if client_id not in self._pending:
self._pending[client_id] = deque()
self._rr_clients.append(client_id)
self._pending[client_id].append(job.id)
order = self._dispatch_order_locked()
position = order.index(job.id)
wait = self._wait_for_prefix_locked(order, position)
jobs_ahead = position + (1 if self._current else 0)
self._cond.notify_all()
return job, jobs_ahead, wait
def _ensure_available_locked(self) -> None:
if self._degraded_reason:
raise QueueUnavailable(self._degraded_reason)
if self._shutdown.is_set():
raise QueueUnavailable("queue server is shutting down")
if self._state.state == "dead":
raise DeviceDeadError(self._state.dead_reason or REBOOT_REQUIRED_MSG)
def _dispatch_order_locked(self) -> list[str]:
queues = {client: deque(ids) for client, ids in self._pending.items() if ids}
clients = deque(client for client in self._rr_clients if client in queues)
order: list[str] = []
while clients:
client = clients.popleft()
order.append(queues[client].popleft())
if queues[client]:
clients.append(client)
return order
def _take_next_locked(self) -> Job | None:
while self._rr_clients:
client = self._rr_clients.popleft()
queue = self._pending.get(client)
if not queue:
self._pending.pop(client, None)
continue
job_id = queue.popleft()
if queue:
self._rr_clients.append(client)
else:
self._pending.pop(client, None)
return self._jobs[job_id]
return None
def _remaining_locked(self, job: Job, now: float | None = None) -> int:
now = time.time() if now is None else now
if job.status == "done":
return 0
estimate = max(0.1, job.per_iter_estimate_sec)
if job.status == "queued":
return round(job.repeat * estimate)
started = job.current_iteration_started_at or job.started_at or now
current = max(0.0, estimate - max(0.0, now - started))
after = max(0, job.repeat - job.repeat_current) * estimate
return round(current + after)
def _wait_for_prefix_locked(self, order: list[str], count: int) -> int:
now = time.time()
total = self._remaining_locked(self._current, now) if self._current else 0
for job_id in order[:count]:
total += self._remaining_locked(self._jobs[job_id], now)
return total
def get_job(self, job_id: str) -> Job | None:
with self._lock:
active = self._jobs.get(job_id)
return active if active is not None else self.store.load_job(job_id)
def snapshot(self, job: Job) -> dict[str, Any]:
with self._lock:
data = self._snapshot_base(job)
if job.status == "queued":
order = self._dispatch_order_locked()
try:
position = order.index(job.id)
except ValueError:
position = -1
data.update({
"position": position + 1,
"estimated_wait_sec": self._wait_for_prefix_locked(order, max(position, 0)),
"estimated_remaining_sec": self._remaining_locked(job),
})
elif job.status == "running":
data.update({
"position": 0, "estimated_wait_sec": 0,
"estimated_remaining_sec": self._remaining_locked(job),
"running_sec": round(time.time() - (job.started_at or job.submitted), 1),
})
else:
data["estimated_remaining_sec"] = 0
return data
@staticmethod
def _snapshot_base(job: Job) -> dict[str, Any]:
data = {
"job_id": job.id, "status": job.status, "client_id": job.client_id,
"cmd": job.cmd, "cwd": job.cwd, "timeout": job.timeout,
"repeat": job.repeat, "mode": job.mode,
"repeat_current": job.repeat_current,
"repeat_completed": job.repeat_completed,
"first_iteration_elapsed": job.first_iteration_elapsed,
"per_iter_estimate_sec": round(job.per_iter_estimate_sec, 2),
"submitted_at": format_timestamp(job.submitted),
"started_at": format_timestamp(job.started_at),
"finished_at": format_timestamp(job.finished_at),
"output_file": job.output_file, "exit_code": job.exit_code,
"elapsed": job.elapsed, "timed_out": job.timed_out,
"log_size": job.log_size, "log_truncated": job.log_truncated,
"dropped_log_bytes": job.dropped_log_bytes, "error": job.error,
}
if job.timed_out:
data["timeout_message"] = f"Command timed out after {job.timeout}s; the queue sent SIGKILL."
if job.stop_requested_at is not None:
data["stop_requested_at"] = format_timestamp(job.stop_requested_at)
return data
def status(self) -> dict[str, Any]:
now = time.time()
with self._lock:
current = None
if self._current:
job = self._current
current = {
"id": job.id, "cmd": job.cmd[:120], "client": job.client_id,
"running_sec": round(now - (job.started_at or job.submitted), 1),
"estimated_remaining_sec": self._remaining_locked(job, now),
"repeat": job.repeat, "mode": job.mode,
"repeat_current": job.repeat_current,
"repeat_completed": job.repeat_completed,
}
order = self._dispatch_order_locked()
cumulative = self._remaining_locked(self._current, now) if self._current else 0
pending = []
for job_id in order:
job = self._jobs[job_id]
run_estimate = self._remaining_locked(job, now)
pending.append({
"id": job.id, "cmd": job.cmd[:120], "client": job.client_id,
"waiting_sec": round(now - job.submitted, 1),
"estimated_wait_sec": cumulative,
"estimated_run_sec": run_estimate,
"repeat": job.repeat, "mode": job.mode,
})
cumulative += run_estimate
state = asdict(self._state)
state.update({
"last_reset_at": format_timestamp(self._state.last_reset_at),
"dead_since": format_timestamp(self._state.dead_since),
"queue_disabled": bool(self._degraded_reason),
"disabled_reason": self._degraded_reason,
})
worker = self._worker
worker_info = {
"alive": bool(worker and worker.is_alive()),
"heartbeat_age_sec": round(time.monotonic() - self._worker_heartbeat, 2),
"degraded_reason": self._degraded_reason,
"maintenance_warning": self._maintenance_warning,
}
try:
recent = self.store.recent_completed(10)
except Exception as exc:
recent = []
self._set_degraded(f"persistence failure: {type(exc).__name__}: {exc}")
return {
"current": current, "pending": pending,
"recent": recent,
"device": state, "worker": worker_info,
}
def read_logs(self, job: Job, offset: int, limit: int) -> dict[str, Any]:
offset = max(0, int(offset))
limit = max(1, min(int(limit), self.config.max_log_read))
path = Path(job.output_file)
try:
size = path.stat().st_size
with open(path, "rb") as stream:
stream.seek(min(offset, size))
data = stream.read(limit + 1)
except FileNotFoundError:
size, data = 0, b""
truncated = len(data) > limit or offset + len(data) < size
data = data[:limit]
next_offset = offset + len(data)
return {
"job_id": job.id, "status": job.status, "output_file": job.output_file,
"offset": offset, "next_offset": next_offset,
"content": data.decode("utf-8", errors="replace"),
"truncated": truncated, "complete": job.status == "done" and next_offset >= size,
"log_size": size, "log_truncated": job.log_truncated,
"dropped_log_bytes": job.dropped_log_bytes,
}
def stop_job(self, job_id: str | None = None) -> dict[str, Any] | None:
with self._lock:
job, proc = self._current, self._current_proc
if not job or not proc or job.mode != "run":
return None
if job_id and job.id != job_id:
raise ValueError(f"Job {job_id} is not currently running")
if job.stop_requested_at is None:
job.stop_requested_at = time.time()
self._persist_job(job)
info = {"id": job.id, "cmd": job.cmd[:120], "signal": "SIGINT"}
self._signal_group(proc, signal.SIGINT)
return info
def request_reset(self, job_id: str | None) -> dict[str, Any]:
reported = self.get_job(job_id) if job_id else None
if job_id and reported is None:
raise KeyError(job_id)
proc_to_stop = None
with self._cond:
self._ensure_available_locked()
epoch = self._state.reset_epoch
if reported and reported.reset_epoch < epoch:
return {
"action": "already_reset", "device_state": self._state.state,
"reset_epoch": epoch,
"hint": "Device was already reset after this job ran. Resubmit the job.",
}
if self._state.state == "resetting" or self._state.reset_pending:
return {
"action": "joined", "device_state": self._state.state,
"reset_epoch": epoch,
"hint": "A reset is already pending or in progress.",
}
self._state.reset_pending = True
if not self._persist_state():
raise QueueUnavailable(self._degraded_reason or "failed to persist reset request")
if (
self.config.reset_preempts_current and self._current
and self._current.mode == "run"
and (reported is None or reported.id == self._current.id)
):
self._current.stop_requested_at = time.time()
self._persist_job(self._current)
proc_to_stop = self._current_proc
self._cond.notify_all()
if proc_to_stop:
self._signal_group(proc_to_stop, signal.SIGINT)
return {
"action": "scheduled", "device_state": self._state.state,
"reset_epoch": epoch,