diff --git a/worker/src/scheduler.ts b/worker/src/scheduler.ts index 8786363..2de1f54 100644 --- a/worker/src/scheduler.ts +++ b/worker/src/scheduler.ts @@ -72,19 +72,20 @@ async function runCycle(): Promise { log.info({ accountCount: accounts.length }, "Processing accounts"); const results = await Promise.allSettled( - accounts.map((account) => - Promise.race([ + accounts.map((account) => { + let timer: ReturnType; + return Promise.race([ withTdlibMutex(account.phone, `ingest:${account.phone}`, () => runWorkerForAccount(account) ), - new Promise((_, reject) => - setTimeout( + new Promise((_, reject) => { + timer = setTimeout( () => reject(new Error(`Account ${account.phone} ingestion timed out after ${CYCLE_TIMEOUT_MS / 60_000}min`)), CYCLE_TIMEOUT_MS - ) - ), - ]) - ) + ); + }), + ]).finally(() => clearTimeout(timer)); + }) ); for (let i = 0; i < results.length; i++) {