"use client"; import React, { useCallback, useState } from "react"; import { Timer, Play, RefreshCw } from "lucide-react"; import { useAuth } from "@/hooks/useAuth"; import { useDismissingState } from "@/hooks/useDismissingState"; import { errorMessage } from "@/lib/utils"; import { apiV2Data } from "@/lib/apiV2"; import { useExternalEffect } from "@/lib/hooks"; type JobRun = { state: string; createdOn?: string; startedOn: string | null; completedOn: string | null; output: unknown }; type JobInfo = { name: string; label: string; cron: string; enabled: boolean; envFlag: string | null; nextRun: string | null; recent: JobRun[] }; type JobsResponse = { mode: "queue" | "timers" | "off"; timezone: string; jobs: JobInfo[] }; const STATE: Record = { completed: "bg-green-50 text-green-800", failed: "bg-red-50 text-red-700", active: "bg-blue-50 text-blue-700", created: "bg-gray-100 text-gray-600", retry: "bg-amber-50 text-amber-800", cancelled: "bg-gray-100 text-gray-500", }; const when = (d: string | null) => (d ? new Date(d).toLocaleString("en-GB", { day: "numeric", month: "short", hour: "2-digit", minute: "2-digit" }) : "—"); function describeCron(cron: string): string { if (cron === "* * * * *") return "Every minute"; const m = /^\*\/(\d+) \* \* \* \*$/.exec(cron); if (m) return `Every ${m[1]} minutes`; const d = /^(\d+) (\d+) \* \* \*$/.exec(cron); if (d) return `Daily at ${d[2].padStart(2, "0")}:${d[1].padStart(2, "0")}`; return cron; } function outputText(run: JobRun): string { if (run.output === null || run.output === undefined) return ""; if (typeof run.output === "object" && run.output && "message" in run.output && run.state === "failed") return String((run.output as { message: unknown }).message); try { return JSON.stringify(run.output); } catch { return ""; } } // Settings → Background jobs: what runs when, the last runs of each job, and "Run now". export function JobsTab({ active }: { active: boolean }) { const { token } = useAuth(); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [running, setRunning] = useState(null); const [open, setOpen] = useState(null); const [message, setMessage] = useDismissingState<{ type: "ok" | "err"; text: string } | null>(null); const load = useCallback(async () => { if (!token) return; setLoading(true); try { setData(await (apiV2Data("get", "/admin/jobs", { authToken: token }) as Promise)); } catch (e) { setMessage({ type: "err", text: errorMessage(e) || "Failed to load background jobs" }); } finally { setLoading(false); } }, [token, setMessage]); useExternalEffect(() => { if (active) load(); }, [active, load]); const runNow = async (job: JobInfo) => { if (!token) return; setRunning(job.name); try { const r = await (apiV2Data("post", "/admin/jobs/{name}/run", { params: { name: job.name }, authToken: token, body: {} }) as Promise<{ queued: boolean }>); setMessage({ type: "ok", text: r.queued ? `"${job.label}" queued — it starts within a few seconds.` : `"${job.label}" ran.` }); setTimeout(load, r.queued ? 4000 : 0); } catch (e) { setMessage({ type: "err", text: errorMessage(e) || "Couldn't run the job" }); } finally { setRunning(null); } }; return (

Background jobs

Tasks the server runs on a schedule ({data?.timezone || "local"} time). Each runs once per schedule, even with more than one server process, and failed runs are retried.

{message &&
{message.text}
} {data?.mode === "timers" && (
The job queue couldn't start, so jobs are running on simple timers in this server process (no retries, history only since the last restart). Check the server log for "[jobs]".
)} {data?.mode === "off" && (
All background jobs are switched off (JOBS_ENABLED=false).
)}
    {(data?.jobs || []).map(job => { const last = job.recent[0]; return (
  • {job.label}
    {job.enabled ? `${describeCron(job.cron)} · next ${when(job.nextRun)}` : `Off${job.envFlag ? ` (${job.envFlag}=false)` : ""}`}
    {last && ( )}
    {open === job.name && job.recent.length > 0 && ( {job.recent.map((r, i) => ( ))}
    StartedFinishedStateResult
    {when(r.startedOn)} {when(r.completedOn)} {r.state} {outputText(r)}
    )}
  • ); })} {!loading && data && data.jobs.length === 0 &&
  • No jobs.
  • }

Change a schedule with the env var JOB_SCHEDULE_<NAME> (cron, e.g. JOB_SCHEDULE_WAITLIST="*/5 * * * *"), or switch one off with its own flag.

); }