From bd69ec3b830c0dc2eaca0bd9d08ab1a1cd291f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=AC=A2?= Date: Tue, 1 Dec 2026 13:42:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E8=AE=A2=E5=8D=95=E9=80=9A=E7=9F=A5):=20?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=86=BB=E7=BB=93/=E5=8F=91=E9=80=81?= =?UTF-8?q?=E5=AE=B9=E5=B7=AE=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=86=BB=E7=BB=93?= =?UTF-8?q?=E6=9C=AA=E5=8F=91=E5=91=8A=E8=AD=A6=E4=B8=8E=E7=9E=AC=E6=97=B6?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E8=87=AA=E5=8A=A8=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 准备阶段允许截止后 65 秒(与发送校验的 5 秒容差一致),避免冻结写入晚几百毫秒导致整批被跳过 - 批次冻结超过 2 分钟仍未提交 SMTP 时生成业务异常,携带批次号与原因,不再静默等待 30 分钟 - SMTP 瞬时错误(ETIMEDOUT/ECONNRESET/EAI_AGAIN 等)自动安排重试,最多 3 次、15/30/45 秒退避,且仅在截止后 30 分钟内 - 策略、收件人拒绝、附件校验等确定性错误不重试,保持人工核验 --- src/modules/order-rules/dispatcher.cjs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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);