(() => { 'use strict'; let api,esc,formatDate; const statuses={healthy:['上次正常','green'],error:['上次失败','red'],pending:['待检查','yellow'],disabled:['已停用','secondary'],paused:['已暂停','secondary'],running:['执行中','blue'],unknown:['状态未知','orange'],unconfigured:['未配置','secondary']}; const guards={running:['守护正常','green'],starting:['启动中','blue'],backoff:['等待恢复','orange'],cooldown:['恢复冷却中','orange'],stale:['守护心跳过期','red'],unmanaged:['未通过守护启动','yellow'],stopped:['已停止','secondary'],stopping:['停止中','secondary']}; const reasons={healthy:'两个服务端口心跳正常',initial_start:'首次启动',unexpected_exit:'业务进程意外退出',health_timeout:'服务持续无响应',manual_restart:'手动重启',manual_stop:'手动停止',port_conflict:'服务端口已被占用',spawn_failed:'进程启动失败',startup_failed:'启动失败'}; const events={guard_started:'守护启动',child_exit:'业务进程退出',unresponsive:'服务持续无响应',restarting:'准备自动恢复',cooldown:'多次失败,进入冷却',manual_restart:'手动重启',guard_stopped:'守护停止',monitor_error:'守护检查异常',spawn_failed:'业务进程启动失败',autostart_installed:'启用 Windows 登录自启',autostart_removed:'移除 Windows 登录自启'}; function duration(seconds){seconds=Math.max(0,Math.floor(seconds));const days=Math.floor(seconds/86400),hours=Math.floor(seconds%86400/3600),minutes=Math.floor(seconds%3600/60);return (days?days+' 天 ':'')+(hours?hours+' 小时 ':'')+(minutes?minutes+' 分钟 ':seconds%60+' 秒');} function badge(status,map=statuses){const [label,color]=map[status]||['状态未知','secondary'];return `${label}`;} async function render(container,isActive=()=>true){ const live=()=>isActive()&&container.isConnected;let pending=false,timer; container.innerHTML=`

服务守护与健康状态

上次检查结果,非实时连通性检测
`; const button=container.querySelector('#health-refresh'),notice=container.querySelector('#health-refresh-state'); function draw(data){ const {runtime}=data,guard=runtime.guard; container.querySelector('#health-runtime').innerHTML=`

工作台服务

当前可访问
${esc(duration(runtime.uptimeSeconds))}
本次启动:${esc(formatDate(runtime.startedAt))}
Windows 登录自启:${runtime.autostart.enabled?'已启用(当前用户)':'未启用'}

独立服务守护

${badge(guard.status,guards)}
累计恢复 / 重启:${Number(guard.restartCount)||0} 次
最近心跳:${esc(formatDate(guard.heartbeatAt))}
${esc(runtime.managed?(reasons[guard.reason]||'等待健康检查结果'):'当前服务直接启动,尚未由守护程序接管。')}
${guard.nextRestartAt?`
下次尝试:${esc(formatDate(guard.nextRestartAt))}
`:''}
`; container.querySelector('#health-groups').innerHTML=data.groups.map(group=>`

${esc(group.title)}

${badge(group.status)}
${group.message?`
${esc(group.message)}
`:group.items.length?group.items.map(item=>`
${esc(item.name)}${badge(item.status)}
${esc(item.detail)}
最近检查
${esc(formatDate(item.checkedAt))}
最近成功
${esc(formatDate(item.lastSuccessAt))}
最近失败
${esc(formatDate(item.lastFailureAt))}
`).join(''):'
尚无可查看的配置。
'}
`).join(''); container.querySelector('#health-events').innerHTML=`

最近守护事件

${guard.events?.length?guard.events.slice(0,12).map(event=>`
${esc(formatDate(event.at))}${esc(events[event.kind]||'运行状态变化')}${event.code!==undefined&&event.code!==null?'(退出码 '+esc(event.code)+')':''}${event.delayMs?','+Math.ceil(event.delayMs/1000)+' 秒后尝试':''}
`).join(''):'暂无守护事件。'}
`; } async function refresh(){ if(pending||!live())return;pending=true;button.disabled=true;clearTimeout(timer); try{const data=await api('/health',undefined,{signal:AbortSignal.timeout(8000)});if(!live())return;draw(data);notice.className='card-footer small text-secondary';notice.innerHTML=`健康状态已更新${esc(formatDate(data.at))}`;} catch{if(live()){const access=container.querySelector('#health-access');if(access){access.className='badge bg-red-lt ms-auto';access.textContent='连接中断';}notice.className='card-footer small text-danger';notice.textContent='暂时无法连接服务,保留上次状态;恢复后会自动更新。';}} finally{pending=false;if(live()){button.disabled=false;timer=setTimeout(refresh,10000);}} } button.onclick=refresh;await refresh(); } window.HealthViews={configure:options=>({api,esc,formatDate}=options),render}; })();