Add retry logic for flaky git operations

Fix #132218
This commit is contained in:
Rob Lourens
2021-10-12 17:08:11 -07:00
parent 3bf7202085
commit 83fec0220c
2 changed files with 24 additions and 5 deletions

View File

@@ -68,3 +68,23 @@ export function timeout(i: number) {
}, i);
});
}
export interface ITask<T> {
(): T;
}
export async function retry<T>(task: ITask<Promise<T>>, delay: number, retries: number): Promise<T> {
let lastError: Error | undefined;
for (let i = 0; i < retries; i++) {
try {
return await task();
} catch (error) {
lastError = error;
await timeout(delay);
}
}
throw lastError;
}