feat: unify BPM health and guard session recovery with state machines

This commit is contained in:
阿白叔 committed 2026-10-24 14:49:15 +08:00
1 parent 97e06328c3
commit 42d19c707d
23 files changed
+681 -90

No files matched your search

+10 -4
View File
@@ -1,6 +1,7 @@
'use strict';
const fs=require('node:fs'),path=require('node:path'),{DatabaseSync}=require('node:sqlite'),{readSupervisor}=require('../../platform/service/control.cjs'),{redact}=require('../../shared/redact.cjs');
const {monitorEventLoopDelay}=require('node:perf_hooks');
const {connectionSnapshot}=require('../bpm/connection-status.cjs');
function timestamp(value){if(typeof value!=='string')return null;const normalized=/^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$/.test(value)?value.replace(' ','T')+'Z':value;const n=Date.parse(normalized);return Number.isFinite(n)?new Date(n).toISOString():null;}
const latest=(...values)=>values.map(timestamp).filter(Boolean).sort().at(-1)||null;
const groupStatus=items=>!items.length?'unconfigured':['error','unknown','running','pending','healthy','paused','disabled'].find(s=>items.some(i=>i.status===s))||'pending';
@@ -23,11 +24,16 @@ class RuntimeHealth {
}
collect(){
const groups=[],safe=(key,title,link,read)=>{try{const items=read();groups.push({key,title,link,status:groupStatus(items),items});}catch{groups.push({key,title,link,status:'unknown',items:[],message:'暂时无法读取此模块状态,请到对应页面核实。'});}};
let connections=[];
let connections=[],connectionViews=[];
safe('bpm','BPM 连接','#bpm',()=>{connections=this.sources.workspace?.snapshot().connections||[];return connections.map(c=>{
const checkedAt=timestamp(c.checkedAt),status=c.status==='disabled'?'disabled':c.status==='connected'?'healthy':['error','login_required'].includes(c.status)?'error':'pending';
return {id:c.id,name:redact(c.name||'BPM 连接'),owner:c.owner,status,checkedAt,detail:status==='error'?'上次验证失败或需要重新登录。':status==='healthy'?'上次已验证登录;连接由后台托管检查。':'尚未验证或已停用。',...this.remember('bpm',c.id,{successAt:latest(c.lastConnectedAt,status==='healthy'?checkedAt:null),failureAt:status==='error'?checkedAt:null})};
const config=()=>this.sources.monitor?.scanner?.store?.config?.()||this.sources.scanner?.store.status().config||{};
const snapshot=connectionSnapshot(c,{manager:this.sources.bpmSessions,monitor:this.sources.monitor,config,now:this.clock()});connectionViews.push(snapshot);
const account=snapshot.connectionState.account,checkedAt=timestamp(account.checkedAt),status=account.status;
return {id:c.id,name:redact(c.name||'BPM 连接'),owner:c.owner,status,checkedAt,reason:account.reason,detail:account.message,...this.remember('bpm',c.id,{successAt:latest(c.lastConnectedAt,status==='healthy'?checkedAt:null),failureAt:['AUTH_REQUIRED','SESSION_UNAVAILABLE'].includes(account.reason)?checkedAt:null})};
});});
safe('bpm-sync','BPM 同步','#bpm',()=>connectionViews.map(c=>{
const sync=c.connectionState.sync;return {id:c.id,name:redact(c.name||'BPM 连接'),owner:c.owner,status:sync.status,reason:sync.reason,checkedAt:timestamp(sync.checkedAt),detail:sync.message};
}));
safe('scan','BPM 扫描','#records',()=>{
const {config={},runs=[]}=this.sources.scanner?.store.status()||{};
for(const r of runs)this.remember('scan',r.connectionId+':'+(r.kind||'inform'),{successAt:r.status==='success'?r.finishedAt:null,failureAt:['error','interrupted'].includes(r.status)?r.finishedAt:null});
@@ -63,7 +69,7 @@ class RuntimeHealth {
return groups;
}
snapshot(user){
const now=this.clock(),groups=this.collect().map(g=>{if(!['bpm','scan','listener','handoff','execution'].includes(g.key)||user.role==='admin'||g.status==='unknown')return g;const items=g.items.filter(i=>i.global||i.owner===user.username);return {...g,items,status:groupStatus(items)};});
const now=this.clock(),groups=this.collect().map(g=>{if(!['bpm','bpm-sync','scan','listener','handoff','execution'].includes(g.key)||user.role==='admin'||g.status==='unknown')return g;const items=g.items.filter(i=>i.global||i.owner===user.username);return {...g,items,status:groupStatus(items)};});
let saved;try{saved=readSupervisor(this.stateDir);}catch{saved={state:null,events:[],autostart:null};}
const s=saved.state,managed=Boolean(this.instanceId&&s?.childInstanceId===this.instanceId),fresh=managed&&now-Date.parse(s.updatedAt)<45000;
const guard=managed?{status:fresh?s.status:'stale',heartbeatAt:s.heartbeatAt,updatedAt:s.updatedAt,restartCount:s.restartCount,childStartedAt:s.childStartedAt,reason:s.reason,nextRestartAt:s.nextRestartAt,events:saved.events.map(e=>({at:e.at,kind:e.kind,code:e.detail.code,delayMs:e.detail.delayMs,reason:e.detail.reason}))}:{status:'unmanaged',restartCount:0,events:[]};