diff --git a/src/api/agent/index.js b/src/api/agent/index.js index 98e789c..42f1147 100644 --- a/src/api/agent/index.js +++ b/src/api/agent/index.js @@ -215,7 +215,12 @@ const activeAgentEvents = new Map(); export function notifyAgentJob(agentId, action, job) { const aId = Number(agentId); const conns = activeAgentEvents.get(aId); - if (!conns || conns.size === 0) return false; + const count = conns ? conns.size : 0; + log.info(`notifyAgentJob: agentId=${aId} action=${action} jobId=${job?.id} activeConnections=${count}`); + if (!conns || conns.size === 0) { + log.warn(`notifyAgentJob: No active SSE connection for agent ${aId} (jobId=${job?.id}); agent must poll/reconnect`); + return false; + } const payload = JSON.stringify({ action, jobId: job.id, @@ -228,6 +233,7 @@ export function notifyAgentJob(agentId, action, job) { if (!clientRes.writableEnded) { clientRes.write(`event: job\ndata: ${payload}\n\n`); clientRes.write(`event: printerjob\ndata: ${payload}\n\n`); + clientRes.flush?.(); } } return true; @@ -269,7 +275,7 @@ export function notifyAgentJob(agentId, action, job) { agentRouter.get('/events', agentAuthMiddleware, async (req, res) => { const version = extractAgentVersion(req); if (version) { - db.touchLastseen(req.agent.id, version).catch(() => {}); + db.touchLastseen(req.agent.id, version).catch(() => { }); } res.setHeader('Content-Type', 'text/event-stream'); @@ -298,7 +304,7 @@ agentRouter.get('/events', agentAuthMiddleware, async (req, res) => { const keepAlive = setInterval(() => { if (res.writableEnded) return; res.write(': heartbeat\n\n'); - db.touchLastseen(req.agent.id).catch(() => {}); + db.touchLastseen(req.agent.id).catch(() => { }); }, 15000); let closed = false;