Prevent queue loss during app updates

- Increase quit timeout from 900ms to 5000ms to ensure pending saves complete
- Add persistNowSync() called before update install to flush queue to disk
- Remove blockAllPersistence from shutdown save condition — shutdown must
  always persist to prevent data loss across restarts
- Add temp file recovery as last resort when both primary and backup
  session files are corrupted

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Sucukdeluxe 2026-03-26 19:34:48 +01:00
parent ffb48a8883
commit 5aeab9ecad
4 changed files with 606 additions and 570 deletions

View File

@ -354,6 +354,9 @@ export class AppController {
if (this.manager.isSessionRunning()) {
this.manager.stop();
}
// Flush any pending async saves BEFORE the update process starts.
// This ensures the queue is fully persisted to disk so it survives the restart.
this.manager.persistNowSync();
const cacheAgeMs = Date.now() - this.lastUpdateCheckAt;
const cached = this.lastUpdateCheck && !this.lastUpdateCheck.error && cacheAgeMs <= 10 * 60 * 1000

View File

@ -4696,9 +4696,12 @@ export class DownloadManager extends EventEmitter {
this.pacedStartReservationByItem.clear();
this.nonResumableActive = 0;
this.session.summaryText = "";
// Persist synchronously on shutdown to guarantee data is written before process exits
// Skip if a backup was just imported — the restored session on disk must not be overwritten
if (!this.skipShutdownPersist && !this.blockAllPersistence) {
// Persist synchronously on shutdown to guarantee data is written before process exits.
// Only skip if a backup was just imported (skipShutdownPersist) — the restored session
// on disk must not be overwritten. blockAllPersistence is intentionally NOT checked
// here: it guards async/periodic saves during runtime, but shutdown must always persist
// to prevent queue loss across restarts/updates.
if (!this.skipShutdownPersist) {
const pkgCount = Object.keys(this.session.packages).length;
const itemCount = Object.keys(this.session.items).length;
logger.info(`Shutdown-Save: ${pkgCount} Pakete, ${itemCount} Items`);
@ -5030,6 +5033,18 @@ export class DownloadManager extends EventEmitter {
}
}
/** Synchronous persist guarantees state is on disk before returning.
* Used before update installs to prevent queue loss. */
public persistNowSync(): void {
this.clearPersistTimer();
const pkgCount = Object.keys(this.session.packages).length;
const itemCount = Object.keys(this.session.items).length;
logger.info(`Pre-Update Sync-Save: ${pkgCount} Pakete, ${itemCount} Items`);
this.foldRuntimeIntoSettings(nowMs());
saveSession(this.storagePaths, this.session);
saveSettings(this.storagePaths, this.settings);
}
private emitState(force = false): void {
const now = nowMs();
const MIN_FORCE_GAP_MS = 120;

View File

@ -257,7 +257,7 @@ function registerIpcHandlers(): void {
if (result.started) {
updateQuitTimer = setTimeout(() => {
app.quit();
}, 900);
}, 5000);
}
return result;
});

View File

@ -881,7 +881,25 @@ export function loadSession(paths: StoragePaths): SessionState {
return backup;
}
logger.error("Session konnte nicht geladen werden (auch Backup fehlgeschlagen)");
// Last resort: try to recover from temp files left by interrupted writes
for (const kind of ["sync", "async"] as const) {
const tmpPath = sessionTempPath(paths.sessionFile, kind);
if (fs.existsSync(tmpPath)) {
const tmpSession = readSessionFile(tmpPath);
if (tmpSession && Object.keys(tmpSession.packages).length > 0) {
logger.warn(`Session aus temporaerer Datei wiederhergestellt: ${tmpPath} (${Object.keys(tmpSession.packages).length} Pakete)`);
try {
const payload = JSON.stringify({ ...tmpSession, updatedAt: Date.now() });
fs.writeFileSync(paths.sessionFile, payload, "utf8");
} catch {
// ignore restore write failure
}
return tmpSession;
}
}
}
logger.error("Session konnte nicht geladen werden (Primary, Backup und Temp-Dateien fehlgeschlagen)");
return emptySession();
}