Spaces:
Paused
Paused
| import { checkUptime, saveUptimeCheck, getAllMonitors } from './uptime.server'; | |
| // Store active intervals for each monitor | |
| const activeIntervals = new Map<string, NodeJS.Timeout>(); | |
| let isInitialized = false; | |
| export async function startCronJob() { | |
| if (isInitialized) { | |
| console.log('Cron job already initialized'); | |
| return; | |
| } | |
| isInitialized = true; | |
| console.log('Initializing cron job system...'); | |
| // Initial load of all monitors | |
| await loadAndScheduleMonitors(); | |
| // Periodically check for new or updated monitors every minute | |
| setInterval(async () => { | |
| await loadAndScheduleMonitors(); | |
| }, 60000); | |
| console.log('Cron job system started'); | |
| } | |
| async function loadAndScheduleMonitors() { | |
| try { | |
| const monitors = await getAllMonitors(); | |
| // Get current monitor IDs from database | |
| const currentMonitorIds = new Set(monitors.map(m => m.id)); | |
| // Remove intervals for monitors that no longer exist or have been updated | |
| for (const [monitorId, interval] of activeIntervals.entries()) { | |
| const monitor = monitors.find(m => m.id === monitorId); | |
| if (!monitor) { | |
| // Monitor was deleted | |
| clearInterval(interval); | |
| activeIntervals.delete(monitorId); | |
| console.log(`Stopped monitoring deleted monitor: ${monitorId}`); | |
| } else { | |
| // Check if interval changed | |
| const currentInterval = activeIntervals.get(monitorId); | |
| if (currentInterval) { | |
| // For simplicity, we restart the interval if monitor still exists | |
| // In production, you might want to check if interval actually changed | |
| clearInterval(currentInterval); | |
| activeIntervals.delete(monitorId); | |
| } | |
| } | |
| } | |
| // Schedule monitors | |
| for (const monitor of monitors) { | |
| if (!activeIntervals.has(monitor.id)) { | |
| scheduleMonitor(monitor.id, monitor.url, monitor.interval); | |
| } | |
| } | |
| } catch (error) { | |
| console.error('Error loading monitors:', error); | |
| } | |
| } | |
| function scheduleMonitor(monitorId: string, url: string, intervalSeconds: number) { | |
| // Ensure minimum interval of 10 seconds | |
| const safeInterval = Math.max(intervalSeconds, 10); | |
| const intervalMs = safeInterval * 1000; | |
| // Immediate first check | |
| checkMonitor(monitorId, url); | |
| // Schedule recurring checks | |
| const interval = setInterval(async () => { | |
| await checkMonitor(monitorId, url); | |
| }, intervalMs); | |
| activeIntervals.set(monitorId, interval); | |
| console.log(`Scheduled monitor ${monitorId} (${url}) every ${safeInterval} seconds`); | |
| } | |
| async function checkMonitor(monitorId: string, url: string) { | |
| try { | |
| const result = await checkUptime(url); | |
| await saveUptimeCheck(monitorId, result); | |
| console.log(`[${new Date().toISOString()}] Checked ${url}: ${result.status} (${result.responseTime}ms)`); | |
| } catch (error) { | |
| console.error(`Error checking ${url}:`, error); | |
| } | |
| } | |
| export function stopCronJob() { | |
| // Clear all intervals | |
| for (const [monitorId, interval] of activeIntervals.entries()) { | |
| clearInterval(interval); | |
| console.log(`Stopped monitoring: ${monitorId}`); | |
| } | |
| activeIntervals.clear(); | |
| isInitialized = false; | |
| console.log('Cron job stopped'); | |
| } | |
| export async function refreshMonitors() { | |
| console.log('Refreshing monitors...'); | |
| await loadAndScheduleMonitors(); | |
| } | |