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:
@@ -16,6 +16,8 @@ const idleMs = Math.max(60_000, Number(process.env.COMFY_IDLE_MS || 30 * 60 * 10
|
||||
let lastWorkAt = Date.now()
|
||||
let lastQueueRunning = 0
|
||||
let stoppedByAgent = false
|
||||
let lastHealthyPort = 0
|
||||
let lastProcessUp = false
|
||||
|
||||
function markWork() {
|
||||
lastWorkAt = Date.now()
|
||||
@@ -138,7 +140,11 @@ function ensureProxy(targetPort) {
|
||||
|
||||
async function syncProxy() {
|
||||
const healthyPort = await findHealthyPort()
|
||||
if (healthyPort) ensureProxy(healthyPort)
|
||||
lastHealthyPort = healthyPort || 0
|
||||
if (healthyPort) {
|
||||
ensureProxy(healthyPort)
|
||||
lastProcessUp = true
|
||||
}
|
||||
return healthyPort
|
||||
}
|
||||
|
||||
@@ -581,15 +587,16 @@ const server = http.createServer(async (req, res) => {
|
||||
if (!authorized(req)) return json(res, 401, { ok: false, error: 'unauthorized' })
|
||||
const url = new URL(req.url || '/', 'http://localhost')
|
||||
if (req.method === 'GET' && url.pathname === '/status') {
|
||||
const [healthyPort, process] = await Promise.all([syncProxy(), processUp()])
|
||||
if (healthyPort) await noteQueue(healthyPort)
|
||||
const processUpNow = Boolean(process || healthyPort)
|
||||
// Answer immediately. MyMonitor allows ~500ms; probing ports + WMI here
|
||||
// made the A light stay red even while this process was running.
|
||||
const healthyPort = lastHealthyPort || proxyTarget
|
||||
const processUpNow = Boolean(lastProcessUp || healthyPort)
|
||||
return json(res, 200, {
|
||||
ok: true,
|
||||
http: Boolean(healthyPort),
|
||||
process: processUpNow,
|
||||
processUp: processUpNow,
|
||||
asleep: Boolean(stoppedByAgent || (!healthyPort && !process)),
|
||||
asleep: Boolean(stoppedByAgent || (!healthyPort && !processUpNow)),
|
||||
lastActivityAt: new Date(lastWorkAt).toISOString(),
|
||||
idleMs,
|
||||
port: healthyPort || null,
|
||||
@@ -623,6 +630,8 @@ const server = http.createServer(async (req, res) => {
|
||||
const killed = await stopComfyProcesses()
|
||||
stoppedByAgent = true
|
||||
lastQueueRunning = 0
|
||||
lastHealthyPort = 0
|
||||
lastProcessUp = false
|
||||
console.log(JSON.stringify({ src: 'comfy-host-agent', event: 'stopped', reason: 'stop', 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 () => {
|
||||
const healthyPort = await syncProxy()
|
||||
if (healthyPort) await noteQueue(healthyPort)
|
||||
lastHealthyPort = await syncProxy() || 0
|
||||
lastProcessUp = Boolean(lastHealthyPort) || await processUp().catch(() => false)
|
||||
if (lastHealthyPort) await noteQueue(lastHealthyPort)
|
||||
console.log(JSON.stringify({
|
||||
src: 'comfy-host-agent',
|
||||
event: 'listen',
|
||||
port,
|
||||
proxyPort,
|
||||
comfyPort: healthyPort || null,
|
||||
comfyPort: lastHealthyPort || null,
|
||||
idleMs,
|
||||
candidates: candidatePorts()
|
||||
}))
|
||||
let lastIdleCheck = 0
|
||||
setInterval(() => {
|
||||
syncProxy().then(async (nextPort) => {
|
||||
lastHealthyPort = nextPort || 0
|
||||
const now = Date.now()
|
||||
if (now - lastIdleCheck < 10_000) return
|
||||
lastIdleCheck = now
|
||||
await maybeIdleStop(nextPort)
|
||||
lastProcessUp = Boolean(nextPort) || await processUp().catch(() => false)
|
||||
}).catch(() => {})
|
||||
}, 3000)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user