diff --git a/src/modules/order-rules/dispatcher.cjs b/src/modules/order-rules/dispatcher.cjs index 757a0ca..0adfb2f 100644 --- a/src/modules/order-rules/dispatcher.cjs +++ b/src/modules/order-rules/dispatcher.cjs @@ -46,21 +46,23 @@ class OrderRuleDispatcher{ async perform(now){ this.store.expireIntakes?.(); for(const batch of this.store.all('batches').reverse())if(batch.status==='prepared'&&!batch.manual&&now-Date.parse(batch.cutoff)>1800000)this.store.finishSending(batch.id,'failed','冻结发送队列超过30分钟,请负责人核验后受控补发。',{outcome:'not_submitted',phase:'queue_expired',at:this.clock().toISOString()}); + for(const batch of this.store.all('batches'))if(batch.status==='prepared'&&!batch.submissionAttemptedAt&&!batch.dispatchWarning&&now-Date.parse(batch.cutoff)>120000){batch.dispatchWarning=this.clock().toISOString();this.store.put('batches',batch);this.store.recordIssue?.('dispatch-warning:'+batch.id,{component:'mail',batchId:batch.id,error:'批次已冻结超过2分钟仍未提交SMTP,请检查发送闸门。',orderIds:batch.orderIds,cutoff:batch.cutoff});this.store.audit?.('系统发送','冻结未发告警',batch.id);} if(!this.ready())return; const config=freeze(structuredClone(this.store.settings())),schedule=this.settings()?.schedule,calendar=this.calendar(); if(!schedule?.times?.length)return; const signature=JSON.stringify([schedule,calendar]); if(!this.schedule||this.schedule.signature!==signature||now-Date.parse(this.schedule.next)>60000){const current=require('./window.cjs').upcomingWindow(now,schedule,calendar);this.schedule={signature,next:current?.available?current.end:nextSlots(schedule,now,1,calendar)[0]};} const resumed=this.store.pendingIntakeCutoffs?.().filter(cutoff=>now-Date.parse(cutoff)>60000)||[]; - const cutoff=resumed[0]||this.schedule.next,canPrepare=cutoff&&now-Date.parse(cutoff)>=0&&(now-Date.parse(cutoff)<=60000||resumed.includes(cutoff)); + const cutoff=resumed[0]||this.schedule.next,canPrepare=cutoff&&now-Date.parse(cutoff)>=0&&(now-Date.parse(cutoff)<=65000||resumed.includes(cutoff)); const queued=this.store.all('batches').some(b=>b.status==='prepared'&&!b.manual&&now-Date.parse(b.cutoff)>=0&&now-Date.parse(b.cutoff)<=1800000); if(!canPrepare&&!queued)return; try{await this.mail.exclusive(config.channelId,async()=>{ - if(!this.ready())return; + for(const batch of this.store.all('batches'))if(batch.status==='prepared'&&!batch.submissionAttemptedAt&&!batch.dispatchWarning&&now-Date.parse(batch.cutoff)>120000){batch.dispatchWarning=this.clock().toISOString();this.store.put('batches',batch);this.store.recordIssue?.('dispatch-warning:'+batch.id,{component:'mail',batchId:batch.id,error:'批次已冻结超过2分钟仍未提交SMTP,请检查发送闸门。',orderIds:batch.orderIds,cutoff:batch.cutoff});this.store.audit?.('系统发送','冻结未发告警',batch.id);} + if(!this.ready())return; if(canPrepare&&!this.store.discoveryBlockers(cutoff).length){const window=windowForCutoff(cutoff,schedule,calendar);this.store.prepare(cutoff,'系统调度',window);} // A frozen collection is immutable. Queue time is distinct from collection time. const batches=this.store.all('batches').reverse().filter(b=>b.status==='prepared'&&!b.manual&&now-Date.parse(b.cutoff)>=0&&now-Date.parse(b.cutoff)<=1800000); - for(const batch of batches){if(!this.ready())break; + for(const batch of batches){if(!this.ready())break;if(batch.retryAt&&Date.parse(batch.retryAt)>this.clock().getTime())continue; let expected;try{expected=windowForCutoff(batch.cutoff,schedule,calendar);}catch{continue;} const legacyStart=new Date(Date.parse(batch.cutoff)+8*3600000).toISOString().slice(0,10)+'T00:00:00+08:00'; if(!batch.window||batch.window.end!==expected.end||![Date.parse(expected.start),Date.parse(legacyStart)].includes(Date.parse(batch.window.start))||batch.window.startInclusive!==true||batch.window.endInclusive!==false)continue; @@ -115,6 +117,7 @@ class OrderRuleDispatcher{ this.store.finishSending(batch.id,partial?'unknown':'sent',partial?'部分收件人未被服务器接受,请核对邮件记录后处理。':'邮件服务器已接受全部收件人;最终送达请以邮件记录为准。'); }catch(error){ if(!sending&&!manual&&error.code==='DISCOVERY_INCOMPLETE')return false; + const transient=['ETIMEDOUT','ECONNRESET','ECONNREFUSED','EAI_AGAIN','EPIPE','ESOCKET','EHOSTUNREACH','ENETUNREACH','UND_ERR_CONNECT_TIMEOUT'].includes(error.code)||/ETIMEDOUT|ECONNRESET|ECONNREFUSED|EAI_AGAIN|socket hang up|网络|连接超时/i.test(String(error.message||''));if(!manual&&!sending&&transient&&(batch.retryCount||0)<3&&this.clock()-Date.parse(batch.cutoff)<1800000){const retryCount=(batch.retryCount||0)+1;this.store.put('batches',{...batch,retryCount,retryAt:new Date(this.clock().getTime()+Math.min(60000,15000*retryCount)).toISOString(),lastError:String(error.message||error.code||'').slice(0,300)});this.store.audit?.('系统发送','发送暂不可用,安排重试',batch.id+' 第'+retryCount+'次:'+String(error.message||error.code||'').slice(0,120));return;} 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())); const evidence=!sending?{outcome:'not_submitted',phase:'preflight',at:this.clock().toISOString()}:rejected?{outcome:'all_recipients_rejected',phase:'smtp_envelope',at:this.clock().toISOString()}:undefined; this.store.finishSending(batch.id,sending&&!rejected?'unknown':'failed',sending&&!rejected?'发送结果待核实,请核对邮件服务器记录后决定是否重试。':`发送前验证失败:${error.code==='MAIL_POLICY_BLOCKED'?error.message:error.code==='ENOENT'?'归档文件不存在。':error.message==='归档文件校验失败。'?error.message:'请检查归档证据、SMTP 与启用状态。'}`,evidence);