-
Notifications
You must be signed in to change notification settings - Fork 14
/
StatusPoller.ts
171 lines (144 loc) · 5.41 KB
/
StatusPoller.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import * as core from '@actions/core'
import ApiClient, { BenchmarkStatus, CancellationReason, Flow, UploadStatusError } from "./ApiClient";
import { canceled, err, info, success, warning } from './log';
const WAIT_TIMEOUT_MS = 1000 * 60 * 30 // 30 minutes
const INTERVAL_MS = 10000 // 10 seconds
const TERMINAL_STATUSES = new Set([BenchmarkStatus.SUCCESS, BenchmarkStatus.ERROR, BenchmarkStatus.WARNING, BenchmarkStatus.CANCELED])
const isCompleted = (flow: Flow): boolean => TERMINAL_STATUSES.has(flow.status)
const getCanceledStatusMessage = (reason?: CancellationReason): string => {
switch (reason) {
case CancellationReason.BENCHMARK_DEPENDENCY_FAILED:
case CancellationReason.OVERLAPPING_BENCHMARK:
return 'Skipped';
case CancellationReason.TIMEOUT:
return 'Timeout';
case CancellationReason.INFRA_ERROR:
default:
return 'Canceled';
}
}
const renderError = (errors?: string[]): string => {
if (!errors || errors.length === 0) return '';
return ` (${errors[0]})`;
}
const printFlowResult = (flow: Flow): void => {
if (flow.status === BenchmarkStatus.SUCCESS) {
success(`[Passed] ${flow.name}`)
} else if (flow.status === BenchmarkStatus.ERROR) {
err(`[Failed] ${flow.name}${renderError(flow.errors)}`)
} else if (flow.status === BenchmarkStatus.WARNING) {
warning(`[Warning] ${flow.name}`)
} else if (flow.status === BenchmarkStatus.CANCELED) {
canceled(`[${getCanceledStatusMessage(flow.cancellationReason)}] ${flow.name}`)
}
}
const flowWord = (count: number): string => count === 1 ? 'Flow' : 'Flows'
const getFailedFlowsCountStr = (flows: Flow[]): string => {
const failedFlows = flows.filter(flow => flow.status === BenchmarkStatus.ERROR)
return `${failedFlows.length}/${flows.length} ${flowWord(flows.length)} Failed`
}
const printUploadResult = (status: BenchmarkStatus, flows: Flow[]) => {
if (status === BenchmarkStatus.ERROR) {
err(getFailedFlowsCountStr(flows))
} else {
const passedFlows = flows.filter(flow => flow.status === BenchmarkStatus.SUCCESS || flow.status === BenchmarkStatus.WARNING)
const canceledFlows = flows.filter(flow => flow.status === BenchmarkStatus.CANCELED)
if (passedFlows.length > 0) {
success(`${passedFlows.length}/${flows.length} ${flowWord(flows.length)} Passed`)
if (canceledFlows.length > 0) {
canceled(`${canceledFlows.length}/${flows.length} ${flowWord(flows.length)} Canceled`)
}
} else {
canceled('Upload Canceled')
}
}
}
export default class StatusPoller {
timeout: NodeJS.Timeout | undefined
completedFlows: { [flowName: string]: string } = {}
stopped: Boolean = false
constructor(
private client: ApiClient,
private uploadId: string,
private consoleUrl: string,
) { }
markFailed(msg: string) {
core.setFailed(msg)
}
onError(errMsg: string, error?: any) {
let msg = `${errMsg}`
if (!!error) msg += ` - received error ${error}`
msg += `. View the Upload in the console for more information: ${this.consoleUrl}`
this.markFailed(msg)
}
async poll(
sleep: number,
prevErrorCount: number = 0
) {
if (this.stopped) {
return
}
try {
const { completed, status, flows } = await this.client.getUploadStatus(this.uploadId)
for (const flow of flows.filter(isCompleted)) {
if (!this.completedFlows[flow.name]) {
printFlowResult(flow)
this.completedFlows[flow.name] = flow.status
}
}
if (completed) {
this.teardown()
console.log('')
printUploadResult(status, flows)
console.log('')
info(`==== View details in the console ====\n`)
info(`${this.consoleUrl}`)
core.setOutput('MAESTRO_CLOUD_UPLOAD_STATUS', status)
core.setOutput('MAESTRO_CLOUD_FLOW_RESULTS', flows)
if (status === BenchmarkStatus.ERROR) {
const resultStr = getFailedFlowsCountStr(flows)
console.log('')
this.markFailed(resultStr)
}
} else {
setTimeout(() => this.poll(sleep), sleep)
}
} catch (error) {
if (error instanceof UploadStatusError) {
if (error.status === 429) {
// back off through extending sleep duration with 25%
const newSleep = sleep * 1.25
setTimeout(() => this.poll(newSleep, prevErrorCount), newSleep)
} else if (error.status >= 500) {
if (prevErrorCount < 3) {
setTimeout(() => this.poll(sleep, prevErrorCount++), sleep)
} else {
this.onError(`Request to get status information failed with status code ${error.status}: ${error.text}`)
}
} else {
this.onError('Could not get Upload status', error)
}
} else {
this.onError('Could not get Upload status', error)
}
}
}
registerTimeout(timeoutInMinutes?: number) {
this.timeout = setTimeout(() => {
warning(`Timed out waiting for Upload to complete. View the Upload in the console for more information: ${this.consoleUrl}`)
this.stopped = true
}, timeoutInMinutes ? (timeoutInMinutes * 60 * 1000) : WAIT_TIMEOUT_MS)
}
teardown() {
this.timeout && clearTimeout(this.timeout)
}
startPolling(timeout?: number) {
try {
this.poll(INTERVAL_MS)
info('Waiting for analyses to complete...\n')
} catch (err) {
this.markFailed(err instanceof Error ? err.message : `${err} `)
}
this.registerTimeout(timeout)
}
}