一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
41 行
936 B
TypeScript
41 行
936 B
TypeScript
import Logger from '@joplin/utils/Logger';
|
|
import { msleep } from '@joplin/utils/time';
|
|
|
|
const logger = Logger.create('retryWithCount');
|
|
|
|
interface FailureEvent {
|
|
error: Error;
|
|
willRetry: boolean;
|
|
}
|
|
|
|
interface Options {
|
|
count: number;
|
|
delayOnFailure?: (retryCount: number)=> number;
|
|
onFail: (event: FailureEvent)=> void|Promise<void>;
|
|
}
|
|
|
|
const retryWithCount = async (task: ()=> Promise<void>, { count, delayOnFailure, onFail }: Options) => {
|
|
let lastError: Error|null = null;
|
|
for (let retry = 0; retry < count; retry ++) {
|
|
try {
|
|
return await task();
|
|
} catch (error) {
|
|
lastError = error;
|
|
|
|
const willRetry = retry + 1 < count;
|
|
await onFail({ error, willRetry });
|
|
|
|
const delay = willRetry && delayOnFailure ? delayOnFailure(retry + 1) : 0;
|
|
if (delay) {
|
|
logger.info(`Retrying after ${delay}ms...`);
|
|
await msleep(delay);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (lastError) throw lastError;
|
|
};
|
|
|
|
export default retryWithCount;
|
|
|