import { Router, Request, Response } from "express"; import axios from "axios"; import { cronJobLogStorage } from "../storage/cron-job-log-storage"; const router = Router(); const SELENIUM_BASE_URL = process.env.SELENIUM_AGENT_BASE_URL || "http://localhost:5002"; // GET /api/job-monitor/summary // Returns last run per cron job + recent history router.get("/summary", async (_req: Request, res: Response) => { try { const [lastRuns, recentLogs] = await Promise.all([ cronJobLogStorage.getLastRunPerJob(), cronJobLogStorage.getRecentLogs(30), ]); res.json({ lastRuns, recentLogs }); } catch (err) { console.error("job-monitor/summary error:", err); res.status(500).json({ error: "Failed to fetch cron job summary" }); } }); // GET /api/job-monitor/failed // Returns recent failed job logs router.get("/failed", async (_req: Request, res: Response) => { try { const failed = await cronJobLogStorage.getFailedLogs(20); res.json(failed); } catch (err) { console.error("job-monitor/failed error:", err); res.status(500).json({ error: "Failed to fetch failed jobs" }); } }); // GET /api/job-monitor/selenium-status // Proxies the Selenium service /status endpoint router.get("/selenium-status", async (_req: Request, res: Response) => { try { const response = await axios.get(`${SELENIUM_BASE_URL}/status`, { timeout: 4000, }); res.json({ online: true, ...response.data }); } catch (err) { // Service may be offline — return gracefully res.json({ online: false, active_jobs: 0, queued_jobs: 0, status: "offline" }); } }); export default router;