/
retry.ts
67 lines (61 loc) · 1.81 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
/*
* 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 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 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 class RetryService extends FtrService {
private readonly config = this.ctx.getService('config');
private readonly log = this.ctx.getService('log');
public async tryForTime<T>(
timeout: number,
block: () => Promise<T>,
onFailureBlock?: () => Promise<T>
) {
return await retryForSuccess(this.log, {
timeout,
methodName: 'retry.tryForTime',
block,
onFailureBlock,
});
}
public async try<T>(block: () => Promise<T>, onFailureBlock?: () => Promise<T>) {
return await retryForSuccess(this.log, {
timeout: this.config.get('timeouts.try'),
methodName: 'retry.try',
block,
onFailureBlock,
});
}
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,
});
}
}