Skip to content

Commit 4b2405d

Browse files
committed
feat: one-shot mode timeout
1 parent b0b3ef2 commit 4b2405d

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/core/app/fetcher.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import moment from "moment";
22
import type { Address } from "viem";
3-
43
import type { PoolExternalAPYResult, PoolPointsResult } from "../../pools";
54
import { getPoolExternalAPY, getPoolPoints } from "../../pools";
65
import { getPoolExtraAPY } from "../../pools/extraAPY/apy";
@@ -52,8 +51,10 @@ import type { TokenExtraRewardsResult } from "../../tokens/tokenExtraRewards";
5251
import { getTokenExtraRewards } from "../../tokens/tokenExtraRewards";
5352
import type { NetworkType } from "../chains";
5453
import { getChainId, getNetworkType, supportedChains } from "../chains";
54+
import { ONE_SHOT_TIMEOUT } from "../config";
5555
import type { IOutputWriter, OneShotOutput } from "../output";
5656
import { captureException } from "../sentry";
57+
import { withTimeout } from "../utils";
5758
import { POOL_APY_TASK_INTERVAL, TOKEN_APY_TASK_INTERVAL } from "./constants";
5859

5960
export type ApyDetails = Apy & { lastUpdated: string };
@@ -319,7 +320,7 @@ export class Fetcher {
319320
setInterval(quarterTask, POOL_APY_TASK_INTERVAL);
320321
}
321322

322-
public async oneShot(outputWriter: IOutputWriter): Promise<void> {
323+
async #oneShot(outputWriter: IOutputWriter): Promise<void> {
323324
console.log("[SYSTEM]: Starting one-shot mode");
324325

325326
await this.runNetworkRewards();
@@ -384,6 +385,17 @@ export class Fetcher {
384385
`[SYSTEM]: One-shot mode completed. Chains: ${output.metadata.totalChains}, Successful: ${output.metadata.successfulChains}, Failed: ${output.metadata.failedChains}`,
385386
);
386387
}
388+
389+
public async oneShot(outputWriter: IOutputWriter): Promise<void> {
390+
if (ONE_SHOT_TIMEOUT) {
391+
await withTimeout(
392+
() => this.#oneShot(outputWriter),
393+
ONE_SHOT_TIMEOUT * 1000,
394+
);
395+
} else {
396+
await this.#oneShot(outputWriter);
397+
}
398+
}
387399
}
388400

389401
interface LogRewardsProps {

src/core/config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ export const OUTPUT_JSON = process.env.OUTPUT_JSON || "output.json";
99
export const SENTRY_DSN = process.env.SENTRY_DSN;
1010

1111
export const S3_BUCKET = process.env.S3_BUCKET;
12+
/**
13+
* Cache TTL in seconds
14+
*/
1215
export const CACHE_TTL = process.env.CACHE_TTL
1316
? parseInt(process.env.CACHE_TTL, 10)
1417
: undefined;
18+
19+
/**
20+
* One-shot mode timeout in seconds
21+
*/
22+
export const ONE_SHOT_TIMEOUT = process.env.ONE_SHOT_TIMEOUT
23+
? parseInt(process.env.ONE_SHOT_TIMEOUT, 10)
24+
: 0;

src/core/utils/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { setTimeout } from "node:timers/promises";
2+
13
export const json_stringify = (
24
o: any,
35
space?: number,
@@ -28,3 +30,15 @@ export const json_stringify = (
2830
};
2931

3032
export type PartialRecord<K extends keyof any, T> = { [P in K]?: T };
33+
34+
export async function timeout(ms: number): Promise<never> {
35+
await setTimeout(ms);
36+
throw new Error("The operation was timed out");
37+
}
38+
39+
export async function withTimeout<T>(
40+
run: () => Promise<T>,
41+
ms: number,
42+
): Promise<T> {
43+
return Promise.race([run(), timeout(ms)]);
44+
}

0 commit comments

Comments
 (0)