Files
Homelab-manager/web/src/components/UpdateRunDrawer.tsx
T
bobbanandClaude Sonnet 5.5 0146eb8bca Jellyfin: who is watching, and don't update a machine mid-film
New Jellyfin integration (read-only, every 2 minutes): version, restart wanted,
streams and transcoding, failed scheduled tasks, item counts, failed sign-ins.
Monitoring -> Jellyfin page with a peak-streams history, three alerts, /metrics.

When Jellyfin runs on a managed server, an update run asks first if people are
streaming (409 "watching" unless startAnyway), and a patch plan waits up to two
hours for them before updating that server.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-24 22:01:18 +02:00

181 lines
8.0 KiB
TypeScript

import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import ImpactNote from "./ImpactNote";
import { api, type IntegrationSummary, type SemaphoreTemplate, type UpdatesServer } from "../api/client";
import { confirmDialog } from "../utils/dialogs";
import { readableError } from "../utils/errors";
import { toast } from "../utils/toast";
import DetailDrawer from "./DetailDrawer";
import { BlockSkeleton } from "./Skeleton";
import UpdatesBadge from "./UpdatesBadge";
interface Loaded {
integration: IntegrationSummary;
templates: SemaphoreTemplate[];
}
const WINDOWS = [
{ minutes: 45, label: "45 minutes" },
{ minutes: 90, label: "1½ hours" },
{ minutes: 180, label: "3 hours" },
{ minutes: 360, label: "6 hours" },
];
const LAST_KEY = "hlm.updateRun.template";
const remembered = () => {
try {
return localStorage.getItem(LAST_KEY) ?? "";
} catch {
return "";
}
};
/**
* "Update this server now": pick the playbook that applies updates, and Homelab Manager puts the server under maintenance, runs it for this
* server only, follows it, waits for the server to report again and says what is left and whether a restart is due. It asks before it
* starts, and says what it will do.
*/
export default function UpdateRunDrawer({ server, onClose, onStarted }: { server: UpdatesServer; onClose: () => void; onStarted: () => void }) {
const [loaded, setLoaded] = useState<Loaded[] | null>(null);
const [failed, setFailed] = useState(false);
const [choice, setChoice] = useState(remembered);
const [limit, setLimit] = useState(server.name);
const [minutes, setMinutes] = useState(90);
const [busy, setBusy] = useState(false);
// People streaming from a Jellyfin on this machine: the update may restart it, so they are named first and the person asked.
const [watching, setWatching] = useState("");
useEffect(() => {
let alive = true;
api.jellyfin
.watching(server.id)
.then((r) => alive && setWatching(r.words))
.catch(() => undefined); // no Jellyfin, or it can't be read: nothing to warn about
return () => {
alive = false;
};
}, [server.id]);
useEffect(() => {
let alive = true;
api.integrations
.list()
.then(async (res) => {
const sems = res.integrations.filter((i) => i.type === "semaphore" && i.enabled);
const all = await Promise.all(sems.map((integration) => api.integrations.semaphore.templates(integration.id).then((r): Loaded => ({ integration, templates: r.templates })).catch((): Loaded => ({ integration, templates: [] }))));
if (alive) setLoaded(all);
})
.catch(() => alive && setFailed(true));
return () => {
alive = false;
};
}, []);
const options = (loaded ?? []).flatMap((l) =>
l.templates.map((t) => ({ key: `${l.integration.id}:${t.projectId}:${t.id}`, integration: l.integration, template: t, label: `${t.name} — ${t.projectName}${(loaded ?? []).length > 1 ? ` (${l.integration.name})` : ""}` })),
);
const picked = options.find((o) => o.key === choice);
const count = server.updates?.count ?? null;
async function start() {
if (!picked) return;
const ok = await confirmDialog({
title: `Update ${server.name} now?`,
message: `${watching ? `${watching[0].toUpperCase()}${watching.slice(1)}, and the update may restart the machine. ` : ""}This silences alerts for ${server.name} for up to ${WINDOWS.find((w) => w.minutes === minutes)?.label ?? `${minutes} minutes`}, runs "${picked.template.name}" in ${picked.integration.name} limited to ${limit.trim()}, and waits for the server to report again. The playbook may restart it.`,
confirmLabel: watching ? "Update anyway" : "Start the update",
});
if (!ok) return;
setBusy(true);
try {
await api.updateRuns.start({ serverId: server.id, integrationId: picked.integration.id, projectId: picked.template.projectId, templateId: picked.template.id, limit: limit.trim(), windowMinutes: minutes, startAnyway: watching !== "" });
try {
localStorage.setItem(LAST_KEY, picked.key);
} catch {
// a remembered choice is only a convenience
}
toast.success(`Started updating ${server.name}.`);
onStarted();
onClose();
} catch (err) {
toast.error(readableError(err));
} finally {
setBusy(false);
}
}
return (
<DetailDrawer title={`Update ${server.name}`} subtitle={server.osType === "windows" ? "Windows" : "Linux"} onClose={onClose}>
<div className="mb-3">
<UpdatesBadge updates={server.updates} />
{count !== null && count > 0 && server.updates && server.updates.security !== null && <span className="text-secondary small ms-2">{server.updates.security} of them security</span>}
</div>
{failed ? (
<div className="text-secondary">Couldn't load the integrations.</div>
) : loaded === null ? (
<BlockSkeleton lines={3} />
) : loaded.length === 0 ? (
<div className="text-secondary">
There is no Semaphore connected. Add one under <Link to="/integrations">Integrations</Link>, with a playbook that applies updates, and this server can be updated from here.
</div>
) : options.length === 0 ? (
<div className="text-secondary">No templates found in Semaphore.</div>
) : (
<>
<div className="mb-3">
<ImpactNote targets={[{ type: "server", id: server.id }]} verb="may look down while it restarts" />
</div>
{watching && (
<div className="alert alert-warning" role="status">
<strong>Somebody is watching.</strong> {watching[0].toUpperCase()}
{watching.slice(1)}. The update may restart this machine; you will be asked once more.
</div>
)}
<ol className="text-secondary small ps-3 mb-3">
<li>Alerts for this server are silenced (a maintenance window).</li>
<li>The playbook runs in Semaphore, for this server only.</li>
<li>Homelab Manager waits for the server to report again — it may be restarting.</li>
<li>It tells you what is still waiting and whether a restart is due, and the window ends.</li>
</ol>
<div className="mb-3">
<label className="form-label" htmlFor="ur-template">
Playbook that applies updates
</label>
<select id="ur-template" className="form-select" value={choice} onChange={(e) => setChoice(e.target.value)}>
<option value="">Choose a template…</option>
{options.map((o) => (
<option key={o.key} value={o.key}>
{o.label}
</option>
))}
</select>
</div>
<div className="mb-3">
<label className="form-label" htmlFor="ur-limit">
Run it on — the host name as it is in the Semaphore inventory
</label>
<input id="ur-limit" className="form-control" value={limit} maxLength={200} onChange={(e) => setLimit(e.target.value)} placeholder="e.g. web01" />
<div className="form-hint text-secondary small mt-1">An update run is always for this one server, never the whole inventory.</div>
</div>
<div className="mb-3">
<label className="form-label" htmlFor="ur-window">
Silence its alerts for up to
</label>
<select id="ur-window" className="form-select" value={minutes} onChange={(e) => setMinutes(Number(e.target.value))}>
{WINDOWS.map((w) => (
<option key={w.minutes} value={w.minutes}>
{w.label}
</option>
))}
</select>
<div className="form-hint text-secondary small mt-1">The window ends sooner, as soon as the run does.</div>
</div>
<button type="button" className="btn btn-primary" disabled={!picked || busy || limit.trim() === ""} onClick={() => void start()}>
{busy ? "Starting…" : "Update now"}
</button>
</>
)}
</DetailDrawer>
);
}