Skip to content

Commit

Permalink
Retry - increase amount and sleepTime
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaur-bndes committed Feb 14, 2023
1 parent c18c2ec commit 2dd6060
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/lib/extract/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {retry} from "../utils/utils.ts";
export abstract class Extractor {
readonly feedback = new ConsoleFeedback();

private readonly maxRetries = 5;

constructor(protected config: Config) {
}

Expand Down Expand Up @@ -45,11 +47,11 @@ export abstract class Extractor {
} else {
let dst = path.resolve(dstDir, child.name);
log.debug(`- MOVE ${from} => ${dst}`);
await retry(3, () => Deno.renameSync(from, dst));
await retry(this.maxRetries, () => Deno.renameSync(from, dst));
}
}

await retry(3, () => Deno.removeSync(srcDir));
await retry(this.maxRetries, () => Deno.removeSync(srcDir));
}

async extractToTemp(file: string): Promise<string> {
Expand Down
10 changes: 9 additions & 1 deletion src/lib/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ import {
assertNotEquals,
} from "https://deno.land/std/testing/asserts.ts";

import {homedir} from "./utils.ts";
import { homedir, retry } from "./utils.ts";

Deno.test("Utils", () => {
const home = homedir()

assertNotEquals(home, undefined);
})

Deno.test("retry", async () => {
const startTime = performance.now()
await retry(3, () => { throw "Abort" })
const endTime = performance.now()

console.log(`Test took ${endTime - startTime}ms`)
})
6 changes: 3 additions & 3 deletions src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ export function envChain(...names: string[]): string | undefined {
return undefined;
}

export async function retry(maxRetries: number, codeToRun:(() => void)): void {
export async function retry(maxRetries: number, codeToRun:(() => void), sleepAmountMax: number = 5): Promise<void> {
let lastError = undefined

for (let retries = 0; retries < maxRetries; retries++) {
try {
codeToRun()
return
} catch (error) {
log.warning("RETRY - Ignoring " + error)
log.warning(`RETRY - Ignoring ${error}`)
lastError = error
await sleepRandomAmountOfSeconds(0, 2)
await sleepRandomAmountOfSeconds(0, Math.max(1, sleepAmountMax))
}
}

Expand Down

0 comments on commit 2dd6060

Please sign in to comment.