From 79321f83293bc37d37af4b0a0b2bd324f5bafe1a Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Tue, 2 Apr 2024 12:58:33 +0100 Subject: fix(workers): Add a timeout to the crawling job to prevent it from getting stuck. Fixes #63 --- apps/workers/utils.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 apps/workers/utils.ts (limited to 'apps/workers/utils.ts') diff --git a/apps/workers/utils.ts b/apps/workers/utils.ts new file mode 100644 index 00000000..2f56d3f0 --- /dev/null +++ b/apps/workers/utils.ts @@ -0,0 +1,16 @@ +export function withTimeout( + func: (param: T) => Promise, + timeoutSec: number, +) { + return async (param: T): Promise => { + return await Promise.race([ + func(param), + new Promise((_resolve, reject) => + setTimeout( + () => reject(new Error(`Timed-out after ${timeoutSec} secs`)), + timeoutSec * 1000, + ), + ), + ]); + }; +} -- cgit v1.2.3-70-g09d2