fix: drain accepted exports and close failed store initialization

This commit is contained in:
abaishu0730 committed 2026-10-19 09:11:40 +08:00
1 parent 54e5bafd86
commit a420d2d9fb
12 files changed
+204 -71

No files matched your search

+5 -3
View File
@@ -25,7 +25,7 @@ const { NotificationService } = require('../modules/notifications/service.cjs');
async function closeServer(server){
if(!server)return;
// An unbound dashboard still owns its session DB.
if(!server.listening){server.closeSessionStore?.();return;}
if(!server.listening){await server.drainExports?.();server.closeSessionStore?.();return;}
await new Promise((resolve,reject)=>server.close(error=>error?reject(error):resolve()));
}
async function main(options = {}) {
@@ -43,6 +43,7 @@ async function main(options = {}) {
const cleanup=[];
let stopping;
const stop=()=>stopping ||= (async()=>{
server?.stopAcceptingExports?.();
const errors=[];
// Stop producers before closing stores. Every teardown gets an attempt, even after a failure.
for(const close of cleanup)try{await close();}catch(error){errors.push(error);}
@@ -67,8 +68,9 @@ async function main(options = {}) {
store=new Store(path.join(stateDir,'dashboard'));
workspace=new WorkspaceStore(path.join(stateDir,'workspace'));
// Register conditional closures before constructing later resources; partial startup follows the same path.
cleanup.push(()=>closeServer(server));
cleanup.push(()=>health?.close(),()=>assistant?.stop(),()=>notifications?.stop(),()=>storage?.stop(),()=>scanner?.stop(),()=>mail?.stop(),()=>custodian?.stop(),()=>bpmSessions?.shutdown());
cleanup.push(()=>closeServer(dashboard),()=>closeServer(server),()=>workspace.close());
cleanup.push(()=>closeServer(dashboard),()=>workspace.close());
cleanup.push(()=>mailStore?.close(),()=>scanStore?.close(),()=>storageStore?.close(),()=>notificationStore?.close(),()=>assistantStore?.close());
bpmSessions=new BpmSessionManager({stateDir:path.join(stateDir,'bpm-sessions')});
custodian=new BpmCustodian({workspace,manager:bpmSessions});
@@ -81,7 +83,7 @@ async function main(options = {}) {
health=new RuntimeHealth({stateDir,startedAt,instanceId:watchdog.instanceId,workspace,scanner,storage,mail});
await bpmSessions.migrateAccounts(workspace.snapshot().connections);
workspace.markSessionChecksPending();
server=await startService({token,port,outputDir,scale:config.scale,installedExtension,extensionSourceDir:path.join(root,'extension'),store,storage,watchdog});
server=await startService({token,port,outputDir,scale:config.scale,installedExtension,extensionSourceDir:path.join(root,'extension'),store,storage,watchdog,renderSnapshot:options.renderSnapshot});
dashboard=createDashboard({store,workspace,outputDir,bpmSessions,mail,scanner,storage,notifications,assistant,health,watchdog,trustedProxy:dashboardConfig.trustedProxy===true});
await new Promise((resolve,reject)=>{dashboard.once('error',reject);dashboard.listen(dashboardPort,dashboardHost,resolve);});store.recoverJobs();mailStore.recoverSends();custodian.start();mail.start();scanner.start();notifications.start();
health.start();