This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
forked from mobile-dev-inc/action-maestro-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatusPoller.ts
141 lines (121 loc) · 4.5 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
import * as core from '@actions/core'
import ApiClient, { BenchmarkStatus, 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 printFlowResult = (flow: Flow): void => {
if (flow.status === BenchmarkStatus.SUCCESS) {
success(`[Passed] ${flow.name}`)
} else if (flow.status === BenchmarkStatus.ERROR) {
err(`[Failed] ${flow.name}`)
} else if (flow.status === BenchmarkStatus.WARNING) {
warning(`[Warning] ${flow.name}`)
} else if (flow.status === BenchmarkStatus.CANCELED) {
canceled(`[Canceled] ${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 } = {}
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 += ` - receveied error ${error}`
msg += `. View the Upload in the console for more information: ${this.consoleUrl}`
this.markFailed(msg)
}
async poll(
sleep: number,
prevErrorCount: number = 0
) {
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}`)
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() {
this.timeout = setTimeout(() => {
warning(`Timed out waiting for Upload to complete. View the Upload in the console for more information: ${this.consoleUrl}`)
}, WAIT_TIMEOUT_MS)
}
teardown() {
this.timeout && clearTimeout(this.timeout)
}
startPolling() {
try {
this.poll(INTERVAL_MS)
info('Waiting for analyses to complete...\n')
} catch (err) {
this.markFailed(err instanceof Error ? err.message : `${err} `)
}
this.registerTimeout()
}
}