@api3/commons 0.11.0 → 0.12.0
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.
|
@@ -40,5 +40,7 @@ export interface RunInLoopOptions {
|
|
|
40
40
|
*/
|
|
41
41
|
initialDelayMs?: number;
|
|
42
42
|
}
|
|
43
|
-
export declare const runInLoop: (fn: () => Promise<
|
|
43
|
+
export declare const runInLoop: (fn: () => Promise<{
|
|
44
|
+
shouldContinueRunning: boolean;
|
|
45
|
+
} | void>, options: RunInLoopOptions) => Promise<void>;
|
|
44
46
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/run-in-loop/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAGxC,MAAM,WAAW,gBAAgB;IAC/B,6EAA6E;IAC7E,MAAM,EAAE,MAAM,CAAC;IACf,kDAAkD;IAClD,QAAQ,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IAC7B;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,eAAO,MAAM,SAAS,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/run-in-loop/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAGxC,MAAM,WAAW,gBAAgB;IAC/B,6EAA6E;IAC7E,MAAM,EAAE,MAAM,CAAC;IACf,kDAAkD;IAClD,QAAQ,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IAC7B;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,eAAO,MAAM,SAAS,OAChB,MAAM,OAAO,CAAC;IAAE,qBAAqB,EAAE,OAAO,CAAA;CAAE,GAAG,IAAI,CAAC,WACnD,gBAAgB,kBA4D1B,CAAC"}
|
|
@@ -18,7 +18,7 @@ const runInLoop = async (fn, options) => {
|
|
|
18
18
|
const executionId = (0, utils_1.generateRandomBytes32)();
|
|
19
19
|
if (enabled) {
|
|
20
20
|
const context = logLabel ? { executionId, label: logLabel } : { executionId };
|
|
21
|
-
await logger.runWithContext(context, async () => {
|
|
21
|
+
const shouldContinueRunning = await logger.runWithContext(context, async () => {
|
|
22
22
|
const goRes = await (0, promise_utils_1.go)(fn, hardTimeoutMs ? { totalTimeoutMs: hardTimeoutMs } : {}); // NOTE: This is a safety net to prevent the loop from hanging
|
|
23
23
|
if (!goRes.success) {
|
|
24
24
|
logger.error(`Unexpected runInLoop error`, goRes.error);
|
|
@@ -30,7 +30,12 @@ const runInLoop = async (fn, options) => {
|
|
|
30
30
|
else {
|
|
31
31
|
logger.info(`Execution finished`, { executionTimeMs });
|
|
32
32
|
}
|
|
33
|
+
return goRes.data?.shouldContinueRunning === false ? false : true;
|
|
33
34
|
});
|
|
35
|
+
// Stop loop execution if the callback return value indicates it should stop
|
|
36
|
+
if (!shouldContinueRunning) {
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
34
39
|
}
|
|
35
40
|
else {
|
|
36
41
|
// If the bot is disabled, we still want to run the loop to prevent the process from hanging. We also want to
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/run-in-loop/index.ts"],"names":[],"mappings":";;;AAAA,uDAAyC;AAGzC,oCAAwD;AA4CjD,MAAM,SAAS,GAAG,KAAK,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/run-in-loop/index.ts"],"names":[],"mappings":";;;AAAA,uDAAyC;AAGzC,oCAAwD;AA4CjD,MAAM,SAAS,GAAG,KAAK,EAC5B,EAA4D,EAC5D,OAAyB,EACzB,EAAE;IACF,MAAM,EACJ,MAAM,EACN,QAAQ,EACR,WAAW,GAAG,CAAC,EACf,aAAa,GAAG,CAAC,EACjB,aAAa,EACb,aAAa,EACb,aAAa,EACb,OAAO,GAAG,IAAI,EACd,cAAc,GACf,GAAG,OAAO,CAAC;IAEZ,IAAI,aAAa,IAAI,aAAa,IAAI,aAAa,GAAG,aAAa,EAAE,CAAC;QACpE,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;IAC1E,CAAC;IACD,IAAI,aAAa,IAAI,aAAa,IAAI,aAAa,GAAG,aAAa,EAAE,CAAC;QACpE,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;IAC1E,CAAC;IAED,IAAI,cAAc;QAAE,MAAM,IAAA,aAAK,EAAC,cAAc,CAAC,CAAC;IAEhD,OAAO,IAAI,EAAE,CAAC;QACZ,MAAM,cAAc,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;QACzC,MAAM,WAAW,GAAG,IAAA,6BAAqB,GAAE,CAAC;QAE5C,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,KAAK,EA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AM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,EAAE,iBAAiB,CAAC,CAAC;QAC5D,MAAM,cAAc,GAAG,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;QACpF,IAAI,cAAc,GAAG,CAAC;YAAE,MAAM,IAAA,aAAK,EAAC,cAAc,CAAC,CAAC;IACtD,CAAC;AACH,CAAC,CAAC;AA9DW,QAAA,SAAS,aA8DpB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@api3/commons",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"keywords": [],
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"engines": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@api3/ois": "^2.3.2",
|
|
28
28
|
"@api3/promise-utils": "^0.4.0",
|
|
29
|
-
"axios": "^1.7.
|
|
29
|
+
"axios": "^1.7.3",
|
|
30
30
|
"dotenv": "^16.4.5",
|
|
31
31
|
"ethers": "^5.7.2",
|
|
32
32
|
"lodash": "^4.17.21",
|
|
@@ -38,12 +38,12 @@
|
|
|
38
38
|
"@api3/eslint-plugin-commons": "^2.0.1",
|
|
39
39
|
"@types/jest": "^29.5.12",
|
|
40
40
|
"@types/lodash": "^4.17.7",
|
|
41
|
-
"@types/node": "^20.14.
|
|
41
|
+
"@types/node": "^20.14.14",
|
|
42
42
|
"eslint": "^8.57.0",
|
|
43
|
-
"husky": "^9.1.
|
|
43
|
+
"husky": "^9.1.4",
|
|
44
44
|
"jest": "^29.7.0",
|
|
45
45
|
"prettier": "^3.3.3",
|
|
46
|
-
"ts-jest": "^29.2.
|
|
46
|
+
"ts-jest": "^29.2.4",
|
|
47
47
|
"typescript": "^5.5.4"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createLogger } from '../logger';
|
|
2
|
+
|
|
3
|
+
import { runInLoop } from './index';
|
|
4
|
+
|
|
5
|
+
describe(runInLoop.name, () => {
|
|
6
|
+
const logger = createLogger({
|
|
7
|
+
colorize: true,
|
|
8
|
+
enabled: true,
|
|
9
|
+
minLevel: 'info',
|
|
10
|
+
format: 'json',
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('stops the loop after getting the stop signal', async () => {
|
|
14
|
+
const fn = async () => ({ shouldContinueRunning: false });
|
|
15
|
+
const fnSpy = jest
|
|
16
|
+
.spyOn({ fn }, 'fn')
|
|
17
|
+
.mockImplementationOnce(async () => ({ shouldContinueRunning: true }))
|
|
18
|
+
.mockImplementationOnce(async () => ({ shouldContinueRunning: true }))
|
|
19
|
+
.mockImplementationOnce(async () => ({ shouldContinueRunning: false }));
|
|
20
|
+
await runInLoop(fnSpy as any, { logger });
|
|
21
|
+
expect(fnSpy).toHaveBeenCalledTimes(3);
|
|
22
|
+
});
|
|
23
|
+
});
|
package/src/run-in-loop/index.ts
CHANGED
|
@@ -45,7 +45,10 @@ export interface RunInLoopOptions {
|
|
|
45
45
|
initialDelayMs?: number;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export const runInLoop = async (
|
|
48
|
+
export const runInLoop = async (
|
|
49
|
+
fn: () => Promise<{ shouldContinueRunning: boolean } | void>,
|
|
50
|
+
options: RunInLoopOptions
|
|
51
|
+
) => {
|
|
49
52
|
const {
|
|
50
53
|
logger,
|
|
51
54
|
logLabel,
|
|
@@ -73,7 +76,7 @@ export const runInLoop = async (fn: () => Promise<void>, options: RunInLoopOptio
|
|
|
73
76
|
|
|
74
77
|
if (enabled) {
|
|
75
78
|
const context = logLabel ? { executionId, label: logLabel } : { executionId };
|
|
76
|
-
await logger.runWithContext(context, async () => {
|
|
79
|
+
const shouldContinueRunning = await logger.runWithContext(context, async () => {
|
|
77
80
|
const goRes = await go(fn, hardTimeoutMs ? { totalTimeoutMs: hardTimeoutMs } : {}); // NOTE: This is a safety net to prevent the loop from hanging
|
|
78
81
|
if (!goRes.success) {
|
|
79
82
|
logger.error(`Unexpected runInLoop error`, goRes.error);
|
|
@@ -85,7 +88,14 @@ export const runInLoop = async (fn: () => Promise<void>, options: RunInLoopOptio
|
|
|
85
88
|
} else {
|
|
86
89
|
logger.info(`Execution finished`, { executionTimeMs });
|
|
87
90
|
}
|
|
91
|
+
|
|
92
|
+
return goRes.data?.shouldContinueRunning === false ? false : true;
|
|
88
93
|
});
|
|
94
|
+
|
|
95
|
+
// Stop loop execution if the callback return value indicates it should stop
|
|
96
|
+
if (!shouldContinueRunning) {
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
89
99
|
} else {
|
|
90
100
|
// If the bot is disabled, we still want to run the loop to prevent the process from hanging. We also want to
|
|
91
101
|
// sleep according to the wait time logic.
|