feat(scheduler): admin run-now/run-all triggers + live progress + completion notices

Add per-job "Run now" buttons and a "Start all now" button to the admin Scheduler
dashboard (admin-gated endpoints). Triggers run the job in a background thread
independent of its interval, refusing a concurrent run (409). While running, the
long jobs (maintenance, enrich, backfill, shorts) report live progress through a
decoupled contextvar sink, shown as a progress bar on the job row via the existing
4s poll. A manually-triggered run posts a completion notification to the triggering
admin's inbox (scheduled runs stay silent to avoid spam); the inbox renders the
"scheduler" type trilingually from type+data. While here, give the maintenance job
its missing dashboard label/description in all three languages.
This commit is contained in:
npeter83 2026-06-18 04:01:10 +02:00
parent 22e3c2fdd8
commit 5988769cda
15 changed files with 344 additions and 30 deletions

View file

@ -117,10 +117,21 @@ function NotificationRow({
// Known notification types are rendered from i18n (so they're trilingual) using the typed
// payload; the server-stored title/body are an English fallback for any unknown type.
const isMaintenance = n.type === "maintenance" && typeof n.data?.count === "number";
const title = isMaintenance ? t("inbox.maintenance.title") : n.title;
const body = isMaintenance
? t("inbox.maintenance.body", { count: n.data!.count })
: n.body;
const isScheduler = n.type === "scheduler" && typeof n.data?.job_id === "string";
let title = n.title;
let body = n.body;
if (isMaintenance) {
title = t("inbox.maintenance.title");
body = t("inbox.maintenance.body", { count: n.data!.count });
} else if (isScheduler) {
// "<Job label> finished/failed", with the raw result summary as the (technical) body.
const job = t(`scheduler.jobs.${n.data!.job_id}`, n.data!.job_id);
title = t(
n.data!.status === "error" ? "inbox.jobDone.titleError" : "inbox.jobDone.titleOk",
{ job }
);
body = n.data!.summary || n.body;
}
return (
<div
className={`glass rounded-xl p-3.5 flex items-start gap-3 transition ${

View file

@ -73,14 +73,45 @@ function StatusLegend() {
);
}
function JobProgress({ p }: { p: NonNullable<SchedulerJob["progress"]> }) {
const { t } = useTranslation();
const phase = p.phase ? t(`scheduler.phase.${p.phase}`, p.phase) : null;
const pct =
p.total && p.total > 0 ? Math.min(100, Math.round((p.current / p.total) * 100)) : null;
return (
<div className="mt-1.5">
<div className="flex items-center justify-between text-[11px] text-muted mb-0.5">
<span className="truncate">{phase}</span>
<span className="tabular-nums shrink-0">
{p.total != null
? `${p.current.toLocaleString()} / ${p.total.toLocaleString()}`
: p.current.toLocaleString()}
</span>
</div>
<div className="h-1.5 rounded-full bg-border overflow-hidden">
{pct != null ? (
<div className="h-full rounded-full bg-accent transition-[width]" style={{ width: `${pct}%` }} />
) : (
// Indeterminate: total unknown, so a slim moving sliver instead of a fill.
<div className="h-full w-1/3 rounded-full bg-accent animate-pulse" />
)}
</div>
</div>
);
}
function JobRow({
job,
onSave,
saving,
onRun,
runDisabled,
}: {
job: SchedulerJob;
onSave: (minutes: number) => void;
saving: boolean;
onRun: () => void;
runDisabled: boolean;
}) {
const { t } = useTranslation();
const [editing, setEditing] = useState(false);
@ -153,6 +184,7 @@ function JobRow({
<span> · {job.last_result}</span>
) : null}
</div>
{job.running && job.progress && <JobProgress p={job.progress} />}
</div>
<div className="shrink-0 text-right text-[11px] text-muted tabular-nums">
{job.running ? (
@ -166,6 +198,16 @@ function JobRow({
"—"
)}
</div>
<Tooltip hint={t("scheduler.runNow")}>
<button
onClick={onRun}
disabled={job.running || runDisabled}
aria-label={t("scheduler.runNow")}
className="shrink-0 p-1.5 rounded-lg text-muted hover:text-accent hover:bg-card transition disabled:opacity-30 disabled:hover:text-muted disabled:hover:bg-transparent"
>
<Play className="w-4 h-4" />
</button>
</Tooltip>
</div>
);
}
@ -227,6 +269,23 @@ export default function Scheduler() {
onError: () => notify({ level: "error", message: t("scheduler.intervalFailed") }),
});
// Manual "run now" triggers. The wrapper runs in a background thread server-side; the
// live poll surfaces the "running" state within a tick, so we just nudge a refetch.
const runMut = useMutation({
mutationFn: (jobId: string) => api.runSchedulerJob(jobId),
onSuccess: (_d, jobId) => {
notify({ level: "success", message: t("scheduler.triggered", { job: t(`scheduler.jobs.${jobId}`, jobId) }) });
qc.invalidateQueries({ queryKey: ["scheduler"] });
},
});
const runAllMut = useMutation({
mutationFn: () => api.runAllSchedulerJobs(),
onSuccess: (res) => {
notify({ level: "success", message: t("scheduler.triggeredAll", { count: res.started.length }) });
qc.invalidateQueries({ queryKey: ["scheduler"] });
},
});
if (q.isLoading && !data)
return <div className="p-8 text-muted">{t("scheduler.loading")}</div>;
if (!data)
@ -260,6 +319,16 @@ export default function Scheduler() {
</div>
</div>
<div className="flex-1" />
<Tooltip hint={data.paused ? t("scheduler.runAllPausedHint") : t("scheduler.runAllHint")}>
<button
onClick={() => runAllMut.mutate()}
disabled={runAllMut.isPending || data.paused}
className="glass-card glass-hover flex items-center gap-2 px-3 py-2 rounded-xl text-sm disabled:opacity-50 transition"
>
<Play className="w-4 h-4" />
{t("scheduler.runAll")}
</button>
</Tooltip>
<Tooltip hint={t("scheduler.pauseHint")}>
<button
onClick={() => pauseResume.mutate(data.paused)}
@ -290,6 +359,8 @@ export default function Scheduler() {
job={job}
saving={intervalMut.isPending}
onSave={(minutes) => intervalMut.mutate({ jobId: job.id, minutes })}
onRun={() => runMut.mutate(job.id)}
runDisabled={data.paused || runAllMut.isPending}
/>
))}
</div>