@bronlabs/intents-sdk 1.0.44 → 1.0.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/orderProcessor.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { EventQueue } from './eventQueue.js';
|
|
2
|
-
|
|
2
|
+
type OrderEvent = {
|
|
3
3
|
orderId: string;
|
|
4
4
|
status: bigint;
|
|
5
|
-
|
|
5
|
+
attempts?: number;
|
|
6
|
+
nextAttemptAt?: number;
|
|
7
|
+
};
|
|
6
8
|
export declare abstract class OrderProcessor {
|
|
7
9
|
protected isRunning: boolean;
|
|
8
10
|
protected delayedQueue: EventQueue<OrderEvent>;
|
|
@@ -10,5 +12,6 @@ export declare abstract class OrderProcessor {
|
|
|
10
12
|
abstract process(orderId: string, status: bigint): Promise<void>;
|
|
11
13
|
stop(): Promise<void>;
|
|
12
14
|
private processDelayedQueue;
|
|
15
|
+
private backoffMs;
|
|
13
16
|
}
|
|
14
17
|
export {};
|
package/dist/orderProcessor.js
CHANGED
|
@@ -17,20 +17,40 @@ export class OrderProcessor {
|
|
|
17
17
|
log.info('Order processor stopped.');
|
|
18
18
|
}
|
|
19
19
|
async processDelayedQueue() {
|
|
20
|
+
const start = this.delayedQueue.peek();
|
|
21
|
+
let looped = false;
|
|
20
22
|
while (!this.delayedQueue.isEmpty() && this.isRunning) {
|
|
21
23
|
const event = this.delayedQueue.peek();
|
|
22
|
-
if (event)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
if (!event)
|
|
25
|
+
break;
|
|
26
|
+
event.attempts = event.attempts ?? 0;
|
|
27
|
+
event.nextAttemptAt = event.nextAttemptAt ?? 0;
|
|
28
|
+
if (event.nextAttemptAt > Date.now()) {
|
|
29
|
+
this.delayedQueue.remove();
|
|
30
|
+
this.delayedQueue.add(event);
|
|
31
|
+
if (start && event.orderId === start.orderId) {
|
|
32
|
+
if (looped)
|
|
33
|
+
break;
|
|
34
|
+
looped = true;
|
|
30
35
|
}
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
await this.process(event.orderId, event.status);
|
|
40
|
+
this.delayedQueue.remove();
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
log.error(`Error processing delayed event ${event.orderId} ${event.status}:`, error);
|
|
44
|
+
event.attempts += 1;
|
|
45
|
+
event.nextAttemptAt = Date.now() + this.backoffMs(event.attempts);
|
|
46
|
+
this.delayedQueue.remove();
|
|
47
|
+
this.delayedQueue.add(event);
|
|
31
48
|
}
|
|
32
49
|
}
|
|
33
|
-
setTimeout(async () => await this.processDelayedQueue(),
|
|
50
|
+
setTimeout(async () => await this.processDelayedQueue(), 1000);
|
|
51
|
+
}
|
|
52
|
+
backoffMs(attempts) {
|
|
53
|
+
return Math.min(60000, 3000 * 2 ** Math.max(0, attempts - 1));
|
|
34
54
|
}
|
|
35
55
|
}
|
|
36
56
|
//# sourceMappingURL=orderProcessor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orderProcessor.js","sourceRoot":"","sources":["../src/orderProcessor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"orderProcessor.js","sourceRoot":"","sources":["../src/orderProcessor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AASxC,MAAM,OAAgB,cAAc;IAIlC;QAHU,cAAS,GAAY,IAAI,CAAC;QAIlC,IAAI,CAAC,YAAY,GAAG,IAAI,UAAU,EAAc,CAAC;QACjD,IAAI,CAAC,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