implement job runner and scheduler
This commit is contained in:
parent
328a70ff9b
commit
2b2a3f1cc0
11 changed files with 1572 additions and 284 deletions
|
|
@ -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")],
|
||||
)
|
||||
),
|
||||
),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue