|
|
|
@@ -2,7 +2,7 @@
|
|
|
|
|
|
|
|
|
|
import Link from "next/link";
|
|
|
|
|
import React, { useCallback, useState } from "react";
|
|
|
|
|
import { DatabaseBackup, Download } from "lucide-react";
|
|
|
|
|
import { DatabaseBackup, Download, Play, RefreshCw, Timer } from "lucide-react";
|
|
|
|
|
import { Button, Checkbox, Field, Notice } from "@crosscode/ui";
|
|
|
|
|
import { AppShell, PageTitle, ShellLoading, type ShellUser } from "@/components/AppShell";
|
|
|
|
|
import { NoticeText } from "@/components/NoticeText";
|
|
|
|
@@ -61,6 +61,7 @@ export function Admin({ section = "people" }: { section?: AdminSection }) {
|
|
|
|
|
{section === "messages" && <Settings flash={flash} keys={["smtp_host", "smtp_port", "smtp_user", "smtp_pass", "smtp_from", "smtp_secure", "wawp_access_token", "wawp_instance_id"]} />}
|
|
|
|
|
{section === "notice" && <NoticeTab flash={flash} />}
|
|
|
|
|
{section === "backups" && <Backups flash={flash} />}
|
|
|
|
|
{section === "jobs" && <Jobs flash={flash} />}
|
|
|
|
|
{section === "licence" && <Licence flash={flash} />}
|
|
|
|
|
{section === "audit" && <Audit />}
|
|
|
|
|
</div>
|
|
|
|
@@ -463,3 +464,162 @@ function Backups({ flash }: { flash: Flash }) {
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Background jobs (1.4.0, as Cross Events) ────────────────────────────────
|
|
|
|
|
|
|
|
|
|
type JobRun = { state: string; createdOn?: string | null; 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[];
|
|
|
|
|
envEnabled: boolean; envCron: string; defaultCron: string; override: { enabled: boolean | null; cron: string | null };
|
|
|
|
|
};
|
|
|
|
|
type JobList = { mode: "queue" | "timers" | "off"; timezone: string; jobsEnabledEnv: boolean; jobs: JobInfo[] };
|
|
|
|
|
|
|
|
|
|
const RUN_STATE: Record<string, string> = {
|
|
|
|
|
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 SCHEDULES = [
|
|
|
|
|
{ label: "Every minute", cron: "* * * * *" }, { label: "Every 5 minutes", cron: "*/5 * * * *" }, { label: "Every 15 minutes", cron: "*/15 * * * *" },
|
|
|
|
|
{ label: "Every hour", cron: "0 * * * *" }, { label: "Daily at 02:00", cron: "0 2 * * *" }, { label: "Daily at 07:00", cron: "0 7 * * *" },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function describeCron(cron: string): string {
|
|
|
|
|
if (cron === "* * * * *") return "Every minute";
|
|
|
|
|
const m = /^\*\/(\d+) \* \* \* \*$/.exec(cron);
|
|
|
|
|
if (m) return `Every ${m[1]} minutes`;
|
|
|
|
|
const h = /^(\d+) \* \* \* \*$/.exec(cron);
|
|
|
|
|
if (h) return `Every hour at :${h[1]!.padStart(2, "0")}`;
|
|
|
|
|
const d = /^(\d+) (\d+) \* \* \*$/.exec(cron);
|
|
|
|
|
if (d) return `Daily at ${d[2]!.padStart(2, "0")}:${d[1]!.padStart(2, "0")}`;
|
|
|
|
|
return cron;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function runText(r: JobRun): string {
|
|
|
|
|
if (r.output === null || r.output === undefined) return "";
|
|
|
|
|
if (r.state === "failed" && typeof r.output === "object" && r.output && "message" in r.output) return String((r.output as { message: unknown }).message);
|
|
|
|
|
try { return JSON.stringify(r.output); } catch { return ""; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** An on/off switch (role="switch", so screen readers hear on and off). */
|
|
|
|
|
function Toggle({ on, label, disabled, onChange }: { on: boolean; label: string; disabled?: boolean; onChange: (on: boolean) => void }) {
|
|
|
|
|
return (
|
|
|
|
|
<button type="button" role="switch" aria-checked={on} aria-label={label} disabled={disabled} onClick={() => onChange(!on)}
|
|
|
|
|
className={`relative inline-flex h-6 w-11 shrink-0 items-center rounded-full transition-colors focus-visible:outline-2 focus-visible:outline-offset-2 disabled:cursor-not-allowed disabled:opacity-50 ${on ? "bg-brand" : "bg-gray-300"}`}>
|
|
|
|
|
<span className={`inline-block h-5 w-5 rounded-full bg-white shadow transition-transform ${on ? "translate-x-5" : "translate-x-0.5"}`} />
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Jobs({ flash }: { flash: Flash }) {
|
|
|
|
|
const [data, setData] = useState<JobList | null>(null);
|
|
|
|
|
const [busy, setBusy] = useState<string | null>(null);
|
|
|
|
|
const [open, setOpen] = useState<string | null>(null);
|
|
|
|
|
const [editing, setEditing] = useState<{ name: string; cron: string } | null>(null);
|
|
|
|
|
const [loadError, setLoadError] = useState<string | null>(null);
|
|
|
|
|
// Not tied to `flash` (a new function each render): the list loads once, then on Refresh.
|
|
|
|
|
const load = useCallback(async () => {
|
|
|
|
|
const r = await call<JobList>("GET", "/admin/jobs");
|
|
|
|
|
if (r.ok) { setData(r.data); setLoadError(null); } else setLoadError(r.error.message);
|
|
|
|
|
}, []);
|
|
|
|
|
useExternalEffect(() => { void load(); }, [load]);
|
|
|
|
|
|
|
|
|
|
const save = async (job: JobInfo, change: { enabled?: boolean | null; cron?: string | null }, done: string) => {
|
|
|
|
|
setBusy(job.name);
|
|
|
|
|
const r = await call("PUT", `/admin/jobs/${encodeURIComponent(job.name)}`, change);
|
|
|
|
|
setBusy(null);
|
|
|
|
|
if (r.ok) { flash("success", done); setEditing(null); void load(); } else flash("error", r.error.message);
|
|
|
|
|
};
|
|
|
|
|
const runNow = async (job: JobInfo) => {
|
|
|
|
|
setBusy(job.name);
|
|
|
|
|
const r = await call<{ queued: boolean }>("POST", `/admin/jobs/${encodeURIComponent(job.name)}/run`);
|
|
|
|
|
setBusy(null);
|
|
|
|
|
if (r.ok) {
|
|
|
|
|
flash("success", r.data.queued ? `"${job.label}" queued: it starts within a few seconds.` : `"${job.label}" ran.`);
|
|
|
|
|
setTimeout(() => void load(), r.data.queued ? 4000 : 0);
|
|
|
|
|
} else flash("error", r.error.message);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!data) return loadError ? <Notice tone="error">{loadError}</Notice> : <p className="text-gray-600">Loading…</p>;
|
|
|
|
|
const off = data.mode === "off";
|
|
|
|
|
return (
|
|
|
|
|
<Card>
|
|
|
|
|
<div className="mb-3 flex items-start justify-between gap-3">
|
|
|
|
|
<Heading icon={<Timer className="h-4 w-4" />} sub={`Times are ${data.timezone}. Each job runs once per schedule, however many server processes there are.`}>Jobs</Heading>
|
|
|
|
|
<Button variant="secondary" onClick={() => void load()}><RefreshCw className="mr-1.5 inline h-3.5 w-3.5" aria-hidden />Refresh</Button>
|
|
|
|
|
</div>
|
|
|
|
|
{off && <Notice tone="warning">All background jobs are switched off in the server's .env (JOBS_ENABLED=false), so nothing here runs. Cross Code can change that from the Hub.</Notice>}
|
|
|
|
|
{data.mode === "timers" && <Notice tone="warning">The job queue couldn't start, so jobs run on simple timers in this process (history only since the last restart).</Notice>}
|
|
|
|
|
<ul className="divide-y rounded-lg border">
|
|
|
|
|
{data.jobs.map((job) => {
|
|
|
|
|
const last = job.recent[0];
|
|
|
|
|
const mine = busy === job.name;
|
|
|
|
|
const isEditing = editing?.name === job.name;
|
|
|
|
|
return (
|
|
|
|
|
<li key={job.name} className="p-3">
|
|
|
|
|
<div className="flex flex-wrap items-center gap-x-3 gap-y-2">
|
|
|
|
|
<Toggle on={job.enabled} disabled={mine || off} label={`${job.label}: ${job.enabled ? "on" : "off"}`}
|
|
|
|
|
onChange={(on) => void save(job, { enabled: on }, `"${job.label}" is ${on ? "on" : "off"}.`)} />
|
|
|
|
|
<div className="mr-auto min-w-0">
|
|
|
|
|
<div className={`text-sm font-medium ${job.enabled ? "text-gray-900" : "text-gray-500"}`}>{job.label}</div>
|
|
|
|
|
<div className="text-xs text-gray-500">
|
|
|
|
|
{job.enabled ? `${describeCron(job.cron)} · next ${when(job.nextRun)}` : "Off"}
|
|
|
|
|
{job.override.enabled !== null
|
|
|
|
|
? <> · {job.enabled ? "switched on" : "switched off"} here · <button type="button" disabled={mine} className="underline hover:text-gray-700" onClick={() => void save(job, { enabled: null }, `"${job.label}" follows the .env again.`)}>use the .env ({job.envEnabled ? "on" : "off"})</button></>
|
|
|
|
|
: !job.envEnabled && job.envFlag ? ` (${job.envFlag}=false in the .env)` : ""}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{last && (
|
|
|
|
|
<button type="button" onClick={() => setOpen(open === job.name ? null : job.name)} title="Recent runs" className={`rounded px-2 py-0.5 text-xs ${RUN_STATE[last.state] ?? "bg-gray-100 text-gray-600"}`}>
|
|
|
|
|
{last.state} · {when(last.completedOn || last.startedOn || last.createdOn)}
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
<Button variant="secondary" disabled={mine || isEditing} onClick={() => setEditing({ name: job.name, cron: job.cron })}>Schedule</Button>
|
|
|
|
|
<Button variant="secondary" disabled={!!busy || off} onClick={() => void runNow(job)}><Play className="mr-1 inline h-3.5 w-3.5" aria-hidden />Run now</Button>
|
|
|
|
|
</div>
|
|
|
|
|
{isEditing && editing && (
|
|
|
|
|
<form className="mt-3 space-y-2 rounded-lg border bg-gray-50 p-3" onSubmit={(e) => { e.preventDefault(); void save(job, { cron: editing.cron.trim() }, `"${job.label}" now runs: ${describeCron(editing.cron.trim())}.`); }}>
|
|
|
|
|
<div className="flex flex-wrap gap-1.5">
|
|
|
|
|
{SCHEDULES.map((p) => (
|
|
|
|
|
<button key={p.cron} type="button" onClick={() => setEditing({ name: job.name, cron: p.cron })}
|
|
|
|
|
className={`rounded border px-2 py-1 text-xs ${editing.cron === p.cron ? "border-brand bg-white font-medium" : "bg-white hover:bg-gray-100"}`}>{p.label}</button>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-wrap items-end gap-3">
|
|
|
|
|
<Field label="Cron (minute hour day month weekday)" name="cron" value={editing.cron} maxLength={100} className="mb-0 w-64 font-mono"
|
|
|
|
|
onChange={(e) => setEditing({ name: job.name, cron: e.target.value })} hint={describeCron(editing.cron.trim())} />
|
|
|
|
|
<div className="ml-auto flex gap-2 pb-6">
|
|
|
|
|
<Button type="button" variant="secondary" onClick={() => setEditing(null)}>Cancel</Button>
|
|
|
|
|
<Button type="submit" busy={mine} disabled={!editing.cron.trim()}>Save schedule</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<p className="text-xs text-gray-500">
|
|
|
|
|
The server's own schedule is {describeCron(job.envCron)}{job.envCron !== job.defaultCron ? ` (from the .env; normally ${describeCron(job.defaultCron)})` : ""}.{" "}
|
|
|
|
|
{job.override.cron !== null && <button type="button" disabled={mine} className="underline hover:text-gray-700" onClick={() => void save(job, { cron: null }, `"${job.label}" is back on its own schedule.`)}>Use that again</button>}
|
|
|
|
|
</p>
|
|
|
|
|
</form>
|
|
|
|
|
)}
|
|
|
|
|
{open === job.name && job.recent.length > 0 && (
|
|
|
|
|
<div className="mt-2 overflow-x-auto">
|
|
|
|
|
<table className="w-full text-xs">
|
|
|
|
|
<thead className="text-gray-500"><tr><th className="py-1 text-left font-medium">Started</th><th className="text-left font-medium">Finished</th><th className="text-left font-medium">State</th><th className="text-left font-medium">Result</th></tr></thead>
|
|
|
|
|
<tbody>
|
|
|
|
|
{job.recent.map((r, i) => (
|
|
|
|
|
<tr key={i} className="border-t">
|
|
|
|
|
<td className="whitespace-nowrap py-1 pr-2">{when(r.startedOn)}</td>
|
|
|
|
|
<td className="whitespace-nowrap pr-2">{when(r.completedOn)}</td>
|
|
|
|
|
<td className="pr-2"><span className={`rounded px-1.5 py-0.5 ${RUN_STATE[r.state] ?? ""}`}>{r.state}</span></td>
|
|
|
|
|
<td className="break-all font-mono text-[11px] text-gray-600">{runText(r)}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
))}
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</li>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</ul>
|
|
|
|
|
<p className="mt-3 text-xs text-gray-500">Without a choice here, the server's .env applies: a job's own flag (e.g. BACKUP_ENABLED=false) and JOB_SCHEDULE_<NAME>. The nightly backup also follows Backups → "Back up every night".</p>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|