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>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-24 22:01:18 +02:00
1 parent 2f082ca5c2
commit 0146eb8bca
38 files changed
+8136 -13

No files matched your search

+5 -2
View File
@@ -75,6 +75,7 @@ const SOURCES: Record<string, { label: string; link: string }> = {
ups: { label: "UPS", link: "/ups" },
slo: { label: "Service level", link: "/service-levels" },
duplicati: { label: "Duplicati", link: "/duplicati" },
jellyfin: { label: "Jellyfin", link: "/jellyfin" },
secrets: { label: "Secrets", link: "/secrets" },
domains: { label: "Domains", link: "/dns?view=domains" },
endpoint: { label: "Endpoint", link: "/endpoints" },
@@ -113,6 +114,8 @@ function originOfConditionKey(key: string): { type: string; id: number | null; c
if (m) return { type: "dockhand", id: Number(m[1]), category: "services" };
m = /^duplicati-(?:failed|stale):(\d+)/.exec(key);
if (m) return { type: "duplicati", id: Number(m[1]), category: "backup" };
m = /^jellyfin-(?:restart|task|logins):(\d+)/.exec(key);
if (m) return { type: "jellyfin", id: Number(m[1]), category: key.startsWith("jellyfin-task") ? "automation" : "monitoring" };
return { type: "server", id: null, category: "disk" };
}
@@ -235,7 +238,7 @@ export async function collectAlerts(): Promise<AlertsReport> {
type: origin.type,
message: c.message,
key: c.key,
link: origin.type === "server" && origin.id !== null ? `/servers/${origin.id}${/^(failed-units|smart|zfs):/.test(c.key) ? "?view=health" : c.key.startsWith("new-port:") ? "?view=network" : c.key.startsWith("rule:") || c.key.startsWith("baseline:") ? "?view=history" : ""}` : origin.type === "unifi" ? "/network/devices" : origin.type === "ups" ? "/ups" : origin.type === "slo" ? "/service-levels" : origin.type === "dockhand" ? "/docker" : origin.type === "duplicati" ? "/duplicati" : undefined,
link: origin.type === "server" && origin.id !== null ? `/servers/${origin.id}${/^(failed-units|smart|zfs):/.test(c.key) ? "?view=health" : c.key.startsWith("new-port:") ? "?view=network" : c.key.startsWith("rule:") || c.key.startsWith("baseline:") ? "?view=history" : ""}` : origin.type === "unifi" ? "/network/devices" : origin.type === "ups" ? "/ups" : origin.type === "slo" ? "/service-levels" : origin.type === "dockhand" ? "/docker" : origin.type === "duplicati" ? "/duplicati" : origin.type === "jellyfin" ? "/jellyfin" : undefined,
silenced: subject !== null && subjects.has(subject), target: targetOfSubject(subject),
});
}
@@ -245,7 +248,7 @@ export async function collectAlerts(): Promise<AlertsReport> {
if (held.has("slo")) cannotCheck("Service-level targets", "couldn't be judged — see the server log", "Server and storage health");
if (held.has("server:baseline")) cannotCheck("What is usual for the time", "couldn't be judged — see the server log", "Server and storage health");
for (const h of held) {
const m = /^(proxmox|synology|unifi|ups|dockhand):(\d+)(?::(.+))?$/.exec(h);
const m = /^(proxmox|synology|unifi|ups|dockhand|jellyfin):(\d+)(?::(.+))?$/.exec(h);
if (!m || (m[3] && held.has(`${m[1]}:${m[2]}`))) continue;
const row = intById.get(Number(m[2]));
cannotCheck(`${SOURCES[m[1]].label} “${row?.name ?? `#${m[2]}`}”${m[3] ? ` (node ${m[3]})` : ""}`, "couldn't be read — see the Diagnostic Log", "Server and storage health");
+25 -1
View File
@@ -1,6 +1,6 @@
import { and, eq } from "drizzle-orm";
import { db } from "../db/client.js";
import { integrations, networkDevices, serverPorts, servers, upsState } from "../db/schema.js";
import { integrations, jellyfinState, networkDevices, serverPorts, servers, upsState } from "../db/schema.js";
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
import { createProxmoxAdapter, type ProxmoxNodeStats } from "../integrations/proxmox/adapter.js";
import { createSynologyAdapter, type SynologyStorageInfo } from "../integrations/synology/adapter.js";
@@ -20,6 +20,7 @@ import { baselineKey, computeBaselineFindings, type BaselineFinding } from "./ba
import { levelFindings, levelKey, type LevelFinding } from "./serviceLevels.js";
import { createDockhandAdapter } from "../integrations/dockhand/adapter.js";
import { currentProblems as duplicatiProblemsNow, type DuplicatiProblem } from "./duplicati.js";
import { isJellyfinFresh, jellyfinProblems, jellyfinSyncStatuses, type JellyfinProblem } from "./jellyfinSync.js";
import { evaluateContainers, loadContainerMemory, saveContainerMemory, type ContainerMemory, type DockerSnapshot } from "./containerHealth.js";
import { recordStorageSamples } from "./storageHistory.js";
import { isUpsFresh, upsProblems, upsSyncStatuses, type UpsStateRow } from "./upsSync.js";
@@ -87,6 +88,8 @@ export interface HealthSnapshot {
baselines?: BaselineFinding[];
/** Service-level targets whose error budget for the month is mostly or wholly used (services/serviceLevels.ts). */
levels?: LevelFinding[];
/** What is wrong with each Jellyfin, from what was stored, only while that reading is recent (services/jellyfinSync.ts). */
jellyfin?: { integrationId: number; problems: JellyfinProblem[] }[];
/** Duplicati backups that failed or have not succeeded in time (services/duplicati.ts). */
duplicati?: DuplicatiProblem[];
/** Each Docker integration's containers as last read, with what was remembered of them (services/containerHealth.ts). */
@@ -198,6 +201,9 @@ export function evaluateHealth(snapshot: HealthSnapshot, thresholds: Thresholds,
}
}
// A Jellyfin that wants a restart, a scheduled task that failed, a burst of failed sign-ins.
for (const j of snapshot.jellyfin ?? []) for (const p of j.problems) out.push({ key: p.key, source: `jellyfin:${j.integrationId}`, message: p.message, severity: p.severity });
// Duplicati backups that failed, or have not succeeded for longer than they should.
for (const p of snapshot.duplicati ?? []) out.push({ key: p.key, source: "duplicati", message: p.message, severity: p.severity });
@@ -428,6 +434,24 @@ export async function collectSnapshot(): Promise<{ snapshot: HealthSnapshot; hel
}
}
// Jellyfin: read by its own pass every couple of minutes; what is judged is what that pass stored, and only while it is recent.
try {
const rowsJ = await db.select({ id: integrations.id, name: integrations.name }).from(integrations).where(and(eq(integrations.type, "jellyfin"), eq(integrations.enabled, true)));
if (rowsJ.length > 0) {
const statuses = await jellyfinSyncStatuses();
for (const r of rowsJ) {
const [state] = await db.select().from(jellyfinState).where(eq(jellyfinState.integrationId, r.id)).limit(1);
if (!state || !isJellyfinFresh(statuses[r.id], Date.now())) {
held.add(`jellyfin:${r.id}`);
continue;
}
(snapshot.jellyfin ??= []).push({ integrationId: r.id, problems: jellyfinProblems(r.id, r.name, state) });
}
}
} catch (err) {
console.error("[health] couldn't judge Jellyfin:", err instanceof Error ? err.message : err);
}
// Duplicati: what its reports say, read from what is stored (nothing is asked of Duplicati). A failure here costs only this check.
try {
snapshot.duplicati = await duplicatiProblemsNow(Date.now());
+229
View File
@@ -0,0 +1,229 @@
/**
* Keeps what Jellyfin says up to date: every two minutes each Jellyfin integration is read (its version, who is streaming, how its scheduled tasks ended, how many sign-ins
* failed), the state is stored, and the number streaming is added to a history. Pages, charts and alerts are built from what is stored, so none of them talks to Jellyfin.
*
* Two things come of it besides a page. **Alerts**: a server that wants a restart, a scheduled task (a library scan) that failed, a burst of failed sign-ins. And **who is
* watching**: before an update run or a patch plan restarts the machine Jellyfin runs on, the app can say that people are streaming from it — and a patch plan waits for them to
* finish, for a while, instead of cutting a film off.
*/
import { and, desc, eq, lt } from "drizzle-orm";
import { db } from "../db/client.js";
import { integrations, jellyfinReadings, jellyfinState, servers } from "../db/schema.js";
import { createJellyfinAdapter, type JellyfinRead, type JellyfinStream, type JellyfinTask } from "../integrations/jellyfin/adapter.js";
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
import { hostOfAddress } from "./kumaTargets.js";
import { matchServerForTarget } from "./uptimeKumaMatch.js";
import { getInternalFlag, setInternalFlag } from "./settingsStore.js";
export const SYNC_INTERVAL_MS = 2 * 60_000;
/** After this long without a good read, what is stored is no longer believed. */
export const STALE_AFTER_MS = 10 * 60_000;
export const READING_KEEP_DAYS = 35;
const READING_GAP_MS = 5 * 60_000 - 20_000;
/** This many failed sign-ins in a day is a warning. */
export const FAILED_LOGINS_WARN = 10;
const STATUS_FLAG = "jellyfinSync";
export interface JellyfinSyncStatus {
/** The last read that worked (kept when a later one failed). */
at: string;
ok: boolean;
error: string | null;
}
async function readStatuses(): Promise<Record<string, JellyfinSyncStatus>> {
try {
const raw = await getInternalFlag(STATUS_FLAG);
return raw ? (JSON.parse(raw) as Record<string, JellyfinSyncStatus>) : {};
} catch {
return {};
}
}
export async function jellyfinSyncStatuses(): Promise<Record<number, JellyfinSyncStatus>> {
return Object.fromEntries(Object.entries(await readStatuses()).map(([k, v]) => [Number(k), v]));
}
export const isJellyfinFresh = (status: JellyfinSyncStatus | undefined, nowMs: number) => !!status && status.ok && nowMs - Date.parse(status.at) <= STALE_AFTER_MS;
export type StateRow = typeof jellyfinState.$inferSelect;
const parse = <T>(text: string): T[] => {
try {
const v = JSON.parse(text);
return Array.isArray(v) ? (v as T[]) : [];
} catch {
return [];
}
};
export const streamsOf = (row: Pick<StateRow, "streams">) => parse<JellyfinStream>(row.streams);
export const tasksOf = (row: Pick<StateRow, "tasks">) => parse<JellyfinTask>(row.tasks);
/** Writes down one read: the state, and a point in the history when it is time (or when the number streaming has changed). */
export async function storeRead(integrationId: number, r: JellyfinRead, now: Date = new Date()): Promise<void> {
const nowIso = now.toISOString();
const values = {
serverName: r.name,
version: r.version,
os: r.os,
pendingRestart: r.pendingRestart,
updateAvailable: r.updateAvailable,
streams: JSON.stringify(r.streams),
tasks: JSON.stringify(r.tasks),
movies: r.counts.movies,
series: r.counts.series,
episodes: r.counts.episodes,
songs: r.counts.songs,
failedLogins24h: r.failedLogins24h,
updatedAt: nowIso,
};
await db.insert(jellyfinState).values({ integrationId, ...values }).onConflictDoUpdate({ target: jellyfinState.integrationId, set: values });
const transcodes = r.streams.filter((s) => s.transcoding).length;
const [newest] = await db.select({ at: jellyfinReadings.at, streams: jellyfinReadings.streams, transcodes: jellyfinReadings.transcodes }).from(jellyfinReadings).where(eq(jellyfinReadings.integrationId, integrationId)).orderBy(desc(jellyfinReadings.at)).limit(1);
const due = !newest || newest.streams !== r.streams.length || newest.transcodes !== transcodes || now.getTime() - Date.parse(newest.at) >= READING_GAP_MS;
if (due) {
await db.insert(jellyfinReadings).values({ integrationId, at: nowIso, streams: r.streams.length, transcodes });
}
await db.delete(jellyfinReadings).where(and(eq(jellyfinReadings.integrationId, integrationId), lt(jellyfinReadings.at, new Date(now.getTime() - READING_KEEP_DAYS * 86_400_000).toISOString())));
}
/** Reads one Jellyfin and stores it. Never throws: how it went is the answer, and what was stored is left alone when it failed. */
export async function syncJellyfin(integrationId: number, now: Date = new Date(), read?: (config: Record<string, unknown>) => Promise<JellyfinRead>): Promise<JellyfinSyncStatus> {
let status: JellyfinSyncStatus;
try {
const loaded = await loadIntegrationConfig(integrationId);
if (!loaded || loaded.integration.type !== "jellyfin") throw new Error("not a Jellyfin integration");
const result = await (read ? read(loaded.config) : createJellyfinAdapter(loaded.config as never).read(now));
await storeRead(integrationId, result, now);
status = { at: now.toISOString(), ok: true, error: null };
} catch (err) {
const previous = (await readStatuses())[String(integrationId)];
status = { at: previous?.at ?? now.toISOString(), ok: false, error: err instanceof Error ? err.message : String(err) };
console.error(`[jellyfin] couldn't read integration ${integrationId}:`, status.error);
}
try {
const all = await readStatuses();
all[String(integrationId)] = status;
await setInternalFlag(STATUS_FLAG, JSON.stringify(all));
} catch (err) {
console.error("[jellyfin] couldn't record the result:", err);
}
return status;
}
export async function syncAllJellyfin(now: Date = new Date()): Promise<Record<number, JellyfinSyncStatus>> {
const rows = await db.select({ id: integrations.id }).from(integrations).where(and(eq(integrations.type, "jellyfin"), eq(integrations.enabled, true)));
const out: Record<number, JellyfinSyncStatus> = {};
for (const r of rows) out[r.id] = await syncJellyfin(r.id, now);
return out;
}
export async function forgetJellyfin(integrationId: number): Promise<void> {
const all = await readStatuses();
if (String(integrationId) in all) {
delete all[String(integrationId)];
await setInternalFlag(STATUS_FLAG, JSON.stringify(all));
}
}
// ─── What is wrong ──────────────────────────────────────────────────────────
export interface JellyfinProblem {
key: string;
message: string;
severity: "warning";
}
/** Pure: what is wrong with one Jellyfin, from what is stored for it. */
export function jellyfinProblems(id: number, name: string, s: Pick<StateRow, "pendingRestart" | "tasks" | "failedLogins24h">): JellyfinProblem[] {
const out: JellyfinProblem[] = [];
if (s.pendingRestart) out.push({ key: `jellyfin-restart:${id}`, severity: "warning", message: `${name} is waiting to be restarted — a plugin or an update needs it` });
for (const t of tasksOf(s)) {
if (t.lastStatus === "Failed") out.push({ key: `jellyfin-task:${id}:${t.name}`, severity: "warning", message: `${name}: the scheduled task “${t.name}” failed${t.error ? ` — ${t.error}` : ""}` });
}
if (s.failedLogins24h !== null && s.failedLogins24h >= FAILED_LOGINS_WARN) out.push({ key: `jellyfin-logins:${id}`, severity: "warning", message: `${name}: ${s.failedLogins24h} sign-ins failed in the last day` });
return out;
}
// ─── Who is watching ────────────────────────────────────────────────────────
export interface Watching {
integrationId: number;
integrationName: string;
streams: JellyfinStream[];
}
/** Pure: a sentence about who is watching, for a warning or a waiting message: "2 people are streaming from Jellyfin (Ann: Dune, Bo: The Office — S1E2)". */
export function watchingWords(w: Watching[]): string {
const streams = w.flatMap((x) => x.streams);
if (streams.length === 0) return "";
const names = w.map((x) => x.integrationName);
const who = streams.slice(0, 3).map((s) => `${s.user}: ${s.title}`).join(", ");
const rest = streams.length > 3 ? ` and ${streams.length - 3} more` : "";
const transcoding = streams.filter((s) => s.transcoding).length;
return `${streams.length === 1 ? "1 person is" : `${streams.length} people are`} streaming from ${[...new Set(names)].join(" and ")} (${who}${rest})${transcoding > 0 ? `, ${transcoding} being transcoded` : ""}`;
}
/** The Jellyfins that run on this server — the one whose address is the server's — and who is streaming from each, as last read and believed. Only those with somebody streaming are listed. */
export async function watchingOnServer(serverId: number, now: Date = new Date()): Promise<Watching[]> {
const rows = await db.select({ id: integrations.id, name: integrations.name, baseUrl: integrations.baseUrl }).from(integrations).where(and(eq(integrations.type, "jellyfin"), eq(integrations.enabled, true)));
if (rows.length === 0) return [];
const statuses = await jellyfinSyncStatuses();
const serverRows = await db.select({ id: servers.id, name: servers.name, hostname: servers.hostname, ipAddresses: servers.ipAddresses }).from(servers);
const matchable = serverRows.map((s) => {
let ips: string[] = [];
try {
const v = JSON.parse(s.ipAddresses ?? "[]");
ips = Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : [];
} catch {
// no addresses
}
return { id: s.id, name: s.name, hostname: s.hostname, ips };
});
const out: Watching[] = [];
for (const r of rows) {
if (!isJellyfinFresh(statuses[r.id], now.getTime())) continue;
if (matchServerForTarget(hostOfAddress(r.baseUrl), matchable)?.id !== serverId) continue;
const [state] = await db.select().from(jellyfinState).where(eq(jellyfinState.integrationId, r.id)).limit(1);
const streams = state ? streamsOf(state) : [];
if (streams.length > 0) out.push({ integrationId: r.id, integrationName: r.name, streams });
}
return out;
}
export interface PeakPoint {
/** ISO. */
t: string;
/** The most that were streaming, and the most transcoded, in this stretch. */
streams: number;
transcodes: number;
}
/** Pure: readings into at most `maxPoints` equal slices of [from, to], each as its peak — an average would hide the evening somebody watched. */
export function bucketPeaks(rows: { at: string; streams: number; transcodes: number }[], from: number, to: number, maxPoints: number): PeakPoint[] {
if (rows.length === 0 || to <= from) return [];
const size = Math.max((to - from) / maxPoints, 1);
const buckets = new Map<number, { tSum: number; n: number; streams: number; transcodes: number }>();
for (const r of rows) {
const t = Date.parse(r.at);
if (t < from || t > to) continue;
const key = Math.min(maxPoints - 1, Math.floor((t - from) / size));
const b = buckets.get(key) ?? { tSum: 0, n: 0, streams: 0, transcodes: 0 };
b.tSum += t;
b.n += 1;
b.streams = Math.max(b.streams, r.streams);
b.transcodes = Math.max(b.transcodes, r.transcodes);
buckets.set(key, b);
}
return [...buckets.entries()].sort((a, b) => a[0] - b[0]).map(([, b]) => ({ t: new Date(b.tSum / b.n).toISOString(), streams: b.streams, transcodes: b.transcodes }));
}
let timer: ReturnType<typeof setInterval> | null = null;
export function initJellyfinSync(): void {
if (timer) return;
const pass = () => void syncAllJellyfin().catch((err) => console.error("[jellyfin] pass failed:", err));
timer = setInterval(pass, SYNC_INTERVAL_MS);
setTimeout(pass, 11_000);
console.log(`[jellyfin] reading Jellyfin every ${SYNC_INTERVAL_MS / 60000} minutes`);
}
+2
View File
@@ -31,6 +31,8 @@ export function subjectOfConditionKey(key: string): string | null {
if (m) return `server:${m[1]}`;
m = /^container-(?:restarting|unhealthy|stopped|created|host):(\d+):/.exec(key);
if (m) return `integration:${m[1]}`;
m = /^jellyfin-(?:restart|task|logins):(\d+)/.exec(key);
if (m) return `integration:${m[1]}`;
m = /^duplicati-(?:failed|stale):\d+:server:(\d+)$/.exec(key);
if (m) return `server:${m[1]}`;
m = /^(?:endpoint(?:-cert)?|heartbeat(?:-fail)?):\d+:server:(\d+)$/.exec(key);
+18 -1
View File
@@ -5,10 +5,11 @@
*/
import { eq } from "drizzle-orm";
import { db } from "../db/client.js";
import { assets, endpointChecks, heartbeats, integrations, maintenanceWindows, secrets, servers, upsState } from "../db/schema.js";
import { assets, endpointChecks, heartbeats, integrations, maintenanceWindows, secrets, servers, upsState, jellyfinState } from "../db/schema.js";
import { isUpsFresh, upsSyncStatuses } from "./upsSync.js";
import { levelStatuses } from "./serviceLevels.js";
import { backupState, listAll as listDuplicati } from "./duplicati.js";
import { isJellyfinFresh, jellyfinSyncStatuses, streamsOf } from "./jellyfinSync.js";
import { latestTests } from "./restoreTests.js";
import { heartbeatState, parseStamp } from "./heartbeats.js";
import { env } from "../env.js";
@@ -60,6 +61,21 @@ class Exposition {
}
}
/** Each Jellyfin, for /metrics: how many are streaming, how many of those are transcoded, and whether it wants a restart. Only while the reading is recent. */
async function addJellyfinMetrics(out: Exposition, now: Date) {
const statuses = await jellyfinSyncStatuses();
const names = new Map((await db.select({ id: integrations.id, name: integrations.name, enabled: integrations.enabled }).from(integrations).where(eq(integrations.type, "jellyfin"))).map((r) => [r.id, r]));
for (const s of await db.select().from(jellyfinState)) {
const row = names.get(s.integrationId);
if (!row || !row.enabled || !isJellyfinFresh(statuses[s.integrationId], now.getTime())) continue;
const labels = { name: row.name };
const streams = streamsOf(s);
out.add("homelab_jellyfin_streams", "gauge", "How many are streaming from Jellyfin right now.", streams.length, labels);
out.add("homelab_jellyfin_transcodes", "gauge", "How many of those streams Jellyfin is transcoding.", streams.filter((x) => x.transcoding).length, labels);
out.add("homelab_jellyfin_pending_restart", "gauge", "1 if Jellyfin is waiting to be restarted, else 0.", s.pendingRestart ? 1 : 0, labels);
}
}
/** Each Duplicati backup, for /metrics: whether it is in order, how long since it last succeeded, and how big what it backs up is. */
async function addDuplicatiMetrics(out: Exposition, now: Date) {
const { sources, backups } = await listDuplicati();
@@ -267,6 +283,7 @@ export async function buildMetrics(now: Date = new Date()): Promise<string> {
await addUpsMetrics(out, now);
await addServiceLevelMetrics(out, now);
await addDuplicatiMetrics(out, now);
await addJellyfinMetrics(out, now);
await addHeartbeatMetrics(out, now);
return out.toString();
}
+21 -1
View File
@@ -17,6 +17,7 @@ import { invalidateAlertsCache } from "./alerts.js";
import { recordAudit } from "./audit.js";
import { impactOf } from "./dependencies.js";
import { startOrExtendWindow } from "./maintenance.js";
import { watchingOnServer, watchingWords } from "./jellyfinSync.js";
import { occurrencesBetween, type Occurrence } from "./maintenanceSchedules.js";
import { notifyPatchPlanFinished } from "./notify.js";
import { parseTags } from "./serverTags.js";
@@ -36,8 +37,13 @@ export interface PlanItem {
message: string | null;
/** Windows made for what depends on this server, which end with it. */
windowIds: number[];
/** When its turn came and somebody was streaming from it, so it was left a while (ISO). */
waitingSince?: string | null;
}
/** How long a server is left, when it is its turn and people are streaming from it, before it is updated anyway. */
export const WATCHING_WAIT_MS = 2 * 3_600_000;
/** How late a plan may still start, if the app was not running at its time. */
export const START_GRACE_MS = 30 * 60_000;
export const POLL_INTERVAL_MS = 20_000;
@@ -217,6 +223,19 @@ async function advanceRun(run: PlanRunRow, now: Date): Promise<void> {
}
}
const actor = `Patch plan "${plan.name}"`;
// People streaming from it (Jellyfin on this machine): its turn waits, up to two hours, rather than cutting a film off.
const watching = await watchingOnServer(server.id, now);
let waited = "";
if (watching.length > 0) {
const since = next.waitingSince ? Date.parse(next.waitingSince) : now.getTime();
if (!next.waitingSince) next.waitingSince = now.toISOString();
if (now.getTime() - since < WATCHING_WAIT_MS) {
next.message = `Waiting: ${watchingWords(watching)}.`;
await saveRun(run, items);
return; // looked at again at the next pass
}
waited = `Went ahead after waiting ${Math.round((now.getTime() - since) / 3_600_000)} h; ${watchingWords(watching)}. `;
}
try {
const ur = await startUpdateRun(
{
@@ -227,10 +246,11 @@ async function advanceRun(run: PlanRunRow, now: Date): Promise<void> {
limit: plan.limitFrom === "hostname" && server.hostname ? server.hostname : server.name,
windowMinutes: plan.windowMinutes,
actor,
startAnyway: true,
},
now,
);
Object.assign(next, { status: "running", updateRunId: ur.id, message: "The update run has started.", windowIds: await silenceDependents(plan, server.id, server.name, actor, now) });
Object.assign(next, { status: "running", updateRunId: ur.id, message: `${waited}The update run has started.`, windowIds: await silenceDependents(plan, server.id, server.name, actor, now) });
await saveRun(run, items);
return; // one at a time: this one is followed until it is over
} catch (err) {
+9
View File
@@ -15,6 +15,7 @@ import { createSemaphoreAdapter, type SemaphoreAdapter, type SemaphoreTask } fro
import { invalidateAlertsCache } from "./alerts.js";
import { recordAudit } from "./audit.js";
import { startOrExtendWindow } from "./maintenance.js";
import { watchingOnServer, watchingWords } from "./jellyfinSync.js";
import { notifyUpdateRunFinished } from "./notify.js";
import { parsePendingUpdates } from "./pendingUpdates.js";
@@ -68,6 +69,8 @@ export interface StartInput {
/** How long the maintenance window lasts at most; it ends sooner when the run does. */
windowMinutes: number;
actor: string;
/** Go ahead although somebody is streaming from the machine (a person who has seen the warning, or a plan that has waited long enough). */
startAnyway?: boolean;
}
export async function startUpdateRun(input: StartInput, now: Date = new Date()): Promise<UpdateRunRow> {
@@ -82,6 +85,12 @@ export async function startUpdateRun(input: StartInput, now: Date = new Date()):
.limit(1);
if (busy) throw new UpdateRunError(409, "already_running", `An update run for ${server.name} is already under way.`);
// Jellyfin on this machine, with people watching: the run may restart it. Said, and the person asked again, rather than cutting a film off.
if (!input.startAnyway) {
const watching = await watchingOnServer(server.id, now);
if (watching.length > 0) throw new UpdateRunError(409, "watching", `${watchingWords(watching)}. Wait until they have finished, or start it anyway.`);
}
const adapter = await semaphoreFor(input.integrationId);
if (!adapter) throw new UpdateRunError(400, "no_semaphore", "That Semaphore integration doesn't exist or isn't switched on.");