Make GPU wake return before Coolify times out, and skip hung Comfy ports.

The site poke hit /start while the agent waited on a dead 8188 probe, so wake looked down even with the host agent running.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Towsty
2026-09-01 07:26:21 -05:00
co-authored by Cursor
parent 9dcb3a50bc
commit 29184961c4
+16 -11
View File
@@ -68,9 +68,9 @@ function candidatePorts() {
return ordered return ordered
} }
async function probeStats(portNum) { async function probeStats(portNum, timeoutMs = 800) {
try { try {
const res = await fetch(`http://127.0.0.1:${portNum}/system_stats`, { signal: AbortSignal.timeout(2500) }) const res = await fetch(`http://127.0.0.1:${portNum}/system_stats`, { signal: AbortSignal.timeout(timeoutMs) })
if (!res.ok) return null if (!res.ok) return null
const stats = await res.json() const stats = await res.json()
if (!stats?.system) return null if (!stats?.system) return null
@@ -91,12 +91,11 @@ function advertisedPort(stats) {
} }
async function findHealthyPort() { async function findHealthyPort() {
const found = [] const probed = await Promise.all(candidatePorts().map(async (next) => {
for (const next of candidatePorts()) {
const stats = await probeStats(next) const stats = await probeStats(next)
if (!stats) continue return stats ? { port: next, advertised: advertisedPort(stats) } : null
found.push({ port: next, advertised: advertisedPort(stats) }) }))
} const found = probed.filter(Boolean)
const native = found.find(item => item.advertised && item.advertised === item.port) const native = found.find(item => item.advertised && item.advertised === item.port)
if (native) return native.port if (native) return native.port
for (const item of found) { for (const item of found) {
@@ -242,11 +241,13 @@ function desktopShellCommand() {
async function pythonMainUp() { async function pythonMainUp() {
try { try {
const { stdout } = await execFileAsync('powershell.exe', [ const { stdout } = await execFileAsync('tasklist', ['/FO', 'CSV', '/NH'], { windowsHide: true, timeout: 2000 })
if (!/python(?:w)?\.exe/i.test(stdout)) return false
const { stdout: verbose } = await execFileAsync('powershell.exe', [
'-NoProfile', '-Command', '-NoProfile', '-Command',
"Get-CimInstance Win32_Process | Where-Object { $_.CommandLine -match 'ComfyUI\\\\main.py|ComfyUI/main.py' } | Select-Object -First 1 -ExpandProperty ProcessId" "Get-CimInstance Win32_Process | Where-Object { $_.CommandLine -match 'ComfyUI\\\\main.py|ComfyUI/main.py' } | Select-Object -First 1 -ExpandProperty ProcessId"
], { windowsHide: true, timeout: 6000 }) ], { windowsHide: true, timeout: 2500 })
return Boolean(String(stdout).trim()) return Boolean(String(verbose).trim())
} catch { } catch {
return false return false
} }
@@ -270,7 +271,7 @@ async function pickListenPort() {
for (const next of ordered) { for (const next of ordered) {
if (!next || seen.has(next)) continue if (!next || seen.has(next)) continue
seen.add(next) seen.add(next)
if (await probeStats(next)) return next if (await probeStats(next, 400)) return next
if (!(await portInUse(next))) return next if (!(await portInUse(next))) return next
} }
return preferred return preferred
@@ -614,6 +615,10 @@ const server = http.createServer(async (req, res) => {
} }
await taskkillImage('Comfy Desktop.exe') await taskkillImage('Comfy Desktop.exe')
const launched = await startComfy() const launched = await startComfy()
if (launched.port) {
lastProcessUp = true
ensureProxy(launched.port)
}
return json(res, 200, { ok: true, asleep: false, ...launched }) return json(res, 200, { ok: true, asleep: false, ...launched })
} }
if (req.method === 'POST' && url.pathname === '/stop') { if (req.method === 'POST' && url.pathname === '/stop') {