Skip to content

[node-core-library] Provide retryCount parameter to actions executed using Async.runWithRetriesAsync #5069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/node-core-library",
"comment": "Provide the `retryCount` parameter to actions executed using `Async.runWithRetriesAsync`",
"type": "patch"
}
],
"packageName": "@rushstack/node-core-library"
}
5 changes: 1 addition & 4 deletions common/reviews/api/node-core-library.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -616,11 +616,8 @@ export interface IRealNodeModulePathResolverOptions {

// @public (undocumented)
export interface IRunWithRetriesOptions<TResult> {
// (undocumented)
action: () => Promise<TResult> | TResult;
// (undocumented)
action: (retryCount: number) => Promise<TResult> | TResult;
maxRetries: number;
// (undocumented)
retryDelayMs?: number;
}

Expand Down
20 changes: 16 additions & 4 deletions libraries/node-core-library/src/Async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,20 @@ export interface IAsyncParallelismOptions {
* @public
*/
export interface IRunWithRetriesOptions<TResult> {
action: () => Promise<TResult> | TResult;
/**
* The action to be performed. The action is repeatedly executed until it completes without throwing or the
* maximum number of retries is reached.
*
* @param retryCount - The number of times the action has been retried.
*/
action: (retryCount: number) => Promise<TResult> | TResult;
/**
* The maximum number of times the action should be retried.
*/
maxRetries: number;
/**
* The delay in milliseconds between retries.
*/
retryDelayMs?: number;
}

Expand Down Expand Up @@ -313,13 +325,13 @@ export class Async {
maxRetries,
retryDelayMs = 0
}: IRunWithRetriesOptions<TResult>): Promise<TResult> {
let retryCounter: number = 0;
let retryCount: number = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
try {
return await action();
return await action(retryCount);
} catch (e) {
if (++retryCounter > maxRetries) {
if (++retryCount > maxRetries) {
throw e;
} else if (retryDelayMs > 0) {
await Async.sleepAsync(retryDelayMs);
Expand Down
Loading