fix: leave each job's timeout to the runner

This commit is contained in:
Abdessamad Derraz committed 2026-11-23 21:44:08 +01:00
1 parent c1ff070863
commit b5457d222b
4 files changed
+34 -2

No files matched your search

+8
View File
@@ -465,6 +465,7 @@ def run_job(ctx: Context, name: str) -> tuple[str, str]:
runner = Runner(ctx, job, state)
started, clock = now_iso(), ctx.host.clock()
lock = global_lock(ctx.lock_path) if job.lock else contextlib.nullcontext()
interrupted: SystemExit | None = None
try:
with lock:
runner.note(f"start {name}")
@@ -474,6 +475,11 @@ def run_job(ctx: Context, name: str) -> tuple[str, str]:
result, detail = exc.result, exc.detail
except OSError as exc:
result, detail = FAILED, f"{type(exc).__name__}: {exc}"
except SystemExit as exc:
# SIGTERM or SIGHUP (core.cli.run_cli): the run is recorded
# as failed, then the process ends with the signal's status.
interrupted = exc
result, detail = FAILED, f"interrupted, exit status {exc.code}"
except LockBusy as exc:
result, detail = DEFERRED, str(exc)
runner.note(f"end {name}: {result} {detail}")
@@ -500,6 +506,8 @@ def run_job(ctx: Context, name: str) -> tuple[str, str]:
set_alert(state, f"job:{name}", bool(alert), str(alert or ""), now_iso())
prune_logs(ctx.home / "logs")
save_state(ctx.state_path, state)
if interrupted is not None:
raise interrupted
return result, detail
+3 -1
View File
@@ -27,7 +27,9 @@ def render_units(
f"WorkingDirectory={repo}\n"
"Environment=RETROBIOS_AUTO=1\n"
f"ExecStart={python} {repo / 'scripts' / 'autorun.py'} run %i{flags}\n"
"TimeoutStartSec=6h\n"
# Each job's own `timeout` is its only limit: Runner stops the
# step and records the run, where a kill from systemd left none.
"TimeoutStartSec=infinity\n"
"Nice=10\n"
"IOSchedulingClass=idle\n"
"SuccessExitStatus=75\n"
+21
View File
@@ -211,6 +211,19 @@ class Guards(Scratch):
self.assertEqual(record["consecutive_failures"], 2)
self.assertIsNone(record["last_success"])
def test_a_run_ended_by_a_signal_is_recorded_then_exits(self):
"""systemd's SIGTERM reached run_job as SystemExit and passed the
record: the run left no result, no failure count and no alert."""
ctx = self.context({"x": {"worktree": False, "min_free_gb": 0, "steps": []}})
with unittest.mock.patch.object(runner.Runner, "execute", side_effect=SystemExit(143)), \
self.assertRaises(SystemExit) as caught:
runner.run_job(ctx, "x")
self.assertEqual(caught.exception.code, 143)
record = self.state(ctx)["jobs"]["x"]
self.assertEqual(record["result"], unattended_jobs.FAILED)
self.assertEqual(record["detail"], "interrupted, exit status 143")
self.assertEqual(record["consecutive_failures"], 1)
class StepEnvironment(Scratch):
"""A step can run third-party code: it never inherits a token or a password."""
@@ -677,6 +690,14 @@ class Units(Scratch):
pushed = unattended_timers.render_units(jobs, Path("/r"), "py", push=True)["retrobios@.service"]
self.assertIn("run %i --push\n", pushed)
def test_the_unit_leaves_the_time_limit_to_each_job(self):
"""A fixed TimeoutStartSec=6h cut a job given 12 h, and killed a 6 h
job before Runner, whose clock starts later, could record it."""
jobs = unattended_jobs.parse_jobs({"jobs": {
"long": {"schedule": "daily", "timeout": 43200}}})
service = unattended_timers.render_units(jobs, Path("/r"), "py")["retrobios@.service"]
self.assertIn("TimeoutStartSec=infinity\n", service)
def test_units_are_written_and_enabled_only_when_asked(self):
units = unattended_timers.render_units(unattended_jobs.load_jobs(), Path("/r"), "py")
calls = []
+2 -1
View File
@@ -879,7 +879,8 @@ python scripts/autorun.py install-timers --enable
`install-timers` writes `retrobios@.service` and one `retrobios-<job>.timer`
per scheduled job into `--unit-dir` (default `~/.config/systemd/user`).
`--enable` also runs `systemctl --user daemon-reload` and enables the timers.
`--with-push` writes units whose jobs run with `--push`.
`--with-push` writes units whose jobs run with `--push`. The service sets no time
limit of its own: a job's `timeout` ends it, and the run is recorded.
### cleanup_disk.py