Intelligence, Advanced Charting & Trading Platform
fix(notification): terminal UNKNOWN send outcome blocks unsafe retries; validate lease chronology
davoodmehraban89 committed
Oct 21, 2026 at 09:16 UTC
d40743b3cc4d2a218aa22f0065d4be1e537aa74f
1 file changed
+15
-7
packages/market-domain/src/notification-outbox.mjs
+15
-7
@@ -3,20 +3,20 @@
3
* Caller MUST persist via an atomic compare-and-swap store for cross-worker
4
* idempotency. Pure logic cannot promise exactly-once network delivery.
5
*/
6
-const allowed=new Set(['PENDING','LEASED','SENT','FAILED','ACKNOWLEDGED']);
6
+const allowed=new Set(['PENDING','LEASED','SENT','FAILED','UNKNOWN','ACKNOWLEDGED']);
7
export function notificationRecord({id,recipientChannel,eventId,createdAt,maxAttempts=3}){
8
if(typeof id!=='string'||!id||typeof eventId!=='string'||!eventId||
9
!['BALE','PWA','EITA'].includes(recipientChannel)||!Number.isInteger(maxAttempts)||
10
maxAttempts<1||maxAttempts>10||!Number.isFinite(Date.parse(createdAt)))
11
throw new RangeError('invalid message metadata');
12
return {id,eventId,recipientChannel,status:'PENDING',createdAt,
13
- attempts:0,maxAttempts,leaseUntil:null,providerMessageId:null,deliveredAt:null,
13
+ attempts:0,maxAttempts,leasedAt:null,leaseUntil:null,providerMessageId:null,deliveredAt:null,
14
failedAt:null,nextRetryAt:null,reason:null};
15
}
16
export function claimDelivery(record,{now,leaseMs=30000}){
17
if(!allowed.has(record?.status)||!Number.isFinite(Date.parse(now))||
18
!Number.isSafeInteger(leaseMs)||leaseMs<=0)throw new RangeError('invalid claim');
19
- if(record.status==='SENT'||record.status==='ACKNOWLEDGED'||record.attempts>=record.maxAttempts)
19
+ if(record.status==='SENT'||record.status==='ACKNOWLEDGED'||record.status==='UNKNOWN'||record.attempts>=record.maxAttempts)
20
return {record,claimed:false};
21
const at=Date.parse(now);
22
if(record.status==='LEASED'&&Date.parse(record.leaseUntil)>at)
@@ -24,13 +24,20 @@ export function claimDelivery(record,{now,leaseMs=30000}){
24
if(record.nextRetryAt&&Date.parse(record.nextRetryAt)>at)
25
return {record,claimed:false};
26
const updated={...record,status:'LEASED',attempts:record.attempts+1,
27
- leaseUntil:new Date(at+leaseMs).toISOString(),nextRetryAt:null};
27
+ leasedAt:now,leaseUntil:new Date(at+leaseMs).toISOString(),nextRetryAt:null};
28
return {record:updated,claimed:true};
29
}
30
-export function recordDeliveryOutcome(record,{at,success,providerMessageId=null,retryDelayMs=60000}){
31
- if(record?.status!=='LEASED'||!Number.isFinite(Date.parse(at))||
32
- typeof success!=='boolean'||!Number.isSafeInteger(retryDelayMs)||retryDelayMs<1000)
30
+export function recordDeliveryOutcome(record,{at,success,unknown=false,providerMessageId=null,retryDelayMs=60000}){
31
+ if(record?.status!=='LEASED'||typeof at!=='string'||!Number.isFinite(Date.parse(at))||
32
+ !Number.isFinite(Date.parse(record.leasedAt))||
33
+ Date.parse(at)<Date.parse(record.leasedAt)||typeof success!=='boolean'||
34
+ typeof unknown!=='boolean'||(unknown&&success)||
35
+ !Number.isSafeInteger(retryDelayMs)||retryDelayMs<1000)
36
throw new RangeError('invalid completion');
37
+ // Network timeout after a POST is ambiguous: it might already have been accepted.
38
+ // Automatic retry risks duplicate user notifications; require manual reconciliation.
39
+ if(unknown)return {...record,status:'UNKNOWN',leaseUntil:null,failedAt:at,
40
+ nextRetryAt:null,reason:'OUTCOME_UNKNOWN_RECONCILE_MANUALLY'};
41
if(success){
42
if(typeof providerMessageId!=='string'||!providerMessageId)
43
throw new RangeError('verified provider message ID required');
@@ -54,6 +61,7 @@ export function notificationUiStatus(record){
61
case 'LEASED':return 'در حال تلاش برای ارسال';
62
case 'SENT':return 'ارسال توسط ارائهدهنده پذیرفته شد؛ مشاهده کاربر تأییدنشده';
63
case 'ACKNOWLEDGED':return 'دریافت توسط کاربر تأیید شد';
64
+ case 'UNKNOWN':return 'نتیجهٔ ارسال نامشخص است؛ برای جلوگیری از پیام تکراری تلاش مجدد متوقف شد';
65
case 'FAILED':return record.reason==='ATTEMPTS_EXHAUSTED'?'ارسال ناموفق؛ تلاش مجدد متوقف شد':'ارسال ناموفق؛ تلاش مجدد زمانبندی شده';
66
default:return 'وضعیت اعلان تأییدنشده';
67
}