Files
events-hub/frontend/src/app/(hub)/servers/page.tsx
T
joshuaandClaude Opus 5.5 5a2aec7a67 Hub 2.3.1: a server running a job is busy, not down
An agent before 1.1.1 neither polls nor sends stats while it runs a job,
so after 5 minutes the Hub alerted (and emailed) that it was down. While
a job it handed out is within its time limit, the server counts as busy:
no agent_silent alert, online for New install, "Running a job" in the UI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 19:02:49 +02:00

105 lines
6.2 KiB
TypeScript

"use client";
import React, { useCallback, useEffect, useState } from "react";
import Link from "next/link";
import { Copy, Plus } from "lucide-react";
import { api, errorText } from "@/lib/api";
import { ago, bytes } from "@/lib/format";
import { Loading, Message, PageTitle } from "@/components/ui";
import { ServersNav, StatusPill, pct, type ServerSummary } from "@/components/servers";
type Msg = { type: "ok" | "err"; text: string } | null;
/** Hub → Servers: every server with its agent, resources and sites; Add server (§3, §10). */
export default function ServersPage() {
const [list, setList] = useState<ServerSummary[] | null>(null);
const [msg, setMsg] = useState<Msg>(null);
const [adding, setAdding] = useState(false);
const [name, setName] = useState("");
const [mode, setMode] = useState<"church" | "central">("church");
const [joined, setJoined] = useState<{ command: string; expiresAt: string } | null>(null);
const load = useCallback(() => api<{ servers: ServerSummary[] }>("/api/admin/servers").then(r => setList(r.servers)).catch(e => setMsg({ type: "err", text: errorText(e) })), []);
useEffect(() => { load(); const t = setInterval(load, 30000); return () => clearInterval(t); }, [load]);
const add = async (e: React.FormEvent) => {
e.preventDefault();
setMsg(null);
try {
const r = await api<{ command: string; expiresAt: string }>("/api/admin/servers/join-token", { method: "POST", body: { name, mode } });
setJoined(r);
} catch (err) { setMsg({ type: "err", text: errorText(err) }); }
};
return (
<div>
<PageTitle title="Servers" subtitle="Each server runs the Cross Code agent, which polls the Hub for jobs and reports its health every minute." actions={<button className="btn-primary" onClick={() => { setAdding(true); setJoined(null); setName(""); }}><Plus className="h-4 w-4" /> Add server</button>} />
<ServersNav active="/servers" />
<Message msg={msg} />
{adding && (
<div className="card mb-6">
{!joined ? (
<form onSubmit={add} className="flex flex-wrap items-end gap-3">
<div className="min-w-[14rem] flex-1"><label className="label" htmlFor="srv-name">Name</label><input id="srv-name" className="input" value={name} onChange={e => setName(e.target.value)} placeholder="church-server-2" required /></div>
<div><label className="label" htmlFor="srv-mode">Kind</label>
<select id="srv-mode" className="input" value={mode} onChange={e => setMode(e.target.value as "church" | "central")}>
<option value="church">Church server (runs sites)</option>
<option value="central">Central server (FastPanel, read-only checks)</option>
</select>
</div>
<button className="btn-primary">Make the join command</button>
<button type="button" className="btn-secondary" onClick={() => setAdding(false)}>Cancel</button>
</form>
) : (
<div className="space-y-2">
<p className="text-sm">Run this on the new server as root. The token works once, until {new Date(joined.expiresAt).toLocaleTimeString()}:</p>
<pre className="overflow-x-auto rounded-lg bg-slate-900 p-3 text-xs text-slate-100">{joined.command}</pre>
<div className="flex gap-2">
<button className="btn-secondary" onClick={() => navigator.clipboard?.writeText(joined.command)}><Copy className="h-4 w-4" /> Copy</button>
<button className="btn-secondary" onClick={() => { setAdding(false); load(); }}>Done</button>
</div>
<p className="text-xs text-slate-500">The server shows here within a minute of joining. Then set its configuration (database, TRUST_PROXY) on its page.</p>
</div>
)}
</div>
)}
{!list ? <Loading /> : list.length === 0 ? <p className="text-sm text-slate-500">No servers yet. Add one to install churches from the Hub.</p> : (
<div className="grid gap-4 md:grid-cols-2">
{list.map(s => {
const disk = s.disk.reduce((m, d) => Math.max(m, d.totalBytes ? (d.usedBytes / d.totalBytes) * 100 : 0), 0);
return (
<Link key={s.id} href={`/servers/${s.id}`} className="card block hover:border-indigo-300">
<div className="flex items-start justify-between gap-2">
<div>
<div className="font-medium">{s.name}</div>
<div className="text-xs text-slate-500">{s.mode === "central" ? "Central server" : "Church server"} · {s.hostname || "?"} · agent {s.agentVersion || "?"}</div>
</div>
<StatusPill status={s.busy ? "busy" : s.online ? "online" : "offline"} />
</div>
<dl className="mt-4 grid grid-cols-3 gap-2 text-sm">
<div><dt className="text-xs text-slate-500">RAM used</dt><dd className="tabular-nums">{pct(s.ram?.usedPercent)} <span className="text-xs text-slate-500">of {bytes(s.ram?.totalBytes)}</span></dd></div>
<div><dt className="text-xs text-slate-500">CPU now · day avg</dt><dd className="tabular-nums">{pct(s.cpu?.percent)} · {pct(s.lastDay?.cpuAvg)}</dd></div>
<div><dt className="text-xs text-slate-500">Fullest disk</dt><dd className="tabular-nums">{pct(disk)}</dd></div>
</dl>
<div className="mt-3 flex flex-wrap gap-x-4 gap-y-1 text-xs text-slate-500">
{s.mode === "church" && <span>{s.deployments ?? 0} site(s)</span>}
<span>Last heard {ago(s.lastSeenAt)}</span>
{s.mode === "church" && s.frpcRunning === false && <span className="font-medium text-red-700">frpc not running</span>}
{s.central && !s.central.proxyBindAddr && <span className="font-medium text-red-700">proxy_bind_addr not set</span>}
</div>
{!!s.alerts?.length && (
<ul className="mt-3 space-y-1 text-xs">
{s.alerts.map(a => <li key={a.type} className={a.level === "critical" ? "text-red-700" : "text-amber-800"}>⚠ {a.message}</li>)}
</ul>
)}
</Link>
);
})}
</div>
)}
</div>
);
}