Expose a monitor endpoint for MyMonitor status and job progress.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { checkComfyHttp, fetchQueue, fetchComfyControlStatus } from '~/server/utils/comfyLifecycle'
|
||||
import { comfyConfigured } from '~/server/utils/comfy'
|
||||
import { listJobs, type Job } from '~/server/utils/jobs'
|
||||
|
||||
const ACTIVE: Job['status'][] = ['queued', 'uploading', 'running']
|
||||
|
||||
function monitorToken() {
|
||||
return String(useRuntimeConfig().monitorToken || process.env.MONITOR_TOKEN || '').trim()
|
||||
}
|
||||
|
||||
function activeProgress(jobs: Job[]) {
|
||||
const active = jobs.filter(job => ACTIVE.includes(job.status))
|
||||
if (!active.length) {
|
||||
return { running: false, percent: 0 }
|
||||
}
|
||||
const percent = Math.max(0, Math.min(100, Math.round(Math.max(...active.map(job => Number(job.progress) || 0)))))
|
||||
return { running: true, percent }
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const expected = monitorToken()
|
||||
if (expected) {
|
||||
const given = String(getHeader(event, 'x-monitor-token') || getQuery(event).token || '').trim()
|
||||
if (given !== expected) {
|
||||
throw createError({ statusCode: 401, statusMessage: 'Monitor token required' })
|
||||
}
|
||||
}
|
||||
|
||||
setHeader(event, 'Cache-Control', 'no-store')
|
||||
|
||||
const videoConfigured = comfyConfigured()
|
||||
const [comfy, queue, control] = await Promise.all([
|
||||
videoConfigured
|
||||
? checkComfyHttp().catch(() => ({ ok: false }))
|
||||
: Promise.resolve({ ok: false }),
|
||||
videoConfigured ? fetchQueue() : Promise.resolve({ running: 0, pending: 0, ok: false }),
|
||||
fetchComfyControlStatus().catch(() => null)
|
||||
])
|
||||
const comfyReachable = Boolean(comfy.ok || queue.ok)
|
||||
const hostAgentOk = Boolean(control && control.ok !== false)
|
||||
const busy = Boolean(comfyReachable && queue.running > 0)
|
||||
const progress = activeProgress(listJobs())
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
instance: String(useRuntimeConfig().public.instanceName || 'aigen'),
|
||||
comfy: {
|
||||
ok: comfyReachable,
|
||||
busy,
|
||||
asleep: Boolean(control?.asleep) && !comfyReachable,
|
||||
processUp: Boolean(control?.process ?? control?.processUp)
|
||||
},
|
||||
hostAgent: {
|
||||
ok: hostAgentOk
|
||||
},
|
||||
progress
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user