-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
retry.ts
123 lines (114 loc) · 3.62 KB
/
retry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { FtrService } from '../ftr_provider_context';
import { retryForSuccess } from './retry_for_success';
import { retryForTruthy } from './retry_for_truthy';
export interface TryWithRetriesOptions {
retryCount: number;
retryDelay?: number;
timeout?: number;
}
export class RetryService extends FtrService {
private readonly config = this.ctx.getService('config');
private readonly log = this.ctx.getService('log');
/**
* Use to retry block within {timeout} period and return block result.
* @param timeout retrying timeout
* @param block retriable action
* @param onFailureBlock optional action to run before the new retriable action attempt
* @param retryDelay optional delay before the new attempt
* @returns result from retriable action
*/
public async tryForTime<T>(
timeout: number,
block: () => Promise<T>,
onFailureBlock?: () => Promise<T>,
retryDelay?: number
) {
return await retryForSuccess<T>(this.log, {
timeout,
methodName: 'retry.tryForTime',
block,
onFailureBlock,
retryDelay,
});
}
public async try<T>(
block: () => Promise<T>,
onFailureBlock?: () => Promise<T>,
retryDelay?: number
) {
return await retryForSuccess(this.log, {
timeout: this.config.get('timeouts.try'),
methodName: 'retry.try',
block,
onFailureBlock,
retryDelay,
});
}
/**
* Use to wait for block condition to be true
* @param description description for retriable action
* @param timeout retrying timeout
* @param block retriable action
* @param onFailureBlock optional action to run before the new retriable action attempt
*/
public async waitForWithTimeout(
description: string,
timeout: number,
block: () => Promise<boolean>,
onFailureBlock?: () => Promise<any>
) {
await retryForTruthy(this.log, {
timeout,
methodName: 'retry.waitForWithTimeout',
description,
block,
onFailureBlock,
});
}
public async waitFor(
description: string,
block: () => Promise<boolean>,
onFailureBlock?: () => Promise<any>
) {
await retryForTruthy(this.log, {
timeout: this.config.get('timeouts.waitFor'),
methodName: 'retry.waitFor',
description,
block,
onFailureBlock,
});
}
/**
* Use to retry block {options.retryCount} times within {options.timeout} period and return block result
* @param description description for retriable action
* @param block retriable action
* @param options options.retryCount for how many attempts to retry
* @param onFailureBlock optional action to run before the new retriable action attempt
* @returns result from retriable action
*/
public async tryWithRetries<T>(
description: string,
block: () => Promise<T>,
options: TryWithRetriesOptions,
onFailureBlock?: () => Promise<T>
): Promise<T> {
const { retryCount, timeout = this.config.get('timeouts.try'), retryDelay = 200 } = options;
return await retryForSuccess<T>(this.log, {
description,
timeout,
methodName: 'retry.tryWithRetries',
block,
onFailureBlock,
retryDelay,
retryCount,
});
}
}