Servarr events: don't crash the runtime when unconfigured
The radarr/sonarr/lidarr/bookshelf events entrypoints built their client with XClient.fromEnv() at import time, which throws when no API key/URL is available yet — crash-looping the runtime container. The tools already guard this; the events entrypoint did not. Mirror the tools' guard: build the client in a try/catch, and only start the poll loop when it succeeds. When it fails, log one line and stay idle until a key is available. Behaviour when configured is unchanged. Claude-Session: https://claude.ai/code/session_01LrgweAeERJYBg88c5cKDzF
This commit is contained in:
+18
-4
@@ -14,7 +14,17 @@
|
||||
import { emit } from "@novox/mesh-sdk/events";
|
||||
import { LidarrClient, type LidarrQueueItem } from "./client.js";
|
||||
|
||||
const lidarr = LidarrClient.fromEnv();
|
||||
// Building the client throws when Lidarr has no URL/key yet. Like the tools (see tools/index.ts),
|
||||
// the events entrypoint must not crash the runtime for that — it stays idle until configured.
|
||||
function buildClient(): LidarrClient | null {
|
||||
try {
|
||||
return LidarrClient.fromEnv();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const lidarr = buildClient();
|
||||
|
||||
// Lidarr removes an item from the queue once it has been imported; a "warning"/"failed" status is
|
||||
// how a stuck or broken grab shows itself, so we do not call those a completion when they vanish.
|
||||
@@ -23,7 +33,7 @@ const FAILED_STATUSES = new Set(["failed", "warning"]);
|
||||
const inQueue = new Map<number, LidarrQueueItem>();
|
||||
let primed = false;
|
||||
|
||||
async function pollQueue(): Promise<void> {
|
||||
async function pollQueue(lidarr: LidarrClient): Promise<void> {
|
||||
const { items } = await lidarr.getQueue();
|
||||
const now = new Map(items.map((i) => [i.id, i]));
|
||||
|
||||
@@ -50,6 +60,10 @@ const tick = (fn: () => Promise<void>, everyMs: number): void => {
|
||||
setInterval(run, everyMs);
|
||||
run();
|
||||
};
|
||||
tick(pollQueue, 30_000);
|
||||
|
||||
console.log("[lidarr] watching the download queue, emitting grabs and completions");
|
||||
if (lidarr) {
|
||||
tick(() => pollQueue(lidarr), 30_000);
|
||||
console.log("[lidarr] watching the download queue, emitting grabs and completions");
|
||||
} else {
|
||||
console.log("[lidarr] not configured — events idle until an API key is available");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user