refactor: organize source web and tests by business module

This commit is contained in:
abaishu0730 committed 2026-10-19 08:45:51 +08:00
1 parent 9854d2d5c2
commit 2aed328774
113 files changed
+189 -183

No files matched your search

-28
View File
@@ -1,28 +0,0 @@
'use strict';
const crypto=require('node:crypto');const {dayPolicy}=require('./schedule.cjs');
const fail=(message,status)=>Object.assign(new Error(message),{status});
class BpmScanner{
constructor({store,workspace,manager,settings,clock=()=>new Date()}){Object.assign(this,{store,workspace,manager,settings,clock});this.pending=new Map();this.stopped=false;this.lastTick=0;this.principal={user:{username:'系统扫描',role:'admin'},sessionId:'scan:'+crypto.randomUUID()};}
start(){if(this.timer||this.stopped)return;this.timer=setInterval(()=>this.tick().catch(()=>{}),15000);this.timer.unref?.();this.tick().catch(()=>{});}
async scan(id,principal){return this.startScan(id,principal).promise;}
startScan(id,principal){
if(this.stopped)throw fail('扫描服务正在关闭。',503);const connection=this.workspace.getConnection(id);
if(!principal?.sessionId||!['admin','operator'].includes(principal.user?.role)||(principal.user.role!=='admin'&&connection.owner!==principal.user.username))throw fail('只有连接负责人或管理员可以扫描。',403);
if(connection.status==='disabled')throw fail('此 BPM 连接已停用。',409);
if(this.pending.has(id))throw fail('此连接正在扫描,请稍后查看结果。',409);
const runId=this.store.begin(id,principal.user.username);
const operation=Promise.resolve().then(async()=>{
try{const rows=await this.manager.scanArrivals(connection,principal);if(this.workspace.getConnection(id).status==='disabled')throw fail('此 BPM 连接已停用。',409);return {...this.store.complete(runId,id,rows),runId};}
catch(cause){this.store.fail(runId,cause.status?cause.message:'BPM 扫描失败,请稍后重试。');throw cause;}
}).finally(()=>this.pending.delete(id));this.pending.set(id,operation);return {promise:operation,runId};
}
tick(){if(this.stopped||this.ticking)return Promise.resolve();this.ticking=this.checkAll().finally(()=>{this.ticking=null;});return this.ticking;}
async checkAll(){
const config=this.store.config(),now=this.clock();if(!config.enabled||+now-this.lastTick<config.intervalMinutes*60000)return;
const snapshot=this.workspace.snapshot();if(!dayPolicy(now,this.settings().schedule,snapshot.calendar.overrides).scan)return;
this.lastTick=+now;
for(const connection of snapshot.connections){if(this.stopped)break;if(connection.status==='disabled'||(!connection.lastConnectedAt&&connection.status!=='connected')||this.pending.has(connection.id))continue;try{await this.scan(connection.id,this.principal);}catch{}}
}
async stop(){this.stopped=true;clearInterval(this.timer);await this.ticking;await Promise.allSettled([...this.pending.values()]);}
}
module.exports={BpmScanner};