fix: carry confirmed unsent orders into the next scheduled batch
This commit is contained in:
1 parent
dfdcf0cc2d
commit
069a69af93
23 files changed
+338
-40
No files matched your search
@@ -35,7 +35,7 @@ class OrderRuleDispatcher{
|
||||
}
|
||||
async perform(now){
|
||||
this.store.expireIntakes?.();
|
||||
for(const batch of this.store.snapshot().batches||[])if(batch.status==='prepared'&&!batch.manual&&now-Date.parse(batch.cutoff)>1800000)this.store.finishSending(batch.id,'failed','冻结发送队列超过30分钟,请负责人核验后受控补发。');
|
||||
for(const batch of this.store.snapshot().batches||[])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()});
|
||||
if(!this.ready())return;
|
||||
const config=freeze(structuredClone(this.store.settings())),schedule=this.settings()?.schedule,calendar=this.calendar();
|
||||
if(!schedule?.times?.length)return;
|
||||
@@ -54,7 +54,7 @@ class OrderRuleDispatcher{
|
||||
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;
|
||||
const grace=batch.intakeGrace?.cutoff===batch.cutoff&&Date.parse(batch.intakeGrace.expiresAt)===Date.parse(batch.cutoff)+1800000&&Date.parse(batch.createdAt)<=Date.parse(batch.intakeGrace.expiresAt)&&batch.orderIds?.every(id=>{const order=this.store.get('orders',id);return order&&this.store.intakeWindow(order.sample||order.event)?.end===batch.cutoff;});
|
||||
const grace=batch.intakeGrace?.cutoff===batch.cutoff&&Date.parse(batch.intakeGrace.expiresAt)===Date.parse(batch.cutoff)+1800000&&Date.parse(batch.createdAt)<=Date.parse(batch.intakeGrace.expiresAt)&&batch.orderIds?.every(id=>{const order=this.store.get('orders',id);return order&&(this.store.intakeWindow(order.sample||order.event)?.end===batch.cutoff||require('./carry-forward.cjs').windowForOrder(this.store,order,modeOf(batch))?.end===batch.cutoff);});
|
||||
if(batch.createdAt&&(Date.parse(batch.createdAt)>Date.parse(batch.cutoff)+60000&&!grace||Date.parse(batch.createdAt)<Date.parse(batch.cutoff)))continue;if(await this.dispatch(freeze(structuredClone(batch)),config)===false)continue;}
|
||||
});}catch(error){this.store.recordIssue?.('dispatch:'+String(cutoff),{component:'mail',error:error.message,cutoff});if(error.status!==409)throw error;}
|
||||
// Keep the current slot during its collection allowance so repaired categories retry.
|
||||
@@ -81,7 +81,7 @@ class OrderRuleDispatcher{
|
||||
assertMailPolicy(batch);
|
||||
require('./restricted-test.cjs').assertBatch(this.store,batch,config);
|
||||
if(batch.channelId&&batch.channelId!==config.channelId){
|
||||
this.store.finishSending(batch.id,'failed','批次固定的发件邮箱与当前渠道不同,请核实邮箱设置后生成新批次。');return;
|
||||
this.store.finishSending(batch.id,'failed','批次固定的发件邮箱与当前渠道不同,请核实邮箱设置后生成新批次。',{outcome:'not_submitted',phase:'preflight',at:this.clock().toISOString()});return;
|
||||
}
|
||||
const attachments=await this.attachments(batch,config);
|
||||
if(!manual&&this.clock().getTime()-Date.parse(batch.cutoff)>1800000)throw Error('冻结发送队列超过30分钟。');
|
||||
@@ -106,7 +106,8 @@ class OrderRuleDispatcher{
|
||||
}catch(error){
|
||||
if(!sending&&!manual&&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==='MAIL_POLICY_BLOCKED'?error.message:error.code==='ENOENT'?'归档文件不存在。':error.message==='归档文件校验失败。'?error.message:'请检查归档证据、SMTP 与启用状态。'}`);
|
||||
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);
|
||||
}finally{
|
||||
try{transport?.close?.();}finally{if(transport)this.mail.clients?.delete(transport);}
|
||||
}
|
||||
@@ -129,7 +130,7 @@ class OrderRuleDispatcher{
|
||||
const root=source.rootBatchId||source.id;
|
||||
if(this.store.all('batches').some(b=>b.id!==source.id&&(b.rootBatchId||b.id)===root&&['sent','sending','unknown','prepared'].includes(b.status)))fail('此批次已发送、结果待核实或已有补发尝试。',409);
|
||||
if(source.orderIds.some(oid=>{const state=delivery(this.store.get('orders',oid),modeOf(source));return !state||['sent','unknown','batched','sending'].includes(state.status);}))fail('原订单关联已发送或结果待核实。',409);
|
||||
const next={...structuredClone(source),id:crypto.randomUUID(),version:1,parentBatchId:source.id,rootBatchId:root,status:'prepared',manual:true,createdAt:this.clock().toISOString(),error:null,resolution:null,finishedAt:null,resend:{actor:actor.username,note:input.note.trim(),at:this.clock().toISOString()}};
|
||||
const next={...structuredClone(source),id:crypto.randomUUID(),version:1,parentBatchId:source.id,rootBatchId:root,status:'prepared',deliveryEvidence:null,submissionAttemptedAt:null,manual:true,createdAt:this.clock().toISOString(),error:null,resolution:null,finishedAt:null,resend:{actor:actor.username,note:input.note.trim(),at:this.clock().toISOString()}};
|
||||
this.store.put('batches',next);for(const oid of next.orderIds){const o=this.store.get('orders',oid);updateDelivery(o,modeOf(next),{batchId:next.id,status:'batched',error:null});this.store.put('orders',o);}
|
||||
this.store.put('resend-requests',{id:requestId,batchId:next.id,inputHash});this.store.audit(actor.username,'人工补发冻结分类邮件',source.id+':'+input.note.trim());return next;
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user