feat(scheduler): a channel's "get full history" backfill is a recorded run, and a row knows its last three runs

The one-channel deep backfill ran as a FastAPI BackgroundTask: what it fetched, what it spent and
how it failed reached only the log. It is now a one-shot run on the scheduler (`channel_backfill`,
trigger ad_hoc, actor = the user, target = the channel) with its quota tagged to the run. Its
per-channel exclusion with the scheduled backfill and the two-run cap are unchanged, and a run
they turn away says why in its result instead of returning nothing. A failure is no longer
swallowed: the run records it.

The scheduler snapshot carries each job's last three runs (one windowed query), so the page can
show an interrupted or skipped run instead of the latest outcome alone.
This commit is contained in:
2026-10-18 01:05:24 +02:00
parent 911527a548
commit 27c2fcbd2e
8 changed files with 256 additions and 55 deletions
+14 -4
View File
@@ -82,14 +82,24 @@ def interrupt_leftovers() -> int:
return res.rowcount if isinstance(res, CursorResult) else 0
def last_runs(db: Session) -> dict[str, JobRun]:
"""Each job's most recent run (any trigger), by job id."""
def recent_runs(db: Session, per_job: int) -> dict[str, list[JobRun]]:
"""Each job's last `per_job` runs, newest first, by job id — one query (a window per job)."""
ranked = select(
JobRun.id,
func.row_number()
.over(partition_by=JobRun.job, order_by=(JobRun.started_at.desc(), JobRun.id.desc()))
.label("rank"),
).subquery()
rows = db.execute(
select(JobRun)
.distinct(JobRun.job)
.join(ranked, ranked.c.id == JobRun.id)
.where(ranked.c.rank <= per_job)
.order_by(JobRun.job, JobRun.started_at.desc(), JobRun.id.desc())
).scalars()
return {r.job: r for r in rows}
out: dict[str, list[JobRun]] = {}
for r in rows:
out.setdefault(r.job, []).append(r)
return out
def gc(db: Session, retention_days: int) -> int: