Feature: Push-Benachrichtigungen (ntfy/Webhook) bei Paket fertig/fehlgeschlagen + Run-Ende

Headless-Server: Paket-Ausgaenge waren bisher nur per RDP+Log sichtbar. Neues
Modul notify.ts schickt einen fire-and-forget POST (ntfy-kompatibel: Title/
Priority/Tags als Header, Nachricht als Body) an eine konfigurierbare URL —
mit der kostenlosen ntfy-App aufs Handy, ohne Account/Port/Firewall (outbound).

- Settings: notifyUrl + 3 Ereignis-Toggles (Default aus) in Allgemein.
- Hook 1: Post-Processing-Ende (Paket completed/failed nach Entpacken).
- Hook 2: refreshPackageStatus fuer den Alle-Items-fehlgeschlagen-Fall (Link
  tot -> Paket erreicht das Post-Processing nie; ohne diesen Hook schwiege
  ausgerechnet der haeufigste Fehlerfall).
- Hook 3: finishRun mit Run-Summary (X/Y erfolgreich, Dauer, Schnitt).
- Dedup-Set pro Paket+Run, Lifecycle gespiegelt an historyRecordedPackages
  (Run-Start-Clear, Retry-Deletes, removePackageFromSession). Guard
  session.running || runPackageIds.has(id): nachlaufendes Entpacken nach
  Run-Ende benachrichtigt noch, Startup-Recovery nach App-Neustart nicht
  (sonst Doppel-Push fuer laengst fertige Pakete).
- 5s-Timeout, Fehler nur als logger.warn — blockiert nie den Download-Pfad.
- 9 Unit-Tests fuer notify.ts.
This commit is contained in:
Sucukdeluxe 2026-06-09 20:50:58 +02:00
parent be4d54a6b5
commit e753ea1296
7 changed files with 176 additions and 0 deletions

View File

@ -107,6 +107,10 @@ export function defaultSettings(): AppSettings {
hideExtractedItems: true, hideExtractedItems: true,
confirmDeleteSelection: true, confirmDeleteSelection: true,
backupIncludeDownloads: false, backupIncludeDownloads: false,
notifyUrl: "",
notifyOnPackageCompleted: false,
notifyOnPackageFailed: false,
notifyOnRunFinished: false,
totalDownloadedAllTime: 0, totalDownloadedAllTime: 0,
totalCompletedFilesAllTime: 0, totalCompletedFilesAllTime: 0,
totalRuntimeAllTimeMs: 0, totalRuntimeAllTimeMs: 0,

View File

@ -56,6 +56,7 @@ import { cleanupArchives, clearExtractResumeState, collectArchiveCleanupTargets,
import { validateFileAgainstManifest } from "./integrity"; import { validateFileAgainstManifest } from "./integrity";
import { classifyDiskError } from "./fs-error"; import { classifyDiskError } from "./fs-error";
import { processVideoFile, resolveVideoTooling, stripDualLangMarker, hasDualLangMarker, isRemuxableVideoFile, type GermanAudioMode, type VideoProcessResult } from "./video-processor"; import { processVideoFile, resolveVideoTooling, stripDualLangMarker, hasDualLangMarker, isRemuxableVideoFile, type GermanAudioMode, type VideoProcessResult } from "./video-processor";
import { sendNotification } from "./notify";
import { logger } from "./logger"; import { logger } from "./logger";
import { getRecentRotationEvents, runWithRotationItemSink, setRotationEventListener } from "./account-rotation-log"; import { getRecentRotationEvents, runWithRotationItemSink, setRotationEventListener } from "./account-rotation-log";
import type { RotationEvent } from "../shared/types"; import type { RotationEvent } from "../shared/types";
@ -1750,6 +1751,8 @@ export class DownloadManager extends EventEmitter {
private historyRecordedPackages = new Set<string>(); private historyRecordedPackages = new Set<string>();
private notifiedPackages = new Set<string>();
private itemCount = 0; private itemCount = 0;
private lastSchedulerHeartbeatAt = 0; private lastSchedulerHeartbeatAt = 0;
@ -2788,6 +2791,7 @@ export class DownloadManager extends EventEmitter {
this.runOutcomes.clear(); this.runOutcomes.clear();
this.runCompletedPackages.clear(); this.runCompletedPackages.clear();
this.historyRecordedPackages.clear(); this.historyRecordedPackages.clear();
this.notifiedPackages.clear();
this.retryAfterByItem.clear(); this.retryAfterByItem.clear();
this.providerStartReservations.clear(); this.providerStartReservations.clear();
this.pacedStartReservationByItem.clear(); this.pacedStartReservationByItem.clear();
@ -5109,6 +5113,7 @@ export class DownloadManager extends EventEmitter {
pkg.enabled = true; pkg.enabled = true;
pkg.updatedAt = nowMs(); pkg.updatedAt = nowMs();
this.historyRecordedPackages.delete(packageId); this.historyRecordedPackages.delete(packageId);
this.notifiedPackages.delete(packageId);
if (this.session.running) { if (this.session.running) {
for (const itemId of itemIds) { for (const itemId of itemIds) {
@ -5176,6 +5181,7 @@ export class DownloadManager extends EventEmitter {
this.abortPackagePostProcessing(pkgId, "reset"); this.abortPackagePostProcessing(pkgId, "reset");
this.runCompletedPackages.delete(pkgId); this.runCompletedPackages.delete(pkgId);
this.historyRecordedPackages.delete(pkgId); this.historyRecordedPackages.delete(pkgId);
this.notifiedPackages.delete(pkgId);
const pkg = this.session.packages[pkgId]; const pkg = this.session.packages[pkgId];
if (pkg && (pkg.status === "completed" || pkg.status === "failed" || pkg.status === "cancelled")) { if (pkg && (pkg.status === "completed" || pkg.status === "failed" || pkg.status === "cancelled")) {
@ -7608,6 +7614,7 @@ export class DownloadManager extends EventEmitter {
} }
} }
this.historyRecordedPackages.delete(packageId); this.historyRecordedPackages.delete(packageId);
this.notifiedPackages.delete(packageId);
this.abortPackagePostProcessing(packageId, "package_removed"); this.abortPackagePostProcessing(packageId, "package_removed");
for (const itemId of itemIds) { for (const itemId of itemIds) {
this.retryAfterByItem.delete(itemId); this.retryAfterByItem.delete(itemId);
@ -10468,6 +10475,34 @@ export class DownloadManager extends EventEmitter {
return /\b0\s*B\b/i.test(item.fullStatus || ""); return /\b0\s*B\b/i.test(item.fullStatus || "");
} }
// Once per package and run; trailing post-processing after run-end still
// notifies (runPackageIds keeps the id), startup recovery does not (set empty).
private notifyPackageOutcome(pkg: PackageEntry, kind: "completed" | "failed", detail: string): void {
const url = String(this.settings.notifyUrl || "").trim();
if (!url) {
return;
}
if (kind === "completed" && !this.settings.notifyOnPackageCompleted) {
return;
}
if (kind === "failed" && !this.settings.notifyOnPackageFailed) {
return;
}
if (!this.session.running && !this.runPackageIds.has(pkg.id)) {
return;
}
if (this.notifiedPackages.has(pkg.id)) {
return;
}
this.notifiedPackages.add(pkg.id);
void sendNotification(url, {
title: kind === "completed" ? "Paket fertig" : "Paket fehlgeschlagen",
message: `${pkg.name}\n${detail}`,
priority: kind === "failed" ? "high" : "default",
tags: kind === "completed" ? "white_check_mark" : "x"
});
}
private refreshPackageStatus(pkg: PackageEntry): void { private refreshPackageStatus(pkg: PackageEntry): void {
let pending = 0; let pending = 0;
let success = 0; let success = 0;
@ -10501,6 +10536,7 @@ export class DownloadManager extends EventEmitter {
return; return;
} }
const prevStatus = pkg.status;
if (failed > 0) { if (failed > 0) {
pkg.status = "failed"; pkg.status = "failed";
} else if (cancelled > 0) { } else if (cancelled > 0) {
@ -10509,6 +10545,11 @@ export class DownloadManager extends EventEmitter {
pkg.status = "completed"; pkg.status = "completed";
} }
pkg.updatedAt = nowMs(); pkg.updatedAt = nowMs();
// A package whose items ALL failed never enters post-processing, so the
// post-process notify hook can't fire for it — cover that case here.
if (pkg.status === "failed" && prevStatus !== "failed" && success === 0) {
this.notifyPackageOutcome(pkg, "failed", `${failed} von ${total} Datei(en) fehlgeschlagen`);
}
} }
private cachedSpeedLimitKbps = 0; private cachedSpeedLimitKbps = 0;
@ -11731,6 +11772,12 @@ export class DownloadManager extends EventEmitter {
pkg.status = "completed"; pkg.status = "completed";
} }
if (pkg.status === "completed") {
this.notifyPackageOutcome(pkg, "completed", `${success} Datei(en)${extractedCount > 0 ? `, ${extractedCount} entpackt` : ""}`);
} else if (pkg.status === "failed") {
this.notifyPackageOutcome(pkg, "failed", `${failed} von ${success + failed + cancelled} Datei(en) fehlgeschlagen`);
}
this.emitState(); this.emitState();
if (pkg.status === "completed" || (pkg.status === "failed" && success > 0)) { if (pkg.status === "completed" || (pkg.status === "failed" && success > 0)) {
@ -12073,6 +12120,14 @@ export class DownloadManager extends EventEmitter {
averageSpeedBps: avgSpeed averageSpeedBps: avgSpeed
}; };
this.session.summaryText = `Summary: Dauer ${duration}s, Ø Speed ${humanSize(avgSpeed)}/s, Erfolg ${success}/${total}`; this.session.summaryText = `Summary: Dauer ${duration}s, Ø Speed ${humanSize(avgSpeed)}/s, Erfolg ${success}/${total}`;
if (this.settings.notifyOnRunFinished && total > 0) {
void sendNotification(this.settings.notifyUrl, {
title: "Durchlauf beendet",
message: `${success}/${total} erfolgreich, ${failed} fehlgeschlagen, ${cancelled} abgebrochen\nDauer ${duration}s, Durchschnitt ${humanSize(avgSpeed)}/s`,
priority: failed > 0 ? "high" : "default",
tags: failed > 0 ? "warning" : "checkered_flag"
});
}
this.runItemIds.clear(); this.runItemIds.clear();
this.runOutcomes.clear(); this.runOutcomes.clear();
if (this.packagePostProcessTasks.size === 0 && !this.hasAnyDeferredPostProcessPending()) { if (this.packagePostProcessTasks.size === 0 && !this.hasAnyDeferredPostProcessPending()) {

49
src/main/notify.ts Normal file
View File

@ -0,0 +1,49 @@
import { logger } from "./logger";
export interface NotifyPayload {
title: string;
message: string;
priority?: "default" | "high";
tags?: string;
}
const NOTIFY_TIMEOUT_MS = 5000;
export function isNotifyUrlValid(url: string): boolean {
return /^https?:\/\/\S+$/i.test(String(url || "").trim());
}
export function buildNotifyRequest(url: string, payload: NotifyPayload): { url: string; init: RequestInit } {
const headers: Record<string, string> = {
"Title": payload.title,
"Content-Type": "text/plain; charset=utf-8"
};
if (payload.priority && payload.priority !== "default") {
headers["Priority"] = payload.priority;
}
if (payload.tags) {
headers["Tags"] = payload.tags;
}
return {
url: String(url || "").trim(),
init: { method: "POST", headers, body: payload.message }
};
}
export async function sendNotification(url: string, payload: NotifyPayload, fetchFn: typeof fetch = fetch): Promise<boolean> {
if (!isNotifyUrlValid(url)) {
return false;
}
try {
const request = buildNotifyRequest(url, payload);
const response = await fetchFn(request.url, { ...request.init, signal: AbortSignal.timeout(NOTIFY_TIMEOUT_MS) });
if (!response.ok) {
logger.warn(`Benachrichtigung fehlgeschlagen (HTTP ${response.status}): ${payload.title}`);
return false;
}
return true;
} catch (error) {
logger.warn(`Benachrichtigung fehlgeschlagen: ${String(error)}`);
return false;
}
}

View File

@ -459,6 +459,10 @@ export function normalizeSettings(settings: AppSettings): AppSettings {
hideExtractedItems: settings.hideExtractedItems !== undefined ? Boolean(settings.hideExtractedItems) : defaults.hideExtractedItems, hideExtractedItems: settings.hideExtractedItems !== undefined ? Boolean(settings.hideExtractedItems) : defaults.hideExtractedItems,
confirmDeleteSelection: settings.confirmDeleteSelection !== undefined ? Boolean(settings.confirmDeleteSelection) : defaults.confirmDeleteSelection, confirmDeleteSelection: settings.confirmDeleteSelection !== undefined ? Boolean(settings.confirmDeleteSelection) : defaults.confirmDeleteSelection,
backupIncludeDownloads: settings.backupIncludeDownloads !== undefined ? Boolean(settings.backupIncludeDownloads) : defaults.backupIncludeDownloads, backupIncludeDownloads: settings.backupIncludeDownloads !== undefined ? Boolean(settings.backupIncludeDownloads) : defaults.backupIncludeDownloads,
notifyUrl: asText(settings.notifyUrl) || defaults.notifyUrl,
notifyOnPackageCompleted: settings.notifyOnPackageCompleted !== undefined ? Boolean(settings.notifyOnPackageCompleted) : defaults.notifyOnPackageCompleted,
notifyOnPackageFailed: settings.notifyOnPackageFailed !== undefined ? Boolean(settings.notifyOnPackageFailed) : defaults.notifyOnPackageFailed,
notifyOnRunFinished: settings.notifyOnRunFinished !== undefined ? Boolean(settings.notifyOnRunFinished) : defaults.notifyOnRunFinished,
totalDownloadedAllTime: typeof settings.totalDownloadedAllTime === "number" && settings.totalDownloadedAllTime >= 0 ? settings.totalDownloadedAllTime : defaults.totalDownloadedAllTime, totalDownloadedAllTime: typeof settings.totalDownloadedAllTime === "number" && settings.totalDownloadedAllTime >= 0 ? settings.totalDownloadedAllTime : defaults.totalDownloadedAllTime,
totalCompletedFilesAllTime: typeof settings.totalCompletedFilesAllTime === "number" && settings.totalCompletedFilesAllTime >= 0 ? settings.totalCompletedFilesAllTime : defaults.totalCompletedFilesAllTime, totalCompletedFilesAllTime: typeof settings.totalCompletedFilesAllTime === "number" && settings.totalCompletedFilesAllTime >= 0 ? settings.totalCompletedFilesAllTime : defaults.totalCompletedFilesAllTime,
totalRuntimeAllTimeMs: typeof settings.totalRuntimeAllTimeMs === "number" && settings.totalRuntimeAllTimeMs >= 0 ? settings.totalRuntimeAllTimeMs : defaults.totalRuntimeAllTimeMs, totalRuntimeAllTimeMs: typeof settings.totalRuntimeAllTimeMs === "number" && settings.totalRuntimeAllTimeMs >= 0 ? settings.totalRuntimeAllTimeMs : defaults.totalRuntimeAllTimeMs,

View File

@ -853,6 +853,7 @@ const emptySnapshot = (): UiSnapshot => ({
maxParallel: 4, maxParallelExtract: 2, extractCpuPriority: "high", retryLimit: 0, speedLimitEnabled: false, speedLimitKbps: 0, speedLimitMode: "global", maxParallel: 4, maxParallelExtract: 2, extractCpuPriority: "high", retryLimit: 0, speedLimitEnabled: false, speedLimitKbps: 0, speedLimitMode: "global",
updateRepo: "", autoUpdateCheck: true, clipboardWatch: false, minimizeToTray: false, updateRepo: "", autoUpdateCheck: true, clipboardWatch: false, minimizeToTray: false,
theme: "dark", collapseNewPackages: true, historyRetentionMode: "permanent", autoSortPackagesByProgress: true, autoSkipExtracted: false, hideExtractedItems: true, confirmDeleteSelection: true, backupIncludeDownloads: false, theme: "dark", collapseNewPackages: true, historyRetentionMode: "permanent", autoSortPackagesByProgress: true, autoSkipExtracted: false, hideExtractedItems: true, confirmDeleteSelection: true, backupIncludeDownloads: false,
notifyUrl: "", notifyOnPackageCompleted: false, notifyOnPackageFailed: false, notifyOnRunFinished: false,
accountListShowDetailedDebridLinkKeys: false, accountListShowDetailedDebridLinkKeys: false,
bandwidthSchedules: [], totalDownloadedAllTime: 0, totalCompletedFilesAllTime: 0, totalRuntimeAllTimeMs: 0, bandwidthSchedules: [], totalDownloadedAllTime: 0, totalCompletedFilesAllTime: 0, totalRuntimeAllTimeMs: 0,
columnOrder: ["name", "size", "progress", "hoster", "account", "prio", "status", "speed"], columnOrder: ["name", "size", "progress", "hoster", "account", "prio", "status", "speed"],
@ -4956,6 +4957,12 @@ export function App(): ReactElement {
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.minimizeToTray} onChange={(e) => setBool("minimizeToTray", e.target.checked)} /> In System Tray minimieren</label> <label className="toggle-line"><input type="checkbox" checked={settingsDraft.minimizeToTray} onChange={(e) => setBool("minimizeToTray", e.target.checked)} /> In System Tray minimieren</label>
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.confirmDeleteSelection} onChange={(e) => setBool("confirmDeleteSelection", e.target.checked)} /> Vor dem Löschen bestätigen</label> <label className="toggle-line"><input type="checkbox" checked={settingsDraft.confirmDeleteSelection} onChange={(e) => setBool("confirmDeleteSelection", e.target.checked)} /> Vor dem Löschen bestätigen</label>
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.backupIncludeDownloads} onChange={(e) => setBool("backupIncludeDownloads", e.target.checked)} /> Download-Liste in Sicherung mitsichern (Standard: nur Einstellungen)</label> <label className="toggle-line"><input type="checkbox" checked={settingsDraft.backupIncludeDownloads} onChange={(e) => setBool("backupIncludeDownloads", e.target.checked)} /> Download-Liste in Sicherung mitsichern (Standard: nur Einstellungen)</label>
<label>Benachrichtigungs-URL (ntfy/Webhook)</label>
<input value={settingsDraft.notifyUrl} placeholder="https://ntfy.sh/mein-topic" onChange={(e) => setText("notifyUrl", e.target.value)} />
<div className="hint">POST an diese URL bei den unten gewählten Ereignissen. Mit der ntfy-App aufs Handy: Topic-URL eintragen, Topic in der App abonnieren kein Account nötig.</div>
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.notifyOnPackageCompleted} onChange={(e) => setBool("notifyOnPackageCompleted", e.target.checked)} /> Benachrichtigen wenn ein Paket fertig ist</label>
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.notifyOnPackageFailed} onChange={(e) => setBool("notifyOnPackageFailed", e.target.checked)} /> Benachrichtigen wenn ein Paket fehlschlägt</label>
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.notifyOnRunFinished} onChange={(e) => setBool("notifyOnRunFinished", e.target.checked)} /> Benachrichtigen wenn der Durchlauf beendet ist</label>
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.theme === "light"} onChange={(e) => { <label className="toggle-line"><input type="checkbox" checked={settingsDraft.theme === "light"} onChange={(e) => {
const next = e.target.checked ? "light" : "dark"; const next = e.target.checked ? "light" : "dark";
settingsDraftRevisionRef.current += 1; settingsDraftRevisionRef.current += 1;

View File

@ -132,6 +132,10 @@ export interface AppSettings {
hideExtractedItems: boolean; hideExtractedItems: boolean;
confirmDeleteSelection: boolean; confirmDeleteSelection: boolean;
backupIncludeDownloads: boolean; backupIncludeDownloads: boolean;
notifyUrl: string;
notifyOnPackageCompleted: boolean;
notifyOnPackageFailed: boolean;
notifyOnRunFinished: boolean;
totalDownloadedAllTime: number; totalDownloadedAllTime: number;
totalCompletedFilesAllTime: number; totalCompletedFilesAllTime: number;
totalRuntimeAllTimeMs: number; totalRuntimeAllTimeMs: number;

53
tests/notify.test.ts Normal file
View File

@ -0,0 +1,53 @@
import { describe, expect, it, vi } from "vitest";
import { buildNotifyRequest, isNotifyUrlValid, sendNotification } from "../src/main/notify";
describe("isNotifyUrlValid", () => {
it("accepts http/https URLs", () => {
expect(isNotifyUrlValid("https://ntfy.sh/mein-topic")).toBe(true);
expect(isNotifyUrlValid("http://192.168.1.10:8080/hook")).toBe(true);
expect(isNotifyUrlValid(" https://ntfy.sh/topic ")).toBe(true);
});
it("rejects empty and non-http values", () => {
expect(isNotifyUrlValid("")).toBe(false);
expect(isNotifyUrlValid("ntfy.sh/topic")).toBe(false);
expect(isNotifyUrlValid("ftp://x")).toBe(false);
expect(isNotifyUrlValid("https:// mit leerzeichen")).toBe(false);
});
});
describe("buildNotifyRequest", () => {
it("builds an ntfy-style POST with title/priority/tags headers and message body", () => {
const req = buildNotifyRequest(" https://ntfy.sh/topic ", { title: "Paket fertig", message: "Show.S01\n5 Datei(en)", priority: "high", tags: "x" });
expect(req.url).toBe("https://ntfy.sh/topic");
expect(req.init.method).toBe("POST");
expect(req.init.body).toBe("Show.S01\n5 Datei(en)");
expect(req.init.headers).toMatchObject({ Title: "Paket fertig", Priority: "high", Tags: "x" });
});
it("omits default priority and empty tags", () => {
const req = buildNotifyRequest("https://ntfy.sh/topic", { title: "T", message: "M", priority: "default" });
expect(req.init.headers).not.toHaveProperty("Priority");
expect(req.init.headers).not.toHaveProperty("Tags");
});
});
describe("sendNotification", () => {
it("returns true on HTTP ok", async () => {
const fetchFn = vi.fn().mockResolvedValue(new Response("", { status: 200 }));
await expect(sendNotification("https://ntfy.sh/topic", { title: "T", message: "M" }, fetchFn)).resolves.toBe(true);
expect(fetchFn).toHaveBeenCalledTimes(1);
});
it("returns false on HTTP error without throwing", async () => {
const fetchFn = vi.fn().mockResolvedValue(new Response("", { status: 500 }));
await expect(sendNotification("https://ntfy.sh/topic", { title: "T", message: "M" }, fetchFn)).resolves.toBe(false);
});
it("returns false on network error without throwing", async () => {
const fetchFn = vi.fn().mockRejectedValue(new Error("offline"));
await expect(sendNotification("https://ntfy.sh/topic", { title: "T", message: "M" }, fetchFn)).resolves.toBe(false);
});
it("does not call fetch for an invalid URL", async () => {
const fetchFn = vi.fn();
await expect(sendNotification("", { title: "T", message: "M" }, fetchFn)).resolves.toBe(false);
await expect(sendNotification("kein-url", { title: "T", message: "M" }, fetchFn)).resolves.toBe(false);
expect(fetchFn).not.toHaveBeenCalled();
});
});