Skip to content

Commit

Permalink
Move retry util function to builder-util-runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
juwonjung-hdj committed Sep 4, 2024
1 parent b6b99c5 commit d71e8b4
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/builder-util-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export { parseXml, XElement } from "./xml"
export { BlockMap } from "./blockMapApi"
export { newError } from "./error"
export { MemoLazy } from "./MemoLazy"
export { retry } from "./retry"

// nsis
export const CURRENT_APP_INSTALLER_FILE_NAME = "installer.exe"
Expand Down
15 changes: 15 additions & 0 deletions packages/builder-util-runtime/src/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CancellationToken } from "./CancellationToken"

export async function retry<T>(task: () => Promise<T>, retryCount: number, interval: number, backoff = 0, attempt = 0, shouldRetry?: (e: any) => boolean): Promise<T> {
const cancellationToken = new CancellationToken()
try {
return await task()
} catch (error: any) {
if ((shouldRetry?.(error) ?? true) && retryCount > 0 && !cancellationToken.cancelled) {
await new Promise(resolve => setTimeout(resolve, interval + backoff * attempt))
return await retry(task, retryCount - 1, interval, backoff, attempt + 1, shouldRetry)
} else {
throw error
}
}
}
16 changes: 4 additions & 12 deletions packages/builder-util/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { appBuilderPath } from "app-builder-bin"
import { CancellationToken, safeStringifyJson } from "builder-util-runtime"
import { safeStringifyJson, retry as _retry } from "builder-util-runtime"
import * as chalk from "chalk"
import { ChildProcess, execFile, ExecFileOptions, SpawnOptions } from "child_process"
import { spawn as _spawn } from "cross-spawn"
Expand Down Expand Up @@ -408,16 +408,8 @@ export async function executeAppBuilder(
}

export async function retry<T>(task: () => Promise<T>, retryCount: number, interval: number, backoff = 0, attempt = 0, shouldRetry?: (e: any) => boolean): Promise<T> {
const cancellationToken = new CancellationToken()
try {
return await task()
} catch (error: any) {
return await _retry(task, retryCount, interval, backoff, attempt, e => {
log.info(`Above command failed, retrying ${retryCount} more times`)
if ((shouldRetry?.(error) ?? true) && retryCount > 0 && !cancellationToken.cancelled) {
await new Promise(resolve => setTimeout(resolve, interval + backoff * attempt))
return await retry(task, retryCount - 1, interval, backoff, attempt + 1, shouldRetry)
} else {
throw error
}
}
return shouldRetry?.(e) ?? true
})
}
1 change: 0 additions & 1 deletion packages/electron-updater/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"out"
],
"dependencies": {
"builder-util": "workspace:*",
"builder-util-runtime": "workspace:*",
"fs-extra": "^10.1.0",
"js-yaml": "^4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-updater/src/AppUpdater.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { retry } from "builder-util"
import {
AllPublishOptions,
asArray,
Expand All @@ -11,6 +10,7 @@ import {
CancellationError,
ProgressInfo,
BlockMap,
retry,
} from "builder-util-runtime"
import { randomBytes } from "crypto"
import { release } from "os"
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d71e8b4

Please sign in to comment.