import { runNotificationSchedulerTick } from '../utils/notification-scheduler.js' import { info as loggerInfo, error as loggerError } from '../utils/logger.js' const INTERVAL_MS = 60_000 let timer = null let running = false async function tick(reason = 'interval') { if (running) return running = true try { const result = await runNotificationSchedulerTick() if (result?.dueUsers) { loggerInfo('[notification-scheduler] Tick', { reason, ...result }) } } catch (error) { loggerError('[notification-scheduler] Tick fehlgeschlagen:', { error }) } finally { running = false } } export default defineNitroPlugin((nitroApp) => { if (process.env.NOTIFICATION_SCHEDULER_DISABLED === 'true') { loggerInfo('[notification-scheduler] Deaktiviert') return } loggerInfo('[notification-scheduler] Gestartet') timer = setInterval(() => tick(), INTERVAL_MS) timer.unref?.() tick('start') nitroApp.hooks.hookOnce('close', () => { if (timer) clearInterval(timer) timer = null }) })