fix(notifications): harden retries and shutdown

This commit is contained in:
Sucukdeluxe
2026-08-22 04:32:53 +02:00
parent 8b2320f771
commit c7e48891bb
5 changed files with 276 additions and 28 deletions
+2 -2
View File
@@ -1287,7 +1287,7 @@ export class AppController {
return this.manager.getItemLogPath(itemId) || getItemLogPath(itemId);
}
public shutdown(): void {
public async shutdown(): Promise<void> {
if (this.runtimeStatsTimer) {
clearInterval(this.runtimeStatsTimer);
this.runtimeStatsTimer = null;
@@ -1295,7 +1295,7 @@ export class AppController {
stopDebugServer();
abortActiveUpdateDownload();
cancelPendingAsyncSaves();
void this.notificationOutbox.drainForShutdown().catch((error) => {
await this.notificationOutbox.drainForShutdown(3000).catch((error) => {
logger.warn(`Notification-Outbox konnte beim Beenden nicht geleert werden: ${String(error)}`);
});
this.manager.prepareForShutdown();
+51 -12
View File
@@ -92,7 +92,42 @@ let scheduledStartTimer: ReturnType<typeof setTimeout> | null = null;
let lastClipboardText = "";
let controller: AppController;
let pendingBackupImport: Buffer | null = null;
const CLIPBOARD_MAX_TEXT_CHARS = 50_000;
const CLIPBOARD_MAX_TEXT_CHARS = 50_000;
export interface BeforeQuitHandlerOptions {
cleanup: () => void;
shutdown: () => Promise<void>;
continueQuit: () => void;
onError: (error: unknown) => void;
}
export function createBeforeQuitHandler(options: BeforeQuitHandlerOptions): (event: { preventDefault: () => void }) => void {
let shutdownStarted = false;
let quitAllowed = false;
return (event) => {
if (quitAllowed) {
return;
}
event.preventDefault();
if (shutdownStarted) {
return;
}
shutdownStarted = true;
let shutdown: Promise<void>;
try {
options.cleanup();
shutdown = options.shutdown();
} catch (error) {
shutdown = Promise.reject(error);
}
void shutdown.catch((error) => {
options.onError(error);
}).finally(() => {
quitAllowed = true;
options.continueQuit();
});
};
}
function isDevMode(): boolean {
return process.env.NODE_ENV === "development";
@@ -1038,16 +1073,20 @@ app.on("window-all-closed", () => {
}
});
app.on("before-quit", () => {
if (updateQuitTimer) { clearTimeout(updateQuitTimer); updateQuitTimer = null; }
stopClipboardWatcher();
destroyTray();
shutdownDaemon();
if (controller) {
try {
controller.shutdown();
} catch (error) {
logger.error(`Fehler beim Shutdown: ${String(error)}`);
app.on("before-quit", createBeforeQuitHandler({
cleanup: () => {
if (updateQuitTimer) { clearTimeout(updateQuitTimer); updateQuitTimer = null; }
stopClipboardWatcher();
destroyTray();
shutdownDaemon();
},
shutdown: async () => {
if (controller) {
await controller.shutdown();
}
},
continueQuit: () => app.quit(),
onError: (error) => {
logger.error(`Fehler beim Shutdown: ${String(error)}`);
}
});
}));
+19 -10
View File
@@ -132,7 +132,7 @@ function oldestIndex(events: NotificationEvent[], predicate: (event: Notificatio
}
function retryDelayMs(attempts: number): number {
return Math.min(MAX_RETRY_DELAY_MS, 1000 * (2 ** Math.min(9, Math.max(0, attempts - 1))));
return Math.min(MAX_RETRY_DELAY_MS, 1000 * (2 ** Math.min(30, Math.max(0, attempts - 1))));
}
export class NotificationOutbox {
@@ -153,6 +153,9 @@ export class NotificationOutbox {
this.clock = options.now || Date.now;
this.autoDrain = Boolean(options.autoDrain);
this.load();
if (this.autoDrain && this.events.length > 0) {
this.scheduleDrain(Math.max(0, this.events[0].nextAttemptAt - this.clock()));
}
}
public async enqueue(event: NotificationEvent): Promise<void> {
@@ -170,11 +173,15 @@ export class NotificationOutbox {
public drain(now?: number): Promise<void> {
return this.runExclusive(async () => {
const drainAt = finiteInteger(now ?? this.clock());
this.enforceLimits(drainAt);
let currentNow = finiteInteger(now ?? this.clock());
this.enforceLimits(currentNow);
while (this.events.length > 0) {
const current = this.events[0];
if (current.nextAttemptAt > drainAt) {
if (current.nextAttemptAt > currentNow) {
await this.persist(currentNow);
if (this.autoDrain) {
this.scheduleDrain(Math.max(0, current.nextAttemptAt - this.clock()));
}
break;
}
let sent = false;
@@ -183,23 +190,25 @@ export class NotificationOutbox {
} catch {
sent = false;
}
const outcomeAt = finiteInteger(this.clock(), currentNow);
if (!sent) {
current.attempts += 1;
current.nextAttemptAt = drainAt + retryDelayMs(current.attempts);
this.lastFailureAt = drainAt;
await this.persist(drainAt);
current.nextAttemptAt = outcomeAt + retryDelayMs(current.attempts);
this.lastFailureAt = outcomeAt;
await this.persist(outcomeAt);
if (this.autoDrain) {
this.scheduleDrain(Math.max(0, current.nextAttemptAt - this.clock()));
}
break;
}
this.events.shift();
this.lastSuccessAt = drainAt;
await this.persist(drainAt);
this.lastSuccessAt = outcomeAt;
await this.persist(outcomeAt);
currentNow = finiteInteger(this.clock(), outcomeAt);
}
if (this.events.length === 0) {
this.clearRetryTimer();
await this.persist(drainAt);
await this.persist(finiteInteger(this.clock(), currentNow));
}
});
}