@creejs/commons-retrier 1.0.0 → 2.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/package.json +5 -8
- package/types/alway-task.d.ts +27 -0
- package/types/constants.d.ts +3 -0
- package/types/event.d.ts +9 -0
- package/types/index.d.ts +25 -0
- package/types/policy/factor-increase-policy.d.ts +12 -0
- package/types/policy/fixed-increase-policy.d.ts +12 -0
- package/types/policy/fixed-interval-policy.d.ts +12 -0
- package/types/policy/shuttle-policy.d.ts +13 -0
- package/types/policy.d.ts +52 -0
- package/types/retrier-factory.d.ts +85 -0
- package/types/retrier.d.ts +233 -0
- package/types/task.d.ts +28 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@creejs/commons-retrier",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Common Utils About Task Retrying",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"private": false,
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"types/",
|
|
11
11
|
"README.md"
|
|
12
12
|
],
|
|
13
|
+
"types": "types/index.d.ts",
|
|
13
14
|
"publishConfig": {
|
|
14
15
|
"access": "public"
|
|
15
16
|
},
|
|
@@ -19,16 +20,12 @@
|
|
|
19
20
|
},
|
|
20
21
|
"scripts": {
|
|
21
22
|
"dts": "tsc",
|
|
22
|
-
"generate-docs": "node_modules/.bin/jsdoc -c ./jsdoc.json"
|
|
23
|
+
"generate-docs": "../../node_modules/.bin/jsdoc -c ./jsdoc.json"
|
|
23
24
|
},
|
|
24
25
|
"author": "rodney.vin@gmail.com",
|
|
25
26
|
"license": "Apache-2.0",
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"@creejs/commons-
|
|
28
|
-
"@creejs/commons-
|
|
29
|
-
},
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"better-docs": "^2.7.3",
|
|
32
|
-
"jsdoc": "^4.0.4"
|
|
28
|
+
"@creejs/commons-events": "^2.0.0",
|
|
29
|
+
"@creejs/commons-lang": "^2.0.0"
|
|
33
30
|
}
|
|
34
31
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export = AlwaysTask;
|
|
2
|
+
declare class AlwaysTask extends Task {
|
|
3
|
+
/**
|
|
4
|
+
* Checks if the given task is an instance of AlwaysTask.
|
|
5
|
+
* @param {*} task - The task to check.
|
|
6
|
+
* @returns {boolean} True if the task is an instance of AlwaysTask, false otherwise.
|
|
7
|
+
*/
|
|
8
|
+
static isAlwaysTask(task: any): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Creates an AlwaysTask instance.
|
|
11
|
+
* @param {Retrier} retrier - The retrier instance to use for retry logic
|
|
12
|
+
* @param {Function} task - The task function to execute
|
|
13
|
+
* @param {boolean} resetRetryPolicyAfterSuccess - Whether to reset retry policy after successful execution
|
|
14
|
+
*/
|
|
15
|
+
constructor(retrier: Retrier, task: Function, resetRetryPolicyAfterSuccess: boolean);
|
|
16
|
+
resetPolicy: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Executes the task with the given retry parameters.
|
|
19
|
+
* @param {number} retries - The number of retries attempted so far.
|
|
20
|
+
* @param {number} latence - The current latency ms.
|
|
21
|
+
* @param {number} nextInterval - The next interval ms.
|
|
22
|
+
* @returns {Promise<*>} The result of the task execution.
|
|
23
|
+
*/
|
|
24
|
+
execute(retries: number, latence: number, nextInterval: number): Promise<any>;
|
|
25
|
+
}
|
|
26
|
+
import Task = require("./task");
|
|
27
|
+
import Retrier = require("./retrier");
|
package/types/event.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const Start: "start";
|
|
2
|
+
export const Retry: "retry";
|
|
3
|
+
export const Success: "success";
|
|
4
|
+
export const Failure: "failure";
|
|
5
|
+
export const Timeout: "timeout";
|
|
6
|
+
export const TaskTimeout: "task-timeout";
|
|
7
|
+
export const Stop: "stop";
|
|
8
|
+
export const Completed: "complete";
|
|
9
|
+
export const MaxRetries: "max-retries";
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
declare const _exports: {
|
|
2
|
+
name: typeof RetrierFactory.name;
|
|
3
|
+
infinite: typeof RetrierFactory.infinite;
|
|
4
|
+
times: typeof RetrierFactory.times;
|
|
5
|
+
maxRetries: typeof RetrierFactory.maxRetries;
|
|
6
|
+
min: typeof RetrierFactory.min;
|
|
7
|
+
max: typeof RetrierFactory.max;
|
|
8
|
+
range: typeof RetrierFactory.range;
|
|
9
|
+
fixedInterval: typeof RetrierFactory.fixedInterval;
|
|
10
|
+
fixedIncrease: typeof RetrierFactory.fixedIncrease;
|
|
11
|
+
factorIncrease: typeof RetrierFactory.factorIncrease;
|
|
12
|
+
shuttleInterval: typeof RetrierFactory.shuttleInterval;
|
|
13
|
+
timeout: typeof RetrierFactory.timeout;
|
|
14
|
+
taskTimeout: typeof RetrierFactory.taskTimeout;
|
|
15
|
+
start: typeof RetrierFactory.start;
|
|
16
|
+
Policy: typeof Policy;
|
|
17
|
+
Retrier: typeof Retrier;
|
|
18
|
+
Event: typeof Event;
|
|
19
|
+
RetrierFactory: typeof RetrierFactory;
|
|
20
|
+
};
|
|
21
|
+
export = _exports;
|
|
22
|
+
import RetrierFactory = require("./retrier-factory");
|
|
23
|
+
import Policy = require("./policy");
|
|
24
|
+
import Retrier = require("./retrier");
|
|
25
|
+
import Event = require("./event");
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export = FactoreIncreasePolicy;
|
|
2
|
+
declare class FactoreIncreasePolicy extends Policy {
|
|
3
|
+
/**
|
|
4
|
+
* each call to _next() increases the interval by lastInterval * factor
|
|
5
|
+
* @param {number} factor - the increasement factor, >= 1
|
|
6
|
+
*/
|
|
7
|
+
constructor(factor: number);
|
|
8
|
+
_factor: number;
|
|
9
|
+
set factor(factor: number);
|
|
10
|
+
get factor(): number;
|
|
11
|
+
}
|
|
12
|
+
import Policy = require("../policy");
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export = FixedIncreasePolicy;
|
|
2
|
+
declare class FixedIncreasePolicy extends Policy {
|
|
3
|
+
/**
|
|
4
|
+
* each call to _next() increases the interval by "increasement".
|
|
5
|
+
* @param {number} increasement - The fixed interval (in milliseconds) between retry attempts.
|
|
6
|
+
*/
|
|
7
|
+
constructor(increasement: number);
|
|
8
|
+
_increasement: number;
|
|
9
|
+
set increasement(increasement: number);
|
|
10
|
+
get increasement(): number;
|
|
11
|
+
}
|
|
12
|
+
import Policy = require("../policy");
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export = FixedIntervalPolicy;
|
|
2
|
+
declare class FixedIntervalPolicy extends Policy {
|
|
3
|
+
/**
|
|
4
|
+
* Creates a fixed interval retry policy with the specified interval.
|
|
5
|
+
* @param {number} interval - The fixed interval (in milliseconds) between retry attempts.
|
|
6
|
+
*/
|
|
7
|
+
constructor(interval: number);
|
|
8
|
+
_interval: number;
|
|
9
|
+
set interval(interval: number);
|
|
10
|
+
get interval(): number;
|
|
11
|
+
}
|
|
12
|
+
import Policy = require("../policy");
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export = ShuttlePolicy;
|
|
2
|
+
declare class ShuttlePolicy extends Policy {
|
|
3
|
+
/**
|
|
4
|
+
* the inteval value shuttles between min and max
|
|
5
|
+
* @param {number} stepLength - the step length to change
|
|
6
|
+
*/
|
|
7
|
+
constructor(stepLength: number);
|
|
8
|
+
_stepLength: number;
|
|
9
|
+
increasement: number;
|
|
10
|
+
set stepLength(stepLength: number);
|
|
11
|
+
get stepLength(): number;
|
|
12
|
+
}
|
|
13
|
+
import Policy = require("../policy");
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export = Policy;
|
|
2
|
+
declare class Policy {
|
|
3
|
+
_min: number;
|
|
4
|
+
_max: number;
|
|
5
|
+
_nextInterval: number;
|
|
6
|
+
/**
|
|
7
|
+
* Copies settings to target policy.
|
|
8
|
+
* @param {Policy} targetPolicy - The policy to modify.
|
|
9
|
+
*/
|
|
10
|
+
copyPolicySetting(targetPolicy: Policy): void;
|
|
11
|
+
/**
|
|
12
|
+
* Sets a fixed interval retry policy.
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Sets the minimum and maximum intervals for retries.
|
|
17
|
+
* @param {number} min - Minimum delay in milliseconds (must be positive and less than max)
|
|
18
|
+
* @param {number} max - Maximum delay in milliseconds (must be positive and greater than min)
|
|
19
|
+
* @returns {this} Returns the Retrier instance for chaining
|
|
20
|
+
* @throws {Error} If min is not less than max or if values are not positive
|
|
21
|
+
*/
|
|
22
|
+
range(min: number, max: number): this;
|
|
23
|
+
/**
|
|
24
|
+
* Sets the minimum retry delay in milliseconds.
|
|
25
|
+
* 1. will change currentInterval to min
|
|
26
|
+
* @param {number} min - The minimum delay (must be positive and less than max).
|
|
27
|
+
* @returns {this} The retrier instance for chaining.
|
|
28
|
+
* @throws {Error} If min is not positive or is greater than/equal to max.
|
|
29
|
+
*/
|
|
30
|
+
min(min: number): this;
|
|
31
|
+
/**
|
|
32
|
+
* Sets the maximum retry retry delay in milliseconds.
|
|
33
|
+
* @param {number} max - The maximum delay (must be positive and greater than min).
|
|
34
|
+
* @throws {Error} If max is not greater than min.
|
|
35
|
+
* @returns {this} The retrier instance for chaining.
|
|
36
|
+
*/
|
|
37
|
+
max(max: number): this;
|
|
38
|
+
reset(): this;
|
|
39
|
+
/**
|
|
40
|
+
* Interval ms of next execution
|
|
41
|
+
* @returns {number}
|
|
42
|
+
* @throws {Error} Always throws "Not Implemented Yet" error.
|
|
43
|
+
*/
|
|
44
|
+
generate(): number;
|
|
45
|
+
_increase(): number;
|
|
46
|
+
/**
|
|
47
|
+
* subclass should implement this method
|
|
48
|
+
* @returns {number} The interval in milliseconds to wait before the next retry attempt.
|
|
49
|
+
* @protected
|
|
50
|
+
*/
|
|
51
|
+
protected _next(): number;
|
|
52
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new Retrier instance with the specified name.
|
|
3
|
+
* @param {string} name - The name to assign to the retrier.
|
|
4
|
+
* @returns {Retrier} A new Retrier instance with the given name.
|
|
5
|
+
*/
|
|
6
|
+
export function name(name: string): Retrier;
|
|
7
|
+
/**
|
|
8
|
+
* Creates and returns a Retrier instance configured for infinite retries.
|
|
9
|
+
* @returns {Retrier} A Retrier instance with infinite retry behavior.
|
|
10
|
+
*/
|
|
11
|
+
export function infinite(): Retrier;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a retrier configured to attempt an operation a specified number of times.
|
|
14
|
+
* @param {number} times - The maximum number of retry attempts.
|
|
15
|
+
* @returns {Retrier} A configured Retrier instance with the specified max retries.
|
|
16
|
+
*/
|
|
17
|
+
export function times(times: number): Retrier;
|
|
18
|
+
/**
|
|
19
|
+
* Alias for times.
|
|
20
|
+
* @param {number} maxRetries - The maximum number of retry attempts.
|
|
21
|
+
* @returns {Retrier} A configured Retrier instance with the specified max retries.
|
|
22
|
+
*/
|
|
23
|
+
export function maxRetries(maxRetries: number): Retrier;
|
|
24
|
+
/**
|
|
25
|
+
* Sets the minimum Interval for the retrier and returns the instance.
|
|
26
|
+
* @param {number} min - The minimum Interval in milliseconds.
|
|
27
|
+
* @returns {Retrier} The retrier instance with updated minimum Interval.
|
|
28
|
+
*/
|
|
29
|
+
export function min(min: number): Retrier;
|
|
30
|
+
/**
|
|
31
|
+
* Sets the maximum Interval for the retrier and returns the instance.
|
|
32
|
+
* @param {number} max - The maximum Interval in milliseconds.
|
|
33
|
+
* @returns {Retrier} The retrier instance with updated maximum Interval.
|
|
34
|
+
*/
|
|
35
|
+
export function max(max: number): Retrier;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a retrier with the specified Interval range.
|
|
38
|
+
* @param {number} min - Minimum Interval.
|
|
39
|
+
* @param {number} max - Maximum Interval.
|
|
40
|
+
* @returns {Retrier} A new Retrier instance configured with specified Interval range.
|
|
41
|
+
*/
|
|
42
|
+
export function range(min: number, max: number): Retrier;
|
|
43
|
+
/**
|
|
44
|
+
* Creates a retrier with a fixed interval between attempts.
|
|
45
|
+
* @param {number} fixedInterval - The fixed interval in milliseconds between retry attempts.
|
|
46
|
+
* @returns {Retrier} A new Retrier instance configured with the specified fixed interval.
|
|
47
|
+
*/
|
|
48
|
+
export function fixedInterval(fixedInterval: number): Retrier;
|
|
49
|
+
/**
|
|
50
|
+
* Creates a retrier with a fixed increase strategy.
|
|
51
|
+
* @param {number} increasement - The fixed amount to increase on each retry.
|
|
52
|
+
* @returns {Retrier} A retrier instance configured with fixed increase.
|
|
53
|
+
*/
|
|
54
|
+
export function fixedIncrease(increasement: number): Retrier;
|
|
55
|
+
/**
|
|
56
|
+
* Creates a new Retrier instance with factor-increase strategy.
|
|
57
|
+
* @param {number} factor - The factor by which to increase the interval on each retry.
|
|
58
|
+
* @returns {Retrier} A new Retrier instance with factor-increase strategy.
|
|
59
|
+
*/
|
|
60
|
+
export function factorIncrease(factor: number): Retrier;
|
|
61
|
+
/**
|
|
62
|
+
* Creates a Retrier instance with a shuttle-interval strategt.
|
|
63
|
+
* @param {number} stepLength - The interval step length of each change
|
|
64
|
+
* @returns {Retrier} A configured Retrier instance with shuttle-interval strategt.
|
|
65
|
+
*/
|
|
66
|
+
export function shuttleInterval(stepLength: number): Retrier;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a Retrier instance with a total-opertion timeout.
|
|
69
|
+
* @param {number} timeout - The timeout value in milliseconds.
|
|
70
|
+
* @returns {Retrier} A Retrier instance configured with the given timeout.
|
|
71
|
+
*/
|
|
72
|
+
export function timeout(timeout: number): Retrier;
|
|
73
|
+
/**
|
|
74
|
+
* Creates a retrier instance with a single-task timeout.
|
|
75
|
+
* @param {number} timeout - The timeout duration in milliseconds for the retrier task.
|
|
76
|
+
* @returns {Retrier} A Retrier instance configured with the given task timeout.
|
|
77
|
+
*/
|
|
78
|
+
export function taskTimeout(timeout: number): Retrier;
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new Retrier instance, sets the task to be retried, and starts the retry process.
|
|
81
|
+
* @param {Function} task - The asynchronous task function to be retried.
|
|
82
|
+
* @returns {Promise<*>} A promise that resolves when the retry process completes.
|
|
83
|
+
*/
|
|
84
|
+
export function start(task: Function): Promise<any>;
|
|
85
|
+
import Retrier = require("./retrier");
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
export = Retrier;
|
|
2
|
+
/**
|
|
3
|
+
* @extends EventEmitter
|
|
4
|
+
*/
|
|
5
|
+
declare class Retrier {
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new Retrier instance with a fixed interval policy.
|
|
8
|
+
* @param {number} [fixedInterval=1000] - The fixed interval in milliseconds between retry attempts. Defaults to 1000ms if not provided.
|
|
9
|
+
*/
|
|
10
|
+
constructor(fixedInterval?: number);
|
|
11
|
+
/**
|
|
12
|
+
* @type {Policy}
|
|
13
|
+
*/
|
|
14
|
+
_policy: Policy;
|
|
15
|
+
_maxRetries: number;
|
|
16
|
+
_currentRetries: number;
|
|
17
|
+
/**
|
|
18
|
+
* Timetou for total operation
|
|
19
|
+
* @type {number}
|
|
20
|
+
*/
|
|
21
|
+
_timeout: number;
|
|
22
|
+
/**
|
|
23
|
+
* Timetou for single task
|
|
24
|
+
*/
|
|
25
|
+
_taskTimeout: number;
|
|
26
|
+
_name: string;
|
|
27
|
+
/**
|
|
28
|
+
* A Deferred Object as Singal to prevent Task concurrent start
|
|
29
|
+
* @type {{resolve:Function, reject:Function, promise: Promise<*>}|undefined}
|
|
30
|
+
*/
|
|
31
|
+
_taskingFlag: {
|
|
32
|
+
resolve: Function;
|
|
33
|
+
reject: Function;
|
|
34
|
+
promise: Promise<any>;
|
|
35
|
+
} | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* A Deferred Object as Singal to prevent Task concurrent stop
|
|
38
|
+
* @type {{resolve:Function, reject:Function, promise: Promise<*>}|undefined}
|
|
39
|
+
*/
|
|
40
|
+
_breakFlag: {
|
|
41
|
+
resolve: Function;
|
|
42
|
+
reject: Function;
|
|
43
|
+
promise: Promise<any>;
|
|
44
|
+
} | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Reason for break
|
|
47
|
+
* @type {Error|undefined}
|
|
48
|
+
*/
|
|
49
|
+
_breakReason: Error | undefined;
|
|
50
|
+
get running(): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Sets the name of the retrier.
|
|
53
|
+
* @param {string} retrierName - The name to assign to the retrier.
|
|
54
|
+
* @returns {this} The retrier instance for chaining.
|
|
55
|
+
*/
|
|
56
|
+
name(retrierName: string): this;
|
|
57
|
+
/**
|
|
58
|
+
* Sets the retry attempts to be infinite by setting max retries to maximum safe integer.
|
|
59
|
+
* @returns {Object} The retrier instance for chaining.
|
|
60
|
+
*/
|
|
61
|
+
infinite(): Object;
|
|
62
|
+
/**
|
|
63
|
+
* Sets the maximum number of retry attempts.
|
|
64
|
+
* @param {number} times - The maximum number of retries.
|
|
65
|
+
* @returns {this} The Retrier instance for chaining.
|
|
66
|
+
*/
|
|
67
|
+
times(times: number): this;
|
|
68
|
+
/**
|
|
69
|
+
* Sets the maximum number of retry attempts.
|
|
70
|
+
* @param {number} maxRetries - The maximum number of retries (must be positive).
|
|
71
|
+
* @returns {this} The retrier instance for chaining.
|
|
72
|
+
*/
|
|
73
|
+
maxRetries(maxRetries: number): this;
|
|
74
|
+
/**
|
|
75
|
+
* Sets the minimum retry delay in milliseconds.
|
|
76
|
+
* @param {number} min - The minimum delay (must be positive and less than max).
|
|
77
|
+
* @returns {this} The retrier instance for chaining.
|
|
78
|
+
* @throws {Error} If min is not positive or is greater than/equal to max.
|
|
79
|
+
*/
|
|
80
|
+
min(min: number): this;
|
|
81
|
+
/**
|
|
82
|
+
* Sets the maximum retry retry delay in milliseconds.
|
|
83
|
+
* @param {number} max - The maximum delay (must be positive and greater than min).
|
|
84
|
+
* @throws {Error} If max is not greater than min.
|
|
85
|
+
* @returns {this} The retrier instance for chaining.
|
|
86
|
+
*/
|
|
87
|
+
max(max: number): this;
|
|
88
|
+
/**
|
|
89
|
+
* Sets the minimum and maximum intervals for retries.
|
|
90
|
+
* @param {number} min - Minimum delay in milliseconds (must be positive and less than max)
|
|
91
|
+
* @param {number} max - Maximum delay in milliseconds (must be positive and greater than min)
|
|
92
|
+
* @returns {Retrier} Returns the Retrier instance for chaining
|
|
93
|
+
* @throws {Error} If min is not less than max or if values are not positive
|
|
94
|
+
*/
|
|
95
|
+
range(min: number, max: number): Retrier;
|
|
96
|
+
/**
|
|
97
|
+
* Sets a fixed interval retry policy.
|
|
98
|
+
* @param {number} fixedInterval - The fixed interval in milliseconds between retries.
|
|
99
|
+
* @returns {Retrier} The Retrier instance for chaining.
|
|
100
|
+
*/
|
|
101
|
+
fixedInterval(fixedInterval: number): Retrier;
|
|
102
|
+
/**
|
|
103
|
+
* Sets a fixed increase policy for retry intervals.
|
|
104
|
+
* @param {number} increasement - The fixed amount to increase the interval by on each retry.
|
|
105
|
+
* @returns {this} The retrier instance for chaining.
|
|
106
|
+
*/
|
|
107
|
+
fixedIncrease(increasement: number): this;
|
|
108
|
+
/**
|
|
109
|
+
* Sets a fixed increase factor for retry delays.
|
|
110
|
+
* @param {number} factor - The multiplier for delay increase between retries.
|
|
111
|
+
* @returns {this} The retrier instance for method chaining.
|
|
112
|
+
*/
|
|
113
|
+
factorIncrease(factor: number): this;
|
|
114
|
+
/**
|
|
115
|
+
* Sets a shuttle retry policy with the given step length.
|
|
116
|
+
* @param {number} stepLength - The interval between retry attempts.
|
|
117
|
+
* @returns {this} The Retrier instance for chaining.
|
|
118
|
+
*/
|
|
119
|
+
shuttleInterval(stepLength: number): this;
|
|
120
|
+
/**
|
|
121
|
+
* Sets the timeout duration for each Task execution.
|
|
122
|
+
* 1. must > 0
|
|
123
|
+
* @param {number} timeout - The timeout duration in milliseconds.
|
|
124
|
+
* @returns {Object} The retrier instance for chaining.
|
|
125
|
+
*/
|
|
126
|
+
taskTimeout(timeout: number): Object;
|
|
127
|
+
/**
|
|
128
|
+
* Sets the timeout duration for all retries.
|
|
129
|
+
* 1. <= 0 - no timeout
|
|
130
|
+
* 2. \> 0 - timeout duration in milliseconds
|
|
131
|
+
* @param {number} timeout - The timeout duration in milliseconds.
|
|
132
|
+
* @returns {Object} The retrier instance for chaining.
|
|
133
|
+
*/
|
|
134
|
+
timeout(timeout: number): Object;
|
|
135
|
+
/**
|
|
136
|
+
* Sets the task function to be retried.
|
|
137
|
+
* @param {Function} task - The function to be executed and retried on failure.
|
|
138
|
+
* @returns {this} Returns the retrier instance for chaining.
|
|
139
|
+
*/
|
|
140
|
+
task(task: Function): this;
|
|
141
|
+
_task: Task | AlwaysTask | undefined;
|
|
142
|
+
/**
|
|
143
|
+
* alias of {@linkcode Retrier.task()}
|
|
144
|
+
* @param {Function} task - The function to be executed and retried
|
|
145
|
+
* @return {this}
|
|
146
|
+
*/
|
|
147
|
+
retry(task: Function): this;
|
|
148
|
+
/**
|
|
149
|
+
* Executes the given task, and never stop
|
|
150
|
+
* 1. if the task fails, will retry it after the interval generated by RetryPolicy
|
|
151
|
+
* 2. if the task succeeds, reset RetryPolicy to Minimum Interval and continue to run the task
|
|
152
|
+
* @param {Function} task - The async function to execute and retry.
|
|
153
|
+
* @param {boolean} [resetAfterSuccess=false] - Whether to reset retry counters after success.
|
|
154
|
+
* @returns {this} The Retrier instance for chaining.
|
|
155
|
+
*/
|
|
156
|
+
always(task: Function, resetAfterSuccess?: boolean): this;
|
|
157
|
+
/**
|
|
158
|
+
* Starts the retry process.
|
|
159
|
+
* @returns {Promise<*>}
|
|
160
|
+
*/
|
|
161
|
+
start(): Promise<any>;
|
|
162
|
+
/**
|
|
163
|
+
* Stops the retrier with an optional reason. If already stopping, returns the existing break promise.
|
|
164
|
+
* @param {Error} [reason] - Optional reason for stopping (defaults to 'Manually Stop' error).
|
|
165
|
+
* @returns {Promise<void>} A promise that resolves when the retrier has fully stopped.
|
|
166
|
+
*/
|
|
167
|
+
stop(reason?: Error): Promise<void>;
|
|
168
|
+
/**
|
|
169
|
+
* Resets the retry policy to its initial state.
|
|
170
|
+
*/
|
|
171
|
+
resetRetryPolicy(): void;
|
|
172
|
+
/**
|
|
173
|
+
* Registers a listener function to be called on "retry" events.
|
|
174
|
+
* @param {Function} listener - The callback function
|
|
175
|
+
* @returns {this}
|
|
176
|
+
*/
|
|
177
|
+
onRetry(listener: Function): this;
|
|
178
|
+
/**
|
|
179
|
+
* Registers a listener for "error" events.
|
|
180
|
+
* @param {Function} listener - The callback function
|
|
181
|
+
* @returns {this}
|
|
182
|
+
*/
|
|
183
|
+
onError(listener: Function): this;
|
|
184
|
+
/**
|
|
185
|
+
* Registers a listener for "failure" events.
|
|
186
|
+
* @param {Function} listener - The callback function
|
|
187
|
+
* @returns {this}
|
|
188
|
+
*/
|
|
189
|
+
onFailure(listener: Function): this;
|
|
190
|
+
/**
|
|
191
|
+
* Registers a listener for "success" events.
|
|
192
|
+
* @param {Function} listener - The callback function
|
|
193
|
+
* @returns {this}
|
|
194
|
+
*/
|
|
195
|
+
onSuccess(listener: Function): this;
|
|
196
|
+
/**
|
|
197
|
+
* Registers a listener for "start" events.
|
|
198
|
+
* @param {Function} listener - The callback function
|
|
199
|
+
* @returns {this}
|
|
200
|
+
*/
|
|
201
|
+
onStart(listener: Function): this;
|
|
202
|
+
/**
|
|
203
|
+
* Registers a listener for "stop" events.
|
|
204
|
+
* @param {Function} listener - The callback function
|
|
205
|
+
* @returns {this}
|
|
206
|
+
*/
|
|
207
|
+
onStop(listener: Function): this;
|
|
208
|
+
/**
|
|
209
|
+
* Registers a listener for "timeout" events.
|
|
210
|
+
* @param {Function} listener - The callback function
|
|
211
|
+
*/
|
|
212
|
+
onTimeout(listener: Function): this;
|
|
213
|
+
/**
|
|
214
|
+
* Registers a listener for "task-timeout" events.
|
|
215
|
+
* @param {Function} listener - The callback function
|
|
216
|
+
* @returns {this}
|
|
217
|
+
*/
|
|
218
|
+
onTaskTimeout(listener: Function): this;
|
|
219
|
+
/**
|
|
220
|
+
* Registers a listener for "completed" events.
|
|
221
|
+
* @param {Function} listener - The callback function
|
|
222
|
+
*/
|
|
223
|
+
onCompleted(listener: Function): this;
|
|
224
|
+
/**
|
|
225
|
+
* Registers a listener for the 'MaxRetries' event.
|
|
226
|
+
* @param {Function} listener - The callback function to be executed when max retries are reached.
|
|
227
|
+
* @returns {this}
|
|
228
|
+
*/
|
|
229
|
+
onMaxRetries(listener: Function): this;
|
|
230
|
+
}
|
|
231
|
+
import Policy = require("./policy");
|
|
232
|
+
import Task = require("./task");
|
|
233
|
+
import AlwaysTask = require("./alway-task");
|
package/types/task.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export = Task;
|
|
2
|
+
declare class Task {
|
|
3
|
+
/**
|
|
4
|
+
* Creates a new Task instance.
|
|
5
|
+
* @param {Retrier} retrier - The retrier instance.
|
|
6
|
+
* @param {Function} task - The function to be executed as the task.
|
|
7
|
+
*/
|
|
8
|
+
constructor(retrier: Retrier, task: Function);
|
|
9
|
+
retrier: Retrier;
|
|
10
|
+
task: Function;
|
|
11
|
+
result: any;
|
|
12
|
+
error: unknown;
|
|
13
|
+
get failed(): boolean;
|
|
14
|
+
get succeeded(): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Executes the task with the given retry parameters.
|
|
17
|
+
* 1. if execution throw error, keep error in this.error
|
|
18
|
+
* 2. if execution return value, keep it in this.result
|
|
19
|
+
* 3. always return Promise<void>
|
|
20
|
+
* @param {number} retries - The number of retries attempted so far.
|
|
21
|
+
* @param {number} latence - The current latency ms.
|
|
22
|
+
* @param {number} nextInterval - The next interval ms.
|
|
23
|
+
* @returns {Promise<void>} The result of the task execution.
|
|
24
|
+
*/
|
|
25
|
+
execute(retries: number, latence: number, nextInterval: number): Promise<void>;
|
|
26
|
+
dispose(): void;
|
|
27
|
+
}
|
|
28
|
+
import Retrier = require("./retrier");
|