switched to new way :)
This commit is contained in:
parent
b03a2a25f1
commit
f22d182c8f
30 changed files with 0 additions and 0 deletions
24
src-old/util/retry.ts
Normal file
24
src-old/util/retry.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { sleep } from './sleep.js';
|
||||
import { logger } from './logger.js';
|
||||
|
||||
export async function retry<T>(
|
||||
fn: () => Promise<T>,
|
||||
options: { maxAttempts?: number; delayMs?: number; label?: string } = {},
|
||||
): Promise<T> {
|
||||
const { maxAttempts = 3, delayMs = 1000, label = 'operation' } = options;
|
||||
|
||||
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (err) {
|
||||
if (attempt === maxAttempts) {
|
||||
logger.error({ err, attempt, label }, `${label} failed after ${maxAttempts} attempts`);
|
||||
throw err;
|
||||
}
|
||||
logger.warn({ err, attempt, label }, `${label} failed, retrying in ${delayMs}ms...`);
|
||||
await sleep(delayMs * attempt);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('Unreachable');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue