fix: harden automation reconciliation and pause lifecycle

Keep full folder scans recoverable under duplicate protection and classify every discovered file once before automatic admission.

Separate manual previews from automatic capacity limits, serialize renderer intake, normalize automation counters and intervals, and make Main authoritative for runtime timestamps.

Enforce persistent pause across startup, import, close recovery, resume failures, and reconciliation while preserving read-only paused scans and exactly one activation reconciliation.

Restore failed asynchronous rotation chunks, complete automation localization and unlimited queue accessibility, and tighten hidden integration cleanup coverage.
This commit is contained in:
Sucukdeluxe
2026-08-26 15:47:31 +02:00
parent ca2d1f8fda
commit afd3b70cf3
12 changed files with 812 additions and 157 deletions
+42 -9
View File
@@ -3,6 +3,7 @@ const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar');
const { walkFolderAsync } = require('./file-discovery');
const { normalizeAutomationSettings } = require('./automation-control');
class FolderMonitor extends EventEmitter {
constructor({
@@ -44,6 +45,9 @@ class FolderMonitor extends EventEmitter {
this._lastScanAt = null;
this._lastScanTrigger = '';
this._lastError = '';
this._startedAt = null;
this._nextReconcileAt = null;
this._reconcileIntervalMs = 5 * 60 * 1000;
this._generation = 0;
}
@@ -55,6 +59,24 @@ class FolderMonitor extends EventEmitter {
return this._start(settings, false);
}
configure(settings) {
const folderPath = String(settings?.folderPath || '').trim();
if (!folderPath) throw new Error('Kein Ordnerpfad angegeben');
const reconcileIntervalMinutes = normalizeAutomationSettings(settings).reconcileIntervalMinutes;
const watcher = this._invalidateLifecycle(true);
if (watcher) {
try {
Promise.resolve(watcher.close()).catch(() => {});
} catch {}
}
this._seenFiles = new Set();
this._settings = { ...settings, folderPath, reconcileIntervalMinutes };
this._reachable = null;
this._lastError = '';
this._emitStatus(this._generation);
return { includesExisting: false, paused: true };
}
stop() {
this._deactivate({ clearSeen: true, paused: false });
}
@@ -67,7 +89,9 @@ class FolderMonitor extends EventEmitter {
scanning: this._scanning,
folderPath: this._settings ? this._settings.folderPath : '',
seenCount: this._seenFiles.size,
startedAt: this._startedAt,
lastScanAt: this._lastScanAt,
nextReconcileAt: this._nextReconcileAt,
lastScanTrigger: this._lastScanTrigger,
error: this._lastError
});
@@ -106,19 +130,22 @@ class FolderMonitor extends EventEmitter {
if (changed) this._emitStatus(this._generation);
}
async resume(settings = this._settings) {
async resume(settings = this._settings, options = {}) {
if (!settings) throw new Error('Keine Ordnerkonfiguration vorhanden');
this._start(settings, true);
if (options.reconcile === false) return { reconciled: false };
return this.scan({ emitFiles: true, trigger: 'resume' });
}
_start(settings, preserveSeen) {
this._deactivate({ clearSeen: !preserveSeen, paused: false });
this._settings = settings;
const reconcileIntervalMinutes = normalizeAutomationSettings(settings).reconcileIntervalMinutes;
this._settings = { ...settings, reconcileIntervalMinutes };
this._paused = false;
this._reachable = null;
this._lastError = '';
settings = this._settings;
const folderPath = String(settings.folderPath || '').trim();
if (!folderPath) throw new Error('Kein Ordnerpfad angegeben');
@@ -158,10 +185,12 @@ class FolderMonitor extends EventEmitter {
this._emitStatus(generation);
this._emitEvent('error', [error], generation);
});
const intervalMinutes = Number(settings.reconcileIntervalMinutes) || 5;
this._reconcileIntervalMs = reconcileIntervalMinutes * 60 * 1000;
this._startedAt = this._now();
this._nextReconcileAt = this._startedAt + this._reconcileIntervalMs;
this._reconcileTimer = this._setInterval(
() => this._reconcile(generation).catch((error) => this._publishBackgroundError(error, generation)),
intervalMinutes * 60 * 1000
this._reconcileIntervalMs
);
this._emitStatus(generation);
return { includesExisting: includeInitial };
@@ -194,6 +223,8 @@ class FolderMonitor extends EventEmitter {
}
this._batchSeenReservations.clear();
this._paused = paused;
this._startedAt = null;
this._nextReconcileAt = null;
this._scanning = false;
this._followUpRequested = false;
this._followUpOptions = null;
@@ -280,9 +311,8 @@ class FolderMonitor extends EventEmitter {
try {
const files = await this._discoverFiles(settings, generation);
if (!this._isCurrent(generation)) return this._cancelledResult(trigger);
const emittedFiles = files.filter((file) => this._acceptPath(file.path));
if (emittedFiles.length > 0) {
const listenerError = this._emitEvent('new-files', [emittedFiles.map((file) => file.path)], generation);
if (files.length > 0) {
const listenerError = this._emitEvent('new-files', [files], generation);
if (listenerError) return this._finishProductiveError(listenerError, generation, trigger, true);
}
if (!this._isCurrent(generation)) return this._cancelledResult(trigger);
@@ -325,7 +355,7 @@ class FolderMonitor extends EventEmitter {
for (const descriptor of discovered) {
if (!this._isCurrent(generation)) return [];
if (!settings.recursive && this._isNestedPath(descriptor.path, settings.folderPath)) continue;
if (!this._classifyPath(descriptor.path, settings).allowed) continue;
const classification = this._classifyPath(descriptor.path, settings);
let mtimeMs = 0;
try {
mtimeMs = Number((await this._stat(descriptor.path)).mtimeMs) || 0;
@@ -335,7 +365,9 @@ class FolderMonitor extends EventEmitter {
path: descriptor.path,
name: descriptor.name || path.basename(descriptor.path),
size: Number(descriptor.size) || 0,
mtimeMs
mtimeMs,
filterMatched: classification.allowed,
filterReason: classification.reason
}));
}
return files;
@@ -353,6 +385,7 @@ class FolderMonitor extends EventEmitter {
async _reconcile(generation) {
if (!this._acceptCallback(generation)) return;
this._nextReconcileAt = this._now() + this._reconcileIntervalMs;
const trigger = this._reachable === false ? 'reconnect' : 'interval';
await this.scan({ emitFiles: true, trigger });
}