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:
@@ -56,6 +56,7 @@ import { cleanupArchives, clearExtractResumeState, collectArchiveCleanupTargets,
|
||||
import { validateFileAgainstManifest } from "./integrity";
|
||||
import { classifyDiskError } from "./fs-error";
|
||||
import { processVideoFile, resolveVideoTooling, stripDualLangMarker, hasDualLangMarker, isRemuxableVideoFile, type GermanAudioMode, type VideoProcessResult } from "./video-processor";
|
||||
import { sendNotification } from "./notify";
|
||||
import { logger } from "./logger";
|
||||
import { getRecentRotationEvents, runWithRotationItemSink, setRotationEventListener } from "./account-rotation-log";
|
||||
import type { RotationEvent } from "../shared/types";
|
||||
@@ -1750,6 +1751,8 @@ export class DownloadManager extends EventEmitter {
|
||||
|
||||
private historyRecordedPackages = new Set<string>();
|
||||
|
||||
private notifiedPackages = new Set<string>();
|
||||
|
||||
private itemCount = 0;
|
||||
|
||||
private lastSchedulerHeartbeatAt = 0;
|
||||
@@ -2788,6 +2791,7 @@ export class DownloadManager extends EventEmitter {
|
||||
this.runOutcomes.clear();
|
||||
this.runCompletedPackages.clear();
|
||||
this.historyRecordedPackages.clear();
|
||||
this.notifiedPackages.clear();
|
||||
this.retryAfterByItem.clear();
|
||||
this.providerStartReservations.clear();
|
||||
this.pacedStartReservationByItem.clear();
|
||||
@@ -5109,6 +5113,7 @@ export class DownloadManager extends EventEmitter {
|
||||
pkg.enabled = true;
|
||||
pkg.updatedAt = nowMs();
|
||||
this.historyRecordedPackages.delete(packageId);
|
||||
this.notifiedPackages.delete(packageId);
|
||||
|
||||
if (this.session.running) {
|
||||
for (const itemId of itemIds) {
|
||||
@@ -5176,6 +5181,7 @@ export class DownloadManager extends EventEmitter {
|
||||
this.abortPackagePostProcessing(pkgId, "reset");
|
||||
this.runCompletedPackages.delete(pkgId);
|
||||
this.historyRecordedPackages.delete(pkgId);
|
||||
this.notifiedPackages.delete(pkgId);
|
||||
|
||||
const pkg = this.session.packages[pkgId];
|
||||
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.notifiedPackages.delete(packageId);
|
||||
this.abortPackagePostProcessing(packageId, "package_removed");
|
||||
for (const itemId of itemIds) {
|
||||
this.retryAfterByItem.delete(itemId);
|
||||
@@ -10468,6 +10475,34 @@ export class DownloadManager extends EventEmitter {
|
||||
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 {
|
||||
let pending = 0;
|
||||
let success = 0;
|
||||
@@ -10501,6 +10536,7 @@ export class DownloadManager extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
const prevStatus = pkg.status;
|
||||
if (failed > 0) {
|
||||
pkg.status = "failed";
|
||||
} else if (cancelled > 0) {
|
||||
@@ -10509,6 +10545,11 @@ export class DownloadManager extends EventEmitter {
|
||||
pkg.status = "completed";
|
||||
}
|
||||
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;
|
||||
@@ -11731,6 +11772,12 @@ export class DownloadManager extends EventEmitter {
|
||||
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();
|
||||
|
||||
if (pkg.status === "completed" || (pkg.status === "failed" && success > 0)) {
|
||||
@@ -12073,6 +12120,14 @@ export class DownloadManager extends EventEmitter {
|
||||
averageSpeedBps: avgSpeed
|
||||
};
|
||||
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.runOutcomes.clear();
|
||||
if (this.packagePostProcessTasks.size === 0 && !this.hasAnyDeferredPostProcessPending()) {
|
||||
|
||||
Reference in New Issue
Block a user