implement job runner and scheduler

This commit is contained in:
Abel Luck 2026-03-30 14:02:39 +02:00
parent 328a70ff9b
commit 2b2a3f1cc0
11 changed files with 1572 additions and 284 deletions

View file

@ -1,4 +1,4 @@
from repub.pages.dashboard import dashboard_page
from repub.pages.dashboard import dashboard_page, dashboard_page_with_data
from repub.pages.runs import execution_logs_page, runs_page
from repub.pages.shim import shim_page
from repub.pages.sources import create_source_page, edit_source_page, sources_page
@ -6,6 +6,7 @@ from repub.pages.sources import create_source_page, edit_source_page, sources_pa
__all__ = [
"create_source_page",
"dashboard_page",
"dashboard_page_with_data",
"edit_source_page",
"execution_logs_page",
"runs_page",

View file

@ -1,5 +1,7 @@
from __future__ import annotations
from collections.abc import Mapping
import htpy as h
from htpy import Node, Renderable
@ -12,36 +14,43 @@ from repub.components import (
stat_card,
status_badge,
)
from repub.pages.runs import RUNNING_EXECUTIONS
def _running_execution_row(execution: dict[str, str | bool]) -> tuple[Node, ...]:
status_tone = "running" if execution["is_running"] else "done"
def _text(values: Mapping[str, object], key: str) -> str:
return str(values[key])
def _running_execution_row(execution: Mapping[str, object]) -> tuple[Node, ...]:
status_tone = "running" if _text(execution, "status") != "Succeeded" else "done"
return (
h.div[
h.div(class_="font-semibold text-slate-950")[execution["source"]],
h.div(class_="font-semibold text-slate-950")[_text(execution, "source")],
h.p(class_="mt-0.5 font-mono text-[11px] text-slate-500")[
execution["slug"]
_text(execution, "slug")
],
],
h.div[
h.p(class_="font-medium text-slate-900")[f"#{execution['execution_id']}"],
h.p(class_="font-medium text-slate-900")[
f"#{_text(execution, 'execution_id')}"
],
h.p(class_="mt-0.5 text-[11px] text-slate-500")[
f"job {execution['job_id']}"
f"job {_text(execution, 'job_id')}"
],
],
h.div[
h.p(class_="font-medium text-slate-900")[execution["started_at"]],
h.p(class_="mt-0.5 text-[11px] text-slate-500")[execution["runtime"]],
h.p(class_="font-medium text-slate-900")[_text(execution, "started_at")],
h.p(class_="mt-0.5 text-[11px] text-slate-500")[
_text(execution, "runtime")
],
],
status_badge(label=str(execution["status"]), tone=status_tone),
status_badge(label=_text(execution, "status"), tone=status_tone),
h.div(class_="min-w-56 whitespace-normal")[
h.p(class_="font-medium text-slate-900")[execution["stats"]],
h.p(class_="mt-0.5 text-[11px] text-slate-500")[execution["worker"]],
h.p(class_="font-medium text-slate-900")[_text(execution, "stats")],
h.p(class_="mt-0.5 text-[11px] text-slate-500")[_text(execution, "worker")],
],
h.div(class_="flex flex-nowrap items-center gap-3")[
inline_link(
href=str(execution["log_href"]),
href=_text(execution, "log_href"),
label="View log",
tone="amber",
),
@ -71,7 +80,13 @@ def dashboard_header() -> Renderable:
]
def operational_snapshot() -> Renderable:
def operational_snapshot(*, snapshot: Mapping[str, str] | None = None) -> Renderable:
values = snapshot or {
"running_now": "0",
"upcoming_today": "0",
"failures_24h": "0",
"artifact_footprint": "0 B",
}
return h.section[
h.div(class_="mb-3 flex items-end justify-between gap-4")[
h.div[
@ -82,37 +97,39 @@ def operational_snapshot() -> Renderable:
"Operational snapshot"
],
],
h.p(class_="text-xs text-slate-500")[
"Static fixture data shaped around the intended operator dashboard"
],
h.p(class_="text-xs text-slate-500")["Live values from the database"],
],
h.dl(class_="grid gap-3 md:grid-cols-2 xl:grid-cols-4")[
stat_card(
label="Running now",
value="3",
detail="Two feed workers and one Pangea worker are active.",
value=values["running_now"],
detail="Currently active job executions.",
),
stat_card(
label="Upcoming today",
value="11",
detail="Next scheduled job fires in 13 minutes.",
value=values["upcoming_today"],
detail="Enabled jobs that are ready for their next run.",
),
stat_card(
label="Failures in 24h",
value="2",
detail="One network timeout and one source parsing error.",
value=values["failures_24h"],
detail="Recent failed executions recorded by the scheduler.",
),
stat_card(
label="Output footprint",
value="18.4 GB",
detail="Mirrored feeds, media, logs, and execution stats.",
label="Artifact footprint",
value=values["artifact_footprint"],
detail="Current log and stats artifact size under out/logs.",
),
],
]
def running_executions_table() -> Renderable:
rows = tuple(_running_execution_row(execution) for execution in RUNNING_EXECUTIONS)
def running_executions_table(
*, running_executions: tuple[Mapping[str, object], ...] | None = None
) -> Renderable:
rows = tuple(
_running_execution_row(execution) for execution in (running_executions or ())
)
headers = ("Source", "Execution", "Started", "Status", "Stats", "Actions")
def render_row(row: tuple[Node, ...]) -> Renderable:
@ -172,6 +189,14 @@ def running_executions_table() -> Renderable:
def dashboard_page() -> Renderable:
return dashboard_page_with_data()
def dashboard_page_with_data(
*,
snapshot: Mapping[str, str] | None = None,
running_executions: tuple[Mapping[str, object], ...] | None = None,
) -> Renderable:
return h.main(
id="morph",
class_="min-h-screen lg:grid lg:grid-cols-[18rem_minmax(0,1fr)]",
@ -180,8 +205,8 @@ def dashboard_page() -> Renderable:
h.div(class_="px-4 py-4 sm:px-5 lg:px-6 lg:py-5")[
h.div(class_="mx-auto max-w-7xl space-y-5")[
dashboard_header(),
operational_snapshot(),
running_executions_table(),
operational_snapshot(snapshot=snapshot),
running_executions_table(running_executions=running_executions),
]
],
]

View file

@ -1,10 +1,11 @@
from __future__ import annotations
from collections.abc import Mapping
import htpy as h
from htpy import Node, Renderable
from repub.components import (
inline_button,
inline_link,
muted_action_link,
page_shell,
@ -13,254 +14,174 @@ from repub.components import (
table_section,
)
RUNNING_EXECUTIONS: tuple[dict[str, str | bool], ...] = (
{
"source": "Pangea mobile articles",
"slug": "pangea-mobile",
"job_id": "7",
"execution_id": "104",
"started_at": "Today, 11:42 UTC",
"runtime": "running for 8m",
"status": "Running",
"stats": "26 requests • 7 items • 2.6 MB",
"worker": "graceful stop after current item",
"log_href": "/job/7/execution/104/logs",
"is_running": True,
},
{
"source": "Guardian feed mirror",
"slug": "guardian-feed",
"job_id": "3",
"execution_id": "103",
"started_at": "Today, 11:33 UTC",
"runtime": "running for 17m",
"status": "Running",
"stats": "91 requests • 13 items • 5.1 MB",
"worker": "streaming stats from worker jsonl",
"log_href": "/job/3/execution/103/logs",
"is_running": True,
},
{
"source": "Podcast enclosure mirror",
"slug": "podcast-audio",
"job_id": "11",
"execution_id": "105",
"started_at": "Today, 11:48 UTC",
"runtime": "running for 2m",
"status": "Stopping",
"stats": "4 requests • 0 items • 0.8 MB",
"worker": "waiting for 15s graceful shutdown window",
"log_href": "/job/11/execution/105/logs",
"is_running": True,
},
)
UPCOMING_JOBS: tuple[dict[str, str | bool], ...] = (
{
"source": "Podcast enclosure mirror",
"slug": "podcast-audio",
"job_id": "11",
"next_run": "Today, 12:15 local",
"schedule": "15 */4 * * 1-6",
"enabled_label": "Enabled",
"enabled_tone": "scheduled",
"run_disabled": True,
"run_reason": "Already running",
"toggle_label": "Disable",
},
{
"source": "Weekly digest feed",
"slug": "weekly-digest",
"job_id": "18",
"next_run": "Tomorrow, 08:00 local",
"schedule": "0 8 * * 1",
"enabled_label": "Disabled",
"enabled_tone": "idle",
"run_disabled": False,
"run_reason": "Ready",
"toggle_label": "Enable",
},
{
"source": "Kenya health desk",
"slug": "kenya-health",
"job_id": "22",
"next_run": "Today, 13:00 local",
"schedule": "0 */6 * * *",
"enabled_label": "Enabled",
"enabled_tone": "scheduled",
"run_disabled": False,
"run_reason": "Ready",
"toggle_label": "Disable",
},
)
COMPLETED_EXECUTIONS: tuple[dict[str, str], ...] = (
{
"source": "Guardian feed mirror",
"slug": "guardian-feed",
"job_id": "3",
"execution_id": "102",
"ended_at": "Today, 10:57 UTC",
"status": "Succeeded",
"status_tone": "done",
"stats": "204 requests • 28 items • 9.4 MB",
"summary": "Finished on schedule",
"log_href": "/job/3/execution/102/logs",
},
{
"source": "Podcast enclosure mirror",
"slug": "podcast-audio",
"job_id": "11",
"execution_id": "101",
"ended_at": "Today, 09:12 UTC",
"status": "Failed",
"status_tone": "failed",
"stats": "timeout after 3 retries",
"summary": "Worker exited with failure",
"log_href": "/job/11/execution/101/logs",
},
{
"source": "Pangea mobile articles",
"slug": "pangea-mobile",
"job_id": "7",
"execution_id": "100",
"ended_at": "Today, 05:48 UTC",
"status": "Canceled",
"status_tone": "idle",
"stats": "stopped by operator after 11m",
"summary": "Graceful stop completed",
"log_href": "/job/7/execution/100/logs",
},
)
def _action_button(
*,
label: str,
tone: str = "default",
disabled: bool = False,
post_path: str | None = None,
) -> Renderable:
classes = {
"default": "bg-stone-100 text-slate-700 hover:bg-stone-200",
"danger": "bg-rose-50 text-rose-700 hover:bg-rose-100",
}
class_name = (
"cursor-not-allowed bg-slate-100 text-slate-400" if disabled else classes[tone]
)
attributes: dict[str, str] = {}
if post_path is not None and not disabled:
attributes["data-on:pointerdown"] = f"@post('{post_path}')"
return h.button(
attributes,
type="button",
disabled=disabled,
class_=(
"inline-flex items-center whitespace-nowrap rounded-full px-3 py-1.5 "
f"text-sm font-semibold transition {class_name}"
),
)[label]
def _running_row(execution: dict[str, str | bool]) -> tuple[Node, ...]:
def _text(values: Mapping[str, object], key: str) -> str:
return str(values[key])
def _maybe_text(values: Mapping[str, object], key: str) -> str | None:
value = values.get(key)
if value in {None, ""}:
return None
return str(value)
def _flag(values: Mapping[str, object], key: str) -> bool:
return bool(values[key])
def _running_row(execution: Mapping[str, object]) -> tuple[Node, ...]:
return (
h.div[
h.div(class_="font-semibold text-slate-950")[execution["source"]],
h.p(class_="mt-1 font-mono text-xs text-slate-500")[execution["slug"]],
h.div(class_="font-semibold text-slate-950")[_text(execution, "source")],
h.p(class_="mt-1 font-mono text-xs text-slate-500")[
_text(execution, "slug")
],
],
h.div[
h.p(class_="font-medium text-slate-900")[f"#{execution['execution_id']}"],
h.p(class_="mt-1 text-xs text-slate-500")[f"job {execution['job_id']}"],
h.p(class_="font-medium text-slate-900")[
f"#{_text(execution, 'execution_id')}"
],
h.p(class_="mt-1 text-xs text-slate-500")[
f"job {_text(execution, 'job_id')}"
],
],
h.div[
h.p(class_="font-medium text-slate-900")[execution["started_at"]],
h.p(class_="mt-1 text-xs text-slate-500")[execution["runtime"]],
h.p(class_="font-medium text-slate-900")[_text(execution, "started_at")],
h.p(class_="mt-1 text-xs text-slate-500")[_text(execution, "runtime")],
],
status_badge(label=str(execution["status"]), tone="running"),
status_badge(label=_text(execution, "status"), tone="running"),
h.div(class_="min-w-56 whitespace-normal")[
h.p(class_="font-medium text-slate-900")[execution["stats"]],
h.p(class_="mt-1 text-xs text-slate-500")[execution["worker"]],
h.p(class_="font-medium text-slate-900")[_text(execution, "stats")],
h.p(class_="mt-1 text-xs text-slate-500")[_text(execution, "worker")],
],
h.div(class_="flex flex-nowrap items-center gap-3")[
inline_link(
href=str(execution["log_href"]),
href=_text(execution, "log_href"),
label="View log",
tone="amber",
),
inline_button(label="Stop", tone="danger"),
_action_button(
label="Stop",
tone="danger",
post_path=_maybe_text(execution, "cancel_post_path"),
),
],
)
def _upcoming_row(job: dict[str, str | bool]) -> tuple[Node, ...]:
def _upcoming_row(job: Mapping[str, object]) -> tuple[Node, ...]:
return (
h.div[
h.div(class_="font-semibold text-slate-950")[job["source"]],
h.p(class_="mt-1 font-mono text-xs text-slate-500")[job["slug"]],
h.div(class_="font-semibold text-slate-950")[_text(job, "source")],
h.p(class_="mt-1 font-mono text-xs text-slate-500")[_text(job, "slug")],
],
h.div[
h.p(class_="font-medium text-slate-900")[job["next_run"]],
h.p(class_="mt-1 text-xs text-slate-500")[f"job {job['job_id']}"],
h.p(class_="font-medium text-slate-900")[_text(job, "next_run")],
h.p(class_="mt-1 text-xs text-slate-500")[f"job {_text(job, 'job_id')}"],
],
h.p(class_="font-mono text-xs text-slate-600")[job["schedule"]],
h.p(class_="font-mono text-xs text-slate-600")[_text(job, "schedule")],
status_badge(
label=str(job["enabled_label"]),
tone=str(job["enabled_tone"]),
label=_text(job, "enabled_label"),
tone=_text(job, "enabled_tone"),
),
h.p(class_="max-w-40 whitespace-normal text-sm text-slate-500")[
job["run_reason"]
_text(job, "run_reason")
],
h.div(class_="flex flex-nowrap items-center gap-2")[
inline_button(label="Run now", disabled=bool(job["run_disabled"])),
inline_button(label=str(job["toggle_label"])),
inline_button(label="Delete", tone="danger"),
_action_button(
label="Run now",
disabled=_flag(job, "run_disabled"),
post_path=_maybe_text(job, "run_post_path"),
),
_action_button(
label=_text(job, "toggle_label"),
post_path=_maybe_text(job, "toggle_post_path"),
),
_action_button(
label="Delete",
tone="danger",
post_path=_maybe_text(job, "delete_post_path"),
),
],
)
def _completed_row(execution: dict[str, str]) -> tuple[Node, ...]:
def _completed_row(execution: Mapping[str, object]) -> tuple[Node, ...]:
return (
h.div[
h.div(class_="font-semibold text-slate-950")[execution["source"]],
h.p(class_="mt-1 font-mono text-xs text-slate-500")[execution["slug"]],
h.div(class_="font-semibold text-slate-950")[_text(execution, "source")],
h.p(class_="mt-1 font-mono text-xs text-slate-500")[
_text(execution, "slug")
],
],
h.div[
h.p(class_="font-medium text-slate-900")[f"#{execution['execution_id']}"],
h.p(class_="mt-1 text-xs text-slate-500")[f"job {execution['job_id']}"],
h.p(class_="font-medium text-slate-900")[
f"#{_text(execution, 'execution_id')}"
],
h.p(class_="mt-1 text-xs text-slate-500")[
f"job {_text(execution, 'job_id')}"
],
],
h.div[
h.p(class_="font-medium text-slate-900")[execution["ended_at"]],
h.p(class_="mt-1 text-xs text-slate-500")[execution["summary"]],
h.p(class_="font-medium text-slate-900")[_text(execution, "ended_at")],
h.p(class_="mt-1 text-xs text-slate-500")[_text(execution, "summary")],
],
status_badge(
label=execution["status"],
tone=execution["status_tone"],
label=_text(execution, "status"),
tone=_text(execution, "status_tone"),
),
h.div(class_="min-w-48 whitespace-normal")[
h.p(class_="font-medium text-slate-900")[execution["stats"]]
h.p(class_="font-medium text-slate-900")[_text(execution, "stats")]
],
inline_link(
href=execution["log_href"],
href=_text(execution, "log_href"),
label="View log",
tone="amber",
),
)
def delete_confirmation_preview() -> Renderable:
return section_card(
content=(
h.div(class_="flex items-center justify-between gap-4")[
h.div[
h.p(
class_="text-xs font-semibold uppercase tracking-[0.22em] text-amber-600"
)["Modal preview"],
h.h2(class_="mt-2 text-xl font-semibold text-slate-950")[
"Delete confirmation"
],
h.p(class_="mt-2 max-w-2xl text-sm text-slate-600")[
"Upcoming jobs use a confirmation modal before deleting a job. This is the intended open state, placed inline for the static UI pass."
],
],
status_badge(label="Preview", tone="scheduled"),
],
h.div(class_="mt-3 rounded-[1.5rem] bg-stone-50 p-5")[
h.p(
class_="text-sm font-semibold uppercase tracking-[0.18em] text-slate-500"
)["Delete job"],
h.h3(class_="mt-2 text-lg font-semibold text-slate-950")[
"Delete Weekly digest feed?"
],
h.p(class_="mt-3 max-w-2xl text-sm leading-6 text-slate-600")[
"This removes the source-linked job record and its schedule. Existing execution history and log files remain available for inspection."
],
h.div(class_="mt-6 flex flex-wrap gap-3")[
inline_button(label="Cancel"),
inline_button(label="Delete job", tone="danger"),
],
],
)
)
def runs_page() -> Renderable:
running_rows = tuple(_running_row(execution) for execution in RUNNING_EXECUTIONS)
upcoming_rows = tuple(_upcoming_row(job) for job in UPCOMING_JOBS)
completed_rows = tuple(
_completed_row(execution) for execution in COMPLETED_EXECUTIONS
)
def runs_page(
*,
running_executions: tuple[Mapping[str, object], ...] | None = None,
upcoming_jobs: tuple[Mapping[str, object], ...] | None = None,
completed_executions: tuple[Mapping[str, object], ...] | None = None,
) -> Renderable:
running_items = running_executions or ()
upcoming_items = upcoming_jobs or ()
completed_items = completed_executions or ()
running_rows = tuple(_running_row(execution) for execution in running_items)
upcoming_rows = tuple(_upcoming_row(job) for job in upcoming_items)
completed_rows = tuple(_completed_row(execution) for execution in completed_items)
return page_shell(
current_path="/runs",
@ -286,7 +207,7 @@ def runs_page() -> Renderable:
table_section(
eyebrow="Queue",
title="Upcoming jobs",
subtitle="Scheduled work shows enable or disable state, run-now affordances, and delete controls. Run now is disabled while the job is already running.",
subtitle="Scheduled work shows enable or disable state, run-now affordances, and destructive delete controls. Deleting removes the source-linked job and its execution history.",
headers=(
"Source",
"Next run",
@ -311,17 +232,43 @@ def runs_page() -> Renderable:
),
rows=completed_rows,
),
delete_confirmation_preview(),
),
)
def execution_logs_page(*, job_id: int, execution_id: int) -> Renderable:
def execution_logs_page(
*,
job_id: int,
execution_id: int,
log_view: Mapping[str, object] | None = None,
) -> Renderable:
if log_view is None:
log_view = {
"title": f"Job {job_id} / execution {execution_id}",
"description": "Plain text log view routed through the app.",
"status_label": "Unavailable",
"status_tone": "failed",
"log_text": "",
"error_message": "Execution log is only available from persisted job runs.",
}
error_message = _maybe_text(log_view, "error_message")
error_notice = (
h.div(
class_="mt-3 rounded-2xl bg-rose-50 px-4 py-3 text-sm font-medium text-rose-800"
)[
h.p["Execution log unavailable"],
h.p(class_="mt-1 font-normal")[error_message],
]
if error_message is not None
else None
)
return page_shell(
current_path=f"/job/{job_id}/execution/{execution_id}/logs",
eyebrow="Execution log",
title=f"Job {job_id} / execution {execution_id}",
description="Plain text log view routed through the app. The final version will stream appended lines while the worker is still active.",
title=_text(log_view, "title"),
description=_text(log_view, "description"),
actions=muted_action_link(href="/runs", label="Back to runs"),
content=(
section_card(
@ -335,25 +282,18 @@ def execution_logs_page(*, job_id: int, execution_id: int) -> Renderable:
f"/job/{job_id}/execution/{execution_id}/logs"
],
h.p(class_="mt-2 text-sm text-slate-600")[
"Streaming text log view. No arbitrary file paths are exposed in the UI."
_text(log_view, "description")
],
],
status_badge(label="Streaming", tone="running"),
status_badge(
label=_text(log_view, "status_label"),
tone=_text(log_view, "status_tone"),
),
],
error_notice,
h.pre(
class_="mt-3 overflow-x-auto rounded-[1.5rem] bg-slate-950 p-5 text-xs leading-6 text-emerald-200"
)[
"\n".join(
(
"11:42:01 scheduler: run_now requested for job 7",
"11:42:02 worker[7]: starting pangea-mobile",
"11:42:08 stats: requests=18 items=4 bytes=1.8MB",
"11:42:11 stats: requests=26 items=7 bytes=2.6MB",
"11:42:17 stats: requests=31 items=9 bytes=3.0MB",
"11:42:24 worker[7]: waiting for more log lines ...",
)
)
],
)[_text(log_view, "log_text")],
)
),
),