fix: close discovery dispatch and persistent UI state races

This commit is contained in:
abaishu0730 committed 2026-10-19 14:33:09 +08:00
1 parent 0a366f43d6
commit 9d8d6971c1
19 files changed
+237 -77

No files matched your search

+8 -3
View File
@@ -48,14 +48,15 @@ class OrderRuleDispatcher{
// A busy mailbox cannot consume the slot or reserve any orders. Recovery
// applies only to this exact business window, never to earlier history.
const window=windowForCutoff(cutoff,schedule,calendar);
const existing=(this.store.snapshot().batches||[]).filter(batch=>sameWindow(batch,cutoff,window));
if(this.store.discoveryBlockers(cutoff).length)return;
const fresh=this.store.prepare(cutoff,'系统调度',window).filter(batch=>sameWindow(batch,cutoff,window));
const batches=[...new Map([...existing,...fresh].map(batch=>[batch.id,freeze(structuredClone(batch))])).values()];
const existing=(this.store.snapshot().batches||[]).filter(batch=>sameWindow(batch,cutoff,window));
const batches=[...new Map([...fresh,...existing].map(batch=>[batch.id,freeze(structuredClone(batch))])).values()];
let interrupted=false;
for(const batch of batches){
if(!this.ready()){interrupted=true;break;}
if(batch.status!=='prepared')continue;
await this.dispatch(batch,config);
if(await this.dispatch(batch,config)===false){interrupted=true;break;}
}
if(!interrupted)this.schedule={signature,next:nextSlots(schedule,now,1,calendar)[0]};
});
@@ -88,6 +89,9 @@ class OrderRuleDispatcher{
transport=this.mail.smtpFactory(channel.smtp);
this.mail.clients?.add(transport);
await transport.verify();
// Scan readiness may change while SMTP verification is pending. Keep this
// unsent reservation recoverable instead of turning a scan barrier into failure.
if(!manual&&(this.store.discoveryBlockers(batch.cutoff).length||this.store.batchIncomplete(batch)))return false;
if(manual?this.stopped||!this.providersReady()||!this.store.providersReady():!this.ready()||this.store.settings().channelId!==config.channelId)throw Error('发送已暂停或配置已变更。');
if(!manual&&this.clock().getTime()-Date.parse(batch.cutoff)>60000)throw Error('本时段发送宽限已结束。');
this.mail.store.guard(channel.id,channel.version);
@@ -97,6 +101,7 @@ class OrderRuleDispatcher{
const partial=result?.rejected?.length||[...batch.to,...batch.cc].some(address=>!accepted.has(address.toLowerCase()));
this.store.finishSending(batch.id,partial?'unknown':'sent',partial?'部分收件人未被服务器接受,请核对邮件记录后处理。':'邮件服务器已接受全部收件人;最终送达请以邮件记录为准。');
}catch(error){
if(!sending&&error.code==='DISCOVERY_INCOMPLETE')return false;
const rejected=error.code==='EENVELOPE'&&error.command==='RCPT TO'&&Array.isArray(error.rejected)&&error.rejected.length>0&&(!error.accepted||error.accepted.length===0)&&[...batch.to,...batch.cc].every(address=>error.rejected.some(r=>String(r.address||r).toLowerCase()===address.toLowerCase()));
this.store.finishSending(batch.id,sending&&!rejected?'unknown':'failed',sending&&!rejected?'发送结果待核实,请核对邮件服务器记录后决定是否重试。':`发送前验证失败:${error.code==='ENOENT'?'归档文件不存在。':error.message==='归档文件校验失败。'?error.message:'请检查归档证据、SMTP 与启用状态。'}`);
}finally{