fix: keep background jobs alive

Add a background job orchestrator that restores profile jobs from saved credentials,
keeps pickup and store-watch schedules alive without UI logins, and exposes job
status via health/admin endpoints.
This commit is contained in:
2026-06-25 12:40:28 +02:00
parent 881b3d8d86
commit 02dfea8562
5 changed files with 308 additions and 6 deletions

View File

@@ -22,6 +22,7 @@ const { readStoreWatch, writeStoreWatch, listWatcherProfiles } = require('./serv
const { readPreferences, writePreferences, sanitizeLocation } = require('./services/userPreferencesStore');
const { readStoresCache, writeStoresCache } = require('./services/storeCacheStore');
const requestLogStore = require('./services/requestLogStore');
const backgroundJobOrchestrator = require('./services/backgroundJobOrchestrator');
const {
readJournal,
writeJournal,
@@ -211,6 +212,12 @@ async function buildRegularPickupMapForConfig(session, config) {
function scheduleWithCurrentSettings(sessionId, config) {
const settings = adminConfig.readSettings();
scheduleConfig(sessionId, config, settings);
const session = sessionStore.get(sessionId);
if (session?.profile?.id) {
backgroundJobOrchestrator.refreshProfile(session.profile.id, { config, settings }).catch((error) => {
console.error('[BACKGROUND] Profil-Jobs konnten nicht aktualisiert werden:', error.message);
});
}
}
function rescheduleAllSessions() {
@@ -223,6 +230,9 @@ function rescheduleAllSessions() {
scheduleConfig(session.id, config, settings);
});
scheduleRegularPickupRefresh(settings);
backgroundJobOrchestrator.refreshAll({ settings }).catch((error) => {
console.error('[BACKGROUND] Profil-Jobs konnten nicht neu geplant werden:', error.message);
});
}
function mergeStoresIntoConfig(config = [], stores = []) {
@@ -1538,6 +1548,10 @@ app.post('/api/admin/settings', requireAuth, requireAdmin, (req, res) => {
res.json(updated);
});
app.get('/api/admin/background-jobs', requireAuth, requireAdmin, (_req, res) => {
res.json(backgroundJobOrchestrator.getStatus());
});
app.get('/api/debug/requests', requireAuth, requireAdmin, (req, res) => {
const limit = req.query?.limit ? Number(req.query.limit) : undefined;
const logs = requestLogStore.list(limit);
@@ -1545,7 +1559,15 @@ app.get('/api/debug/requests', requireAuth, requireAdmin, (req, res) => {
});
app.get('/api/health', (_req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
const background = backgroundJobOrchestrator.getStatus();
res.json({
status: 'ok',
timestamp: new Date().toISOString(),
backgroundJobs: {
running: background.running,
profiles: background.profiles.length
}
});
});
app.get('*', (req, res) => {
@@ -1559,6 +1581,7 @@ async function startServer() {
console.error('[RESTORE] Fehler bei der Session-Wiederherstellung:', error.message);
}
scheduleRegularPickupRefresh(adminConfig.readSettings());
backgroundJobOrchestrator.start();
startBackgroundStoreRefreshTicker();
app.listen(port, () => {
console.log(`Server läuft auf Port ${port}`);