@celljs/retry 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +192 -0
- package/cell.yml +0 -0
- package/lib/common/back-off/back-off-policy.d.ts +7 -0
- package/lib/common/back-off/back-off-policy.d.ts.map +1 -0
- package/lib/common/back-off/back-off-policy.js +56 -0
- package/lib/common/back-off/back-off-policy.js.map +1 -0
- package/lib/common/back-off/back-off-policy.spec.d.ts +2 -0
- package/lib/common/back-off/back-off-policy.spec.d.ts.map +1 -0
- package/lib/common/back-off/back-off-policy.spec.js +209 -0
- package/lib/common/back-off/back-off-policy.spec.js.map +1 -0
- package/lib/common/back-off/back-off-protocol.d.ts +12 -0
- package/lib/common/back-off/back-off-protocol.d.ts.map +1 -0
- package/lib/common/back-off/back-off-protocol.js +5 -0
- package/lib/common/back-off/back-off-protocol.js.map +1 -0
- package/lib/common/back-off/index.d.ts +3 -0
- package/lib/common/back-off/index.d.ts.map +1 -0
- package/lib/common/back-off/index.js +19 -0
- package/lib/common/back-off/index.js.map +1 -0
- package/lib/common/error/index.d.ts +2 -0
- package/lib/common/error/index.d.ts.map +1 -0
- package/lib/common/error/index.js +18 -0
- package/lib/common/error/index.js.map +1 -0
- package/lib/common/error/retry-error.d.ts +4 -0
- package/lib/common/error/retry-error.d.ts.map +1 -0
- package/lib/common/error/retry-error.js +8 -0
- package/lib/common/error/retry-error.js.map +1 -0
- package/lib/common/index.d.ts +5 -0
- package/lib/common/index.d.ts.map +1 -0
- package/lib/common/index.js +21 -0
- package/lib/common/index.js.map +1 -0
- package/lib/common/module.d.ts +4 -0
- package/lib/common/module.d.ts.map +1 -0
- package/lib/common/module.js +6 -0
- package/lib/common/module.js.map +1 -0
- package/lib/common/policy/index.d.ts +3 -0
- package/lib/common/policy/index.d.ts.map +1 -0
- package/lib/common/policy/index.js +19 -0
- package/lib/common/policy/index.js.map +1 -0
- package/lib/common/policy/policy-protocol.d.ts +6 -0
- package/lib/common/policy/policy-protocol.d.ts.map +1 -0
- package/lib/common/policy/policy-protocol.js +5 -0
- package/lib/common/policy/policy-protocol.js.map +1 -0
- package/lib/common/policy/retry-policy.d.ts +6 -0
- package/lib/common/policy/retry-policy.d.ts.map +1 -0
- package/lib/common/policy/retry-policy.js +49 -0
- package/lib/common/policy/retry-policy.js.map +1 -0
- package/lib/common/policy/retry-policy.spec.d.ts +2 -0
- package/lib/common/policy/retry-policy.spec.d.ts.map +1 -0
- package/lib/common/policy/retry-policy.spec.js +102 -0
- package/lib/common/policy/retry-policy.spec.js.map +1 -0
- package/lib/common/retry/index.d.ts +3 -0
- package/lib/common/retry/index.d.ts.map +1 -0
- package/lib/common/retry/index.js +19 -0
- package/lib/common/retry/index.js.map +1 -0
- package/lib/common/retry/retry-operations.d.ts +15 -0
- package/lib/common/retry/retry-operations.d.ts.map +1 -0
- package/lib/common/retry/retry-operations.js +94 -0
- package/lib/common/retry/retry-operations.js.map +1 -0
- package/lib/common/retry/retry-operations.spec.d.ts +2 -0
- package/lib/common/retry/retry-operations.spec.d.ts.map +1 -0
- package/lib/common/retry/retry-operations.spec.js +140 -0
- package/lib/common/retry/retry-operations.spec.js.map +1 -0
- package/lib/common/retry/retry-protocol.d.ts +123 -0
- package/lib/common/retry/retry-protocol.d.ts.map +1 -0
- package/lib/common/retry/retry-protocol.js +5 -0
- package/lib/common/retry/retry-protocol.js.map +1 -0
- package/lib/package.spec.d.ts +1 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +4 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +41 -0
- package/src/common/back-off/back-off-policy.spec.ts +246 -0
- package/src/common/back-off/back-off-policy.ts +43 -0
- package/src/common/back-off/back-off-protocol.ts +14 -0
- package/src/common/back-off/index.ts +2 -0
- package/src/common/error/index.ts +1 -0
- package/src/common/error/retry-error.ts +5 -0
- package/src/common/index.ts +4 -0
- package/src/common/module.ts +4 -0
- package/src/common/policy/index.ts +2 -0
- package/src/common/policy/policy-protocol.ts +7 -0
- package/src/common/policy/retry-policy.spec.ts +125 -0
- package/src/common/policy/retry-policy.ts +39 -0
- package/src/common/retry/index.ts +2 -0
- package/src/common/retry/retry-operations.spec.ts +154 -0
- package/src/common/retry/retry-operations.ts +78 -0
- package/src/common/retry/retry-protocol.ts +133 -0
- package/src/package.spec.ts +4 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Component } from '@celljs/core';
|
|
2
|
+
import { RetryContext } from '../retry/retry-protocol';
|
|
3
|
+
import { RetryPolicy } from './policy-protocol';
|
|
4
|
+
|
|
5
|
+
@Component(RetryPolicy)
|
|
6
|
+
export class DefaultRetryPolicy implements RetryPolicy {
|
|
7
|
+
|
|
8
|
+
async canRetry(ctx: RetryContext): Promise<boolean> {
|
|
9
|
+
const { startTime, timeout, maxAttempts, retryCount, lastError, retryErrors, notRetryErrors } = ctx;
|
|
10
|
+
if (timeout !== undefined && timeout < Date.now() - startTime) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
if (maxAttempts !== undefined && maxAttempts < retryCount) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (lastError) {
|
|
17
|
+
if (notRetryErrors.length) {
|
|
18
|
+
for (const item of notRetryErrors) {
|
|
19
|
+
if (lastError instanceof item) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (retryErrors.length) {
|
|
25
|
+
let includes = false;
|
|
26
|
+
for (const item of retryErrors) {
|
|
27
|
+
if (lastError instanceof item) {
|
|
28
|
+
includes = true;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (!includes) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { coreTestModule } from '@celljs/core/lib/common/test/test-module';
|
|
2
|
+
import { RetryOperationsImpl } from './retry-operations';
|
|
3
|
+
import { RetryOptions, RetryContext, RetryOperations } from './retry-protocol';
|
|
4
|
+
import { RetryError } from '../error';
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
import { autoBind, ContainerFactory, ContainerProvider, ContainerUtil } from '@celljs/core';
|
|
7
|
+
import '../index';
|
|
8
|
+
|
|
9
|
+
describe('RetryOperationsImpl', () => {
|
|
10
|
+
let retryOperations: RetryOperationsImpl;
|
|
11
|
+
|
|
12
|
+
before(() => {
|
|
13
|
+
const container = ContainerFactory.create(coreTestModule, autoBind());
|
|
14
|
+
ContainerProvider.set(container);
|
|
15
|
+
retryOperations = ContainerUtil.get(RetryOperations);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe('initRetryContext', () => {
|
|
19
|
+
it('should initialize RetryContext with provided options', () => {
|
|
20
|
+
const options: RetryOptions<any> = {
|
|
21
|
+
timeout: 5000,
|
|
22
|
+
maxAttempts: 3,
|
|
23
|
+
backOffOptions: { delay: 1000 },
|
|
24
|
+
rollbackError: RetryError,
|
|
25
|
+
exhaustedOnly: true,
|
|
26
|
+
retryErrors: [RetryError],
|
|
27
|
+
notRetryErrors: [Error]
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const context: RetryContext = retryOperations['initRetryContext'](options);
|
|
31
|
+
|
|
32
|
+
expect(context.timeout).to.equal(5000);
|
|
33
|
+
expect(context.maxAttempts).to.equal(3);
|
|
34
|
+
expect(context.backOffOptions).to.deep.equal({ delay: 1000 });
|
|
35
|
+
expect(context.rollbackError).to.equal(RetryError);
|
|
36
|
+
expect(context.exhaustedOnly).to.be.true;
|
|
37
|
+
expect(context.retryErrors).to.deep.equal([RetryError]);
|
|
38
|
+
expect(context.notRetryErrors).to.deep.equal([Error]);
|
|
39
|
+
expect(context.retryCount).to.equal(0);
|
|
40
|
+
expect(context.lastError).to.be.undefined;
|
|
41
|
+
expect(context.startTime).to.be.a('number');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should set default values when options are not provided', () => {
|
|
45
|
+
const options: RetryOptions<any> = {};
|
|
46
|
+
|
|
47
|
+
const context: RetryContext = retryOperations['initRetryContext'](options);
|
|
48
|
+
|
|
49
|
+
expect(context.timeout).to.be.undefined;
|
|
50
|
+
expect(context.maxAttempts).to.be.undefined;
|
|
51
|
+
expect(context.backOffOptions).to.be.undefined;
|
|
52
|
+
expect(context.rollbackError).to.be.undefined;
|
|
53
|
+
expect(context.exhaustedOnly).to.be.undefined;
|
|
54
|
+
expect(context.retryErrors).to.deep.equal([]);
|
|
55
|
+
expect(context.notRetryErrors).to.deep.equal([]);
|
|
56
|
+
expect(context.retryCount).to.equal(0);
|
|
57
|
+
expect(context.lastError).to.be.undefined;
|
|
58
|
+
expect(context.startTime).to.be.a('number');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should correctly handle retryErrors and notRetryErrors options', () => {
|
|
62
|
+
const options: RetryOptions<any> = {
|
|
63
|
+
retryErrors: [RetryError],
|
|
64
|
+
notRetryErrors: [Error]
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const context: RetryContext = retryOperations['initRetryContext'](options);
|
|
68
|
+
|
|
69
|
+
expect(context.retryErrors).to.deep.equal([RetryError]);
|
|
70
|
+
expect(context.notRetryErrors).to.deep.equal([Error]);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('handleRetryExhausted', () => {
|
|
75
|
+
it('should return the result of the recovery callback', async () => {
|
|
76
|
+
const recoveryCallback = async () => 2;
|
|
77
|
+
const result = await retryOperations['handleRetryExhausted']({} as RetryContext, recoveryCallback);
|
|
78
|
+
expect(result).to.equal(2);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('should throw the last error if no recovery callback is provided', async () => {
|
|
82
|
+
const error = new Error();
|
|
83
|
+
try {
|
|
84
|
+
await retryOperations['handleRetryExhausted']({ lastError: error } as RetryContext);
|
|
85
|
+
} catch (e) {
|
|
86
|
+
expect(e).to.be.instanceOf(RetryError);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('wrapError', () => {
|
|
92
|
+
it('should return a RetryError', () => {
|
|
93
|
+
const error = new Error();
|
|
94
|
+
const result = retryOperations['wrapError'](error);
|
|
95
|
+
expect(result).to.be.instanceOf(RetryError);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe('execute', () => {
|
|
100
|
+
it('should execute the callback and return its result', async () => {
|
|
101
|
+
const callback = async () => 2;
|
|
102
|
+
const result = await retryOperations.execute(callback);
|
|
103
|
+
expect(result).to.equal(2);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('should retry the callback until the retry policy allows it', async () => {
|
|
107
|
+
let count = 0;
|
|
108
|
+
const callback = async () => {
|
|
109
|
+
count++;
|
|
110
|
+
if (count < 3) {
|
|
111
|
+
throw new Error();
|
|
112
|
+
}
|
|
113
|
+
return 2;
|
|
114
|
+
};
|
|
115
|
+
const result = await retryOperations.execute(callback, { maxAttempts: 5 });
|
|
116
|
+
expect(result).to.equal(2);
|
|
117
|
+
expect(count).to.equal(3);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should throw the last error if the retry policy does not allow retry', async () => {
|
|
121
|
+
const callback = async () => {
|
|
122
|
+
throw new Error();
|
|
123
|
+
};
|
|
124
|
+
try {
|
|
125
|
+
await retryOperations.execute(callback, { maxAttempts: 1 });
|
|
126
|
+
} catch (e) {
|
|
127
|
+
expect(e).to.be.instanceOf(RetryError);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('should handle the retry exhausted case', async () => {
|
|
132
|
+
const callback = async () => {
|
|
133
|
+
throw new Error();
|
|
134
|
+
};
|
|
135
|
+
try {
|
|
136
|
+
await retryOperations.execute(callback, { maxAttempts: 2, recoveryCallback: async () => 2 });
|
|
137
|
+
} catch (e) {
|
|
138
|
+
expect(e).to.equal(2);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should handle the retry exhausted case with no recovery callback', async () => {
|
|
143
|
+
const callback = async () => {
|
|
144
|
+
throw new Error();
|
|
145
|
+
};
|
|
146
|
+
try {
|
|
147
|
+
await retryOperations.execute(callback, { maxAttempts: 2 });
|
|
148
|
+
} catch (e) {
|
|
149
|
+
expect(e).to.be.instanceOf(RetryError);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Autowired, Component, Logger } from '@celljs/core';
|
|
2
|
+
import { RecoveryCallback, RetryCallback, RetryContext, RetryOperations, RetryOptions } from './retry-protocol';
|
|
3
|
+
import { RetryError } from '../error';
|
|
4
|
+
import { BackOffPolicy } from '../back-off/back-off-protocol';
|
|
5
|
+
import { RetryPolicy } from '../policy/policy-protocol';
|
|
6
|
+
|
|
7
|
+
@Component(RetryOperations)
|
|
8
|
+
export class RetryOperationsImpl implements RetryOperations {
|
|
9
|
+
|
|
10
|
+
@Autowired(RetryPolicy)
|
|
11
|
+
protected readonly retryPolicy: RetryPolicy;
|
|
12
|
+
|
|
13
|
+
@Autowired(BackOffPolicy)
|
|
14
|
+
protected readonly backOffPolicy: BackOffPolicy;
|
|
15
|
+
|
|
16
|
+
@Autowired(Logger)
|
|
17
|
+
protected readonly logger: Logger;
|
|
18
|
+
|
|
19
|
+
protected initRetryContext(options: RetryOptions<any>): RetryContext {
|
|
20
|
+
return {
|
|
21
|
+
timeout: options.timeout,
|
|
22
|
+
maxAttempts: options.maxAttempts,
|
|
23
|
+
backOffOptions: options.backOffOptions,
|
|
24
|
+
rollbackError: options.rollbackError,
|
|
25
|
+
retryCount: 0,
|
|
26
|
+
lastError: undefined,
|
|
27
|
+
exhaustedOnly: options.exhaustedOnly,
|
|
28
|
+
startTime: Date.now(),
|
|
29
|
+
retryErrors: options.retryErrors ?? [],
|
|
30
|
+
notRetryErrors: options.notRetryErrors ?? []
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
protected async handleRetryExhausted<T>(ctx: RetryContext, recoveryCallback?: RecoveryCallback<T>): Promise<T> {
|
|
35
|
+
if (recoveryCallback && !ctx.noRecovery) {
|
|
36
|
+
try {
|
|
37
|
+
return recoveryCallback(ctx);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
throw this.wrapError(ctx.lastError);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
throw this.wrapError(ctx.lastError);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
protected wrapError(error?: Error): RetryError {
|
|
46
|
+
return new RetryError('Error occurred during retry: ' + error?.message);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async execute<T>(callback: RetryCallback<T>, options: RetryOptions<T> = {}): Promise<T> {
|
|
50
|
+
const retryContext = this.initRetryContext(options);
|
|
51
|
+
const backOffContext = await this.backOffPolicy.start(retryContext);
|
|
52
|
+
const retryPolicy = this.retryPolicy;
|
|
53
|
+
try {
|
|
54
|
+
while (await retryPolicy.canRetry(retryContext)) {
|
|
55
|
+
try {
|
|
56
|
+
return await callback(retryContext);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
retryContext.lastError = error;
|
|
59
|
+
retryContext.retryCount++;
|
|
60
|
+
if (await retryPolicy.canRetry(retryContext) && !retryContext.exhaustedOnly) {
|
|
61
|
+
await this.backOffPolicy.backOff(backOffContext);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.logger.debug('Checking for rethrow: count=' + retryContext.retryCount);
|
|
65
|
+
if (retryContext.rollbackError && error instanceof retryContext.rollbackError) {
|
|
66
|
+
this.logger.debug('Rethrow in retry for policy: count=' + retryContext.retryCount);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
this.logger.debug('Retry failed last attempt: count=' + retryContext.retryCount);
|
|
72
|
+
return this.handleRetryExhausted<T>(retryContext, options.recoveryCallback);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
throw this.wrapError(error);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { CustomError } from '@celljs/core';
|
|
2
|
+
|
|
3
|
+
export const RetryOperations = Symbol('RetryOperations');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Represents the context for retrying an operation.
|
|
7
|
+
*/
|
|
8
|
+
export interface RetryContext {
|
|
9
|
+
startTime: number;
|
|
10
|
+
maxAttempts?: number;
|
|
11
|
+
timeout?: number;
|
|
12
|
+
backOffOptions?: BackOffOptions;
|
|
13
|
+
rollbackError?: typeof CustomError;
|
|
14
|
+
retryCount: number;
|
|
15
|
+
lastError?: Error;
|
|
16
|
+
exhaustedOnly?: boolean;
|
|
17
|
+
noRecovery?: boolean;
|
|
18
|
+
retryErrors: (typeof CustomError)[];
|
|
19
|
+
notRetryErrors: (typeof CustomError)[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RetryCallback<T> {
|
|
23
|
+
(): Promise<T>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface RetryCallback<T> {
|
|
27
|
+
(ctx: RetryContext): Promise<T>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface RecoveryCallback<T> {
|
|
31
|
+
(ctx: RetryContext): Promise<T>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface FixedBackoffOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Set the back off period in milliseconds. Cannot be < 1. Default value is 1000ms.
|
|
37
|
+
*/
|
|
38
|
+
period: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface BackOffOptions {
|
|
42
|
+
/**
|
|
43
|
+
* A canonical backoff period. Used as an initial value in the exponential case, and
|
|
44
|
+
* as a minimum value in the uniform case.
|
|
45
|
+
* The initial or canonical backoff period in milliseconds.
|
|
46
|
+
*/
|
|
47
|
+
delay?: number | (() => Promise<number>);
|
|
48
|
+
/**
|
|
49
|
+
* If positive, then used as a multiplier for generating the next delay for backoff.
|
|
50
|
+
* A multiplier to use to calculate the next backoff delay.
|
|
51
|
+
*/
|
|
52
|
+
multiplier?: number;
|
|
53
|
+
/**
|
|
54
|
+
* The maximum wait in milliseconds between retries. If less than {@link delay}
|
|
55
|
+
* then a default value is applied depending on the resulting policy.
|
|
56
|
+
* The maximum wait between retries in milliseconds.
|
|
57
|
+
*/
|
|
58
|
+
maxDelay?: number | (() => Promise<number>);
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* In the exponential case ({@link multiplier} > 0) set this to true to have the
|
|
62
|
+
* backoff delays randomized, so that the maximum delay is multiplier times the
|
|
63
|
+
* previous delay and the distribution is uniform between the two values.
|
|
64
|
+
* The flag to signal randomization is required.
|
|
65
|
+
*/
|
|
66
|
+
random?: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* RetryOptions interface represents the options for retrying a task.
|
|
71
|
+
*
|
|
72
|
+
* @template T - The type of the recovery callback parameter.
|
|
73
|
+
* @property {RecoveryCallback<T>} recoveryCallback - The callback function to recover from an error.
|
|
74
|
+
* @property {typeof CustomError} rollbackError - The custom error class to rollback the operation.
|
|
75
|
+
* @property {number} maxAttempts - The maximum number of attempts to retry the task.
|
|
76
|
+
* @property {number} timeout - The timeout duration in milliseconds for each attempt.
|
|
77
|
+
* @property {boolean} exhaustedOnly - Indicates whether to retry only when all attempts are exhausted.
|
|
78
|
+
* @property {BackOffOptions} backOffOptions - The options for exponential backoff strategy.
|
|
79
|
+
* @property {(typeof CustomError)[]} retryErrors - The list of custom error classes to retry on.
|
|
80
|
+
* @property {(typeof CustomError)[]} notRetryErrors - The list of custom error classes to not retry on.
|
|
81
|
+
*/
|
|
82
|
+
export interface RetryOptions<T = any> {
|
|
83
|
+
recoveryCallback?: RecoveryCallback<T>;
|
|
84
|
+
rollbackError?: typeof CustomError;
|
|
85
|
+
maxAttempts?: number;
|
|
86
|
+
timeout?: number;
|
|
87
|
+
exhaustedOnly?: boolean;
|
|
88
|
+
backOffOptions?: BackOffOptions;
|
|
89
|
+
retryErrors?: (typeof CustomError)[];
|
|
90
|
+
notRetryErrors?: (typeof CustomError)[];
|
|
91
|
+
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* RetryOperations interface represents the operations for retrying a task with different options.
|
|
96
|
+
*/
|
|
97
|
+
export interface RetryOperations {
|
|
98
|
+
/**
|
|
99
|
+
* Execute the callback function with the given options.
|
|
100
|
+
*
|
|
101
|
+
* @template T - The type of the return value of the callback function.
|
|
102
|
+
* @param {RetryCallback<T>} callback - The callback function to execute.
|
|
103
|
+
* @param {RetryOptions<T>} options - The options for retrying the task.
|
|
104
|
+
* @returns {Promise<T>} - The result of the callback function
|
|
105
|
+
* @throws {RetryError} - If an error occurred during retry.
|
|
106
|
+
* @example
|
|
107
|
+
* const result = await retryOperations.execute(async () => {
|
|
108
|
+
* // do something
|
|
109
|
+
* }, { maxAttempts: 3, timeout: 1000 });
|
|
110
|
+
* console.log(result);
|
|
111
|
+
* @example
|
|
112
|
+
* const result = await retryOperations.execute(async () => {
|
|
113
|
+
* // do something
|
|
114
|
+
* }, { maxAttempts: 3, timeout: 1000, backOffOptions: { delay: 1000 } });
|
|
115
|
+
* console.log(result);
|
|
116
|
+
* @example
|
|
117
|
+
* const result = await retryOperations.execute(async () => {
|
|
118
|
+
* // do something
|
|
119
|
+
* }, { maxAttempts: 3, timeout: 1000, backOffOptions: { delay: 1000, multiplier: 2 } });
|
|
120
|
+
* console.log(result);
|
|
121
|
+
* @example
|
|
122
|
+
* const result = await retryOperations.execute(async () => {
|
|
123
|
+
* // do something
|
|
124
|
+
* }, { maxAttempts: 3, timeout: 1000, backOffOptions: { delay: 1000, multiplier: 2, random: true } });
|
|
125
|
+
* console.log(result);
|
|
126
|
+
* @example
|
|
127
|
+
* const result = await retryOperations.execute(async () => {
|
|
128
|
+
* // do something
|
|
129
|
+
* }, { maxAttempts: 3, timeout: 1000, backOffOptions: { delay: 1000, multiplier: 2, random: true, maxDelay: 5000 } });
|
|
130
|
+
* console.log(result);
|
|
131
|
+
*/
|
|
132
|
+
execute<T>(callback: RetryCallback<T>, options?: RetryOptions<T>): Promise<T>;
|
|
133
|
+
}
|