Make host-agent /status answer from cache so MyMonitor can see it.

The overlay polls in 500ms; probing Comfy ports and WMI on every request timed out and kept the A light red while the agent was running.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Towsty
2026-09-01 07:21:38 -05:00
co-authored by Cursor
parent b7c4f01f5e
commit 9dcb3a50bc
+20 -8
View File
@@ -16,6 +16,8 @@ const idleMs = Math.max(60_000, Number(process.env.COMFY_IDLE_MS || 30 * 60 * 10
let lastWorkAt = Date.now() let lastWorkAt = Date.now()
let lastQueueRunning = 0 let lastQueueRunning = 0
let stoppedByAgent = false let stoppedByAgent = false
let lastHealthyPort = 0
let lastProcessUp = false
function markWork() { function markWork() {
lastWorkAt = Date.now() lastWorkAt = Date.now()
@@ -138,7 +140,11 @@ function ensureProxy(targetPort) {
async function syncProxy() { async function syncProxy() {
const healthyPort = await findHealthyPort() const healthyPort = await findHealthyPort()
if (healthyPort) ensureProxy(healthyPort) lastHealthyPort = healthyPort || 0
if (healthyPort) {
ensureProxy(healthyPort)
lastProcessUp = true
}
return healthyPort return healthyPort
} }
@@ -581,15 +587,16 @@ const server = http.createServer(async (req, res) => {
if (!authorized(req)) return json(res, 401, { ok: false, error: 'unauthorized' }) if (!authorized(req)) return json(res, 401, { ok: false, error: 'unauthorized' })
const url = new URL(req.url || '/', 'http://localhost') const url = new URL(req.url || '/', 'http://localhost')
if (req.method === 'GET' && url.pathname === '/status') { if (req.method === 'GET' && url.pathname === '/status') {
const [healthyPort, process] = await Promise.all([syncProxy(), processUp()]) // Answer immediately. MyMonitor allows ~500ms; probing ports + WMI here
if (healthyPort) await noteQueue(healthyPort) // made the A light stay red even while this process was running.
const processUpNow = Boolean(process || healthyPort) const healthyPort = lastHealthyPort || proxyTarget
const processUpNow = Boolean(lastProcessUp || healthyPort)
return json(res, 200, { return json(res, 200, {
ok: true, ok: true,
http: Boolean(healthyPort), http: Boolean(healthyPort),
process: processUpNow, process: processUpNow,
processUp: processUpNow, processUp: processUpNow,
asleep: Boolean(stoppedByAgent || (!healthyPort && !process)), asleep: Boolean(stoppedByAgent || (!healthyPort && !processUpNow)),
lastActivityAt: new Date(lastWorkAt).toISOString(), lastActivityAt: new Date(lastWorkAt).toISOString(),
idleMs, idleMs,
port: healthyPort || null, port: healthyPort || null,
@@ -623,6 +630,8 @@ const server = http.createServer(async (req, res) => {
const killed = await stopComfyProcesses() const killed = await stopComfyProcesses()
stoppedByAgent = true stoppedByAgent = true
lastQueueRunning = 0 lastQueueRunning = 0
lastHealthyPort = 0
lastProcessUp = false
console.log(JSON.stringify({ src: 'comfy-host-agent', event: 'stopped', reason: 'stop', killed })) console.log(JSON.stringify({ src: 'comfy-host-agent', event: 'stopped', reason: 'stop', killed }))
return json(res, 200, { ok: true, stopped: true, asleep: true, killed }) return json(res, 200, { ok: true, stopped: true, asleep: true, killed })
} }
@@ -634,24 +643,27 @@ const server = http.createServer(async (req, res) => {
}) })
server.listen(port, '0.0.0.0', async () => { server.listen(port, '0.0.0.0', async () => {
const healthyPort = await syncProxy() lastHealthyPort = await syncProxy() || 0
if (healthyPort) await noteQueue(healthyPort) lastProcessUp = Boolean(lastHealthyPort) || await processUp().catch(() => false)
if (lastHealthyPort) await noteQueue(lastHealthyPort)
console.log(JSON.stringify({ console.log(JSON.stringify({
src: 'comfy-host-agent', src: 'comfy-host-agent',
event: 'listen', event: 'listen',
port, port,
proxyPort, proxyPort,
comfyPort: healthyPort || null, comfyPort: lastHealthyPort || null,
idleMs, idleMs,
candidates: candidatePorts() candidates: candidatePorts()
})) }))
let lastIdleCheck = 0 let lastIdleCheck = 0
setInterval(() => { setInterval(() => {
syncProxy().then(async (nextPort) => { syncProxy().then(async (nextPort) => {
lastHealthyPort = nextPort || 0
const now = Date.now() const now = Date.now()
if (now - lastIdleCheck < 10_000) return if (now - lastIdleCheck < 10_000) return
lastIdleCheck = now lastIdleCheck = now
await maybeIdleStop(nextPort) await maybeIdleStop(nextPort)
lastProcessUp = Boolean(nextPort) || await processUp().catch(() => false)
}).catch(() => {}) }).catch(() => {})
}, 3000) }, 3000)
}) })