feat: add verified offline recovery and retire legacy send assembly
This commit is contained in:
1 parent
84e2f0d9ba
commit
1148aa6f14
19 files changed
+235
-24
No files matched your search
+15
-8
@@ -21,8 +21,7 @@ const { ZcodeRuntime } = require('../modules/assistant/zcode-runtime.cjs');
|
||||
const { RuntimeHealth } = require('../modules/health/runtime-health.cjs');
|
||||
const { StorageStore } = require('../modules/storage/store.cjs');
|
||||
const { StorageService } = require('../modules/storage/service.cjs');
|
||||
const { NotificationStore } = require('../modules/notifications/store.cjs');
|
||||
const { NotificationService } = require('../modules/notifications/service.cjs');
|
||||
const { NotificationHistory } = require('../modules/notifications/history.cjs');
|
||||
const { RuleStore } = require('../modules/order-rules/store.cjs');
|
||||
const { OrderRuleDispatcher } = require('../modules/order-rules/dispatcher.cjs');
|
||||
const { BpmOrderProvider } = require('../modules/bpm/order-provider.cjs');
|
||||
@@ -35,6 +34,13 @@ async function closeServer(server){
|
||||
if(!server.listening){await server.drainExports?.();server.closeSessionStore?.();return;}
|
||||
await new Promise((resolve,reject)=>server.close(error=>error?reject(error):resolve()));
|
||||
}
|
||||
async function resolveOutputDir(root,stateDir,config){
|
||||
if(Object.hasOwn(config,'outputDir'))return path.resolve(root,config.outputDir||'output/pdf');
|
||||
let restored;try{restored=JSON.parse(await fs.readFile(path.join(stateDir,'restore-config.json'),'utf8'));}catch(e){if(e.code==='ENOENT')return path.resolve(root,'output/pdf');throw e;}
|
||||
if(restored.outputDir!=='restored-pdf')throw Error('恢复输出目录必须位于新状态目录内。');
|
||||
const resolved=path.resolve(stateDir,restored.outputDir),relative=path.relative(path.resolve(stateDir),resolved);if(!relative||relative.startsWith('..')||path.isAbsolute(relative))throw Error('恢复输出目录必须位于新状态目录内。');
|
||||
const [realState,realOutput]=await Promise.all([fs.realpath(stateDir),fs.realpath(resolved)]),realRelative=path.relative(realState,realOutput);if(!realRelative||path.isAbsolute(realRelative)||realRelative==='..'||realRelative.startsWith('..'+path.sep)||(await fs.lstat(resolved)).isSymbolicLink())throw Error('恢复输出目录不能是链接。');return resolved;
|
||||
}
|
||||
async function main(options = {}) {
|
||||
const startedAt=new Date().toISOString(),watchdog={token:process.env.ORDER_SUPERVISOR_TOKEN,instanceId:process.env.ORDER_SUPERVISOR_INSTANCE,startedAt};
|
||||
const root = require('../shared/paths.cjs').PROJECT_ROOT;
|
||||
@@ -64,7 +70,7 @@ async function main(options = {}) {
|
||||
let token;
|
||||
try { token = (await fs.readFile(tokenPath,'utf8')).trim(); } catch(error) { if(error.code !== 'ENOENT') throw error; token=crypto.randomBytes(32).toString('hex'); await fs.writeFile(tokenPath,token,{mode:0o600}); }
|
||||
if (!/^[a-f0-9]{64}$/.test(token)) throw new Error('本地配对文件损坏,请删除 pairing.key 后重启。');
|
||||
const outputDir = path.resolve(root,config.outputDir || 'output/pdf');
|
||||
const outputDir = await resolveOutputDir(root,stateDir,config);
|
||||
const installedExtension = path.join(stateDir,'extension');
|
||||
const dashboardConfig=config.dashboard || {};
|
||||
const dashboardPort=dashboardConfig.port ?? 18766;
|
||||
@@ -76,7 +82,7 @@ async function main(options = {}) {
|
||||
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(()=>bpmMonitor?.stop(),()=>health?.close(),()=>assistant?.stop(),()=>orderDispatcher?.stop(),()=>orderExecution?.stop(),()=>notifications?.stop(),()=>storage?.stop(),()=>scanner?.stop(),()=>mail?.stop(),()=>custodian?.stop(),()=>bpmSessions?.shutdown());
|
||||
cleanup.push(()=>bpmMonitor?.stop(),()=>health?.close(),()=>assistant?.stop(),()=>orderDispatcher?.stop(),()=>orderExecution?.stop(),()=>storage?.stop(),()=>scanner?.stop(),()=>mail?.stop(),()=>custodian?.stop(),()=>bpmSessions?.shutdown());
|
||||
cleanup.push(()=>closeServer(dashboard),()=>workspace.close());
|
||||
cleanup.push(()=>mailStore?.close(),()=>scanStore?.close(),()=>storageStore?.close(),()=>notificationStore?.close(),()=>orderRules?.close(),()=>assistantStore?.close());
|
||||
bpmSessions=new BpmSessionManager({stateDir:path.join(stateDir,'bpm-sessions'),accessClient:new BpmAccessClient()});
|
||||
@@ -85,14 +91,15 @@ async function main(options = {}) {
|
||||
mailStore.migrateLegacy(store.settings().templates);
|
||||
scanStore=new BpmScanStore(path.join(stateDir,'bpm-scan'));scanner=new BpmScanner({store:scanStore,workspace,manager:bpmSessions,settings:()=>store.settings()});
|
||||
storageStore=new StorageStore(path.join(stateDir,'storage'));storage=new StorageService({store:storageStore,jobs:store,outputDir});
|
||||
notificationStore=new NotificationStore(path.join(stateDir,'notifications'));notificationStore.recover();notifications=new NotificationService({store:notificationStore,mail,settings:()=>store.settings(),calendar:()=>workspace.meta('calendar').overrides});
|
||||
notificationStore=new NotificationHistory(path.join(stateDir,'notifications'));notifications={store:notificationStore,historyOnly:true};
|
||||
orderRules=new RuleStore(path.join(stateDir,'order-rules'),{schedule:()=>store.settings().schedule,calendar:()=>workspace.meta('calendar').overrides});
|
||||
orderProvider=new BpmOrderProvider({manager:bpmSessions});orderArchive=new OrderArchiveProvider({storage,outputDir,journal:orderRules});
|
||||
orderExecution=new OrderExecutionCoordinator({store:orderRules,workspace,provider:orderProvider,archive:orderArchive,mail,dashboard:store,settings:()=>store.settings(),calendar:()=>workspace.meta('calendar').overrides,systemPrincipal:custodian.principal});
|
||||
orderDispatcher=new OrderRuleDispatcher({store:orderRules,mail,settings:()=>store.settings(),calendar:()=>workspace.meta('calendar').overrides,providersReady:()=>{const cap=orderExecution.snapshot().capabilities;return ['liveTodo','pdfExport','automaticArchive'].every(key=>cap?.[key]===true);},templateResolver:(categoryId,rule)=>require('../modules/mail/rule-template.cjs').ruleTemplate(mailStore,categoryId,rule),attachmentReader:file=>orderArchive.read(file)});
|
||||
bpmMonitor=new BpmArrivalMonitor({workspace,manager:bpmSessions,scanner,provider:orderProvider,settings:()=>store.settings()});
|
||||
orderRules.deliveryBlockers=allowed=>scanStore.deliveries.pendingSources().filter(allowed);
|
||||
bpmMonitor=new BpmArrivalMonitor({workspace,manager:bpmSessions,scanner,provider:orderProvider,settings:()=>store.settings(),onTodoReconciled:payload=>orderExecution.notifyDiscovery(payload)});
|
||||
assistantStore=new AssistantStore(path.join(stateDir,'assistant'));assistantStore.recover();assistant=new AssistantService({store:assistantStore,runtime:new ZcodeRuntime({stateDir:path.join(stateDir,'assistant','runtime')}),dashboard:store,workspace,scanner,storage,orderRules,orderExecution});
|
||||
health=new RuntimeHealth({stateDir,startedAt,instanceId:watchdog.instanceId,workspace,scanner,storage,mail,execution:orderExecution});
|
||||
health=new RuntimeHealth({stateDir,startedAt,instanceId:watchdog.instanceId,workspace,scanner,storage,mail,execution:orderExecution,monitor:bpmMonitor});
|
||||
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,renderSnapshot:options.renderSnapshot});
|
||||
@@ -110,4 +117,4 @@ async function main(options = {}) {
|
||||
return {server,dashboard,workspace,bpmSessions,bpmMonitor,custodian,mail,scanner,storage,notifications,orderRules,orderDispatcher,orderExecution,assistant,health,stop};
|
||||
}catch(error){try{await stop();}catch(cleanupError){error.cleanupError=cleanupError;}throw error;}
|
||||
}
|
||||
module.exports={main};
|
||||
module.exports={main,resolveOutputDir};
|
||||
Reference in new issue
Block a user