Skip to content

Commit

Permalink
[node-core-library] Provide retryCount parameter to actions execute…
Browse files Browse the repository at this point in the history
…d using `Async.runWithRetriesAsync` (#5069)

* Add retryCount parameter to the action provided to Async.runWithRetriesAsync

* Rush change

---------

Co-authored-by: Daniel <[email protected]>
  • Loading branch information
D4N14L and D4N14L authored Jan 8, 2025
1 parent f2faa8c commit c3f31d2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
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

0 comments on commit c3f31d2

Please sign in to comment.