u
This commit is contained in:
@@ -215,7 +215,12 @@ const activeAgentEvents = new Map();
|
|||||||
export function notifyAgentJob(agentId, action, job) {
|
export function notifyAgentJob(agentId, action, job) {
|
||||||
const aId = Number(agentId);
|
const aId = Number(agentId);
|
||||||
const conns = activeAgentEvents.get(aId);
|
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({
|
const payload = JSON.stringify({
|
||||||
action,
|
action,
|
||||||
jobId: job.id,
|
jobId: job.id,
|
||||||
@@ -228,6 +233,7 @@ export function notifyAgentJob(agentId, action, job) {
|
|||||||
if (!clientRes.writableEnded) {
|
if (!clientRes.writableEnded) {
|
||||||
clientRes.write(`event: job\ndata: ${payload}\n\n`);
|
clientRes.write(`event: job\ndata: ${payload}\n\n`);
|
||||||
clientRes.write(`event: printerjob\ndata: ${payload}\n\n`);
|
clientRes.write(`event: printerjob\ndata: ${payload}\n\n`);
|
||||||
|
clientRes.flush?.();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -269,7 +275,7 @@ export function notifyAgentJob(agentId, action, job) {
|
|||||||
agentRouter.get('/events', agentAuthMiddleware, async (req, res) => {
|
agentRouter.get('/events', agentAuthMiddleware, async (req, res) => {
|
||||||
const version = extractAgentVersion(req);
|
const version = extractAgentVersion(req);
|
||||||
if (version) {
|
if (version) {
|
||||||
db.touchLastseen(req.agent.id, version).catch(() => {});
|
db.touchLastseen(req.agent.id, version).catch(() => { });
|
||||||
}
|
}
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'text/event-stream');
|
res.setHeader('Content-Type', 'text/event-stream');
|
||||||
@@ -298,7 +304,7 @@ agentRouter.get('/events', agentAuthMiddleware, async (req, res) => {
|
|||||||
const keepAlive = setInterval(() => {
|
const keepAlive = setInterval(() => {
|
||||||
if (res.writableEnded) return;
|
if (res.writableEnded) return;
|
||||||
res.write(': heartbeat\n\n');
|
res.write(': heartbeat\n\n');
|
||||||
db.touchLastseen(req.agent.id).catch(() => {});
|
db.touchLastseen(req.agent.id).catch(() => { });
|
||||||
}, 15000);
|
}, 15000);
|
||||||
|
|
||||||
let closed = false;
|
let closed = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user