-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathabstract-check-runner.ts
More file actions
262 lines (234 loc) · 9.21 KB
/
abstract-check-runner.ts
File metadata and controls
262 lines (234 loc) · 9.21 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { assets, testSessions } from '../rest/api'
import { SocketClient } from './socket-client'
import PQueue from 'p-queue'
import * as uuid from 'uuid'
import { EventEmitter } from 'node:events'
import type { MqttClient } from 'mqtt'
import type { Region } from '..'
import { TestResultsShortLinks } from '../rest/test-sessions'
// eslint-disable-next-line no-restricted-syntax
export enum Events {
CHECK_REGISTERED = 'CHECK_REGISTERED',
CHECK_INPROGRESS = 'CHECK_INPROGRESS',
CHECK_ATTEMPT_RESULT = 'CHECK_ATTEMPT_RESULT',
CHECK_FAILED = 'CHECK_FAILED',
CHECK_SUCCESSFUL = 'CHECK_SUCCESSFUL',
CHECK_FINISHED = 'CHECK_FINISHED',
RUN_STARTED = 'RUN_STARTED',
RUN_FINISHED = 'RUN_FINISHED',
ERROR = 'ERROR',
MAX_SCHEDULING_DELAY_EXCEEDED = 'MAX_SCHEDULING_DELAY_EXCEEDED',
STREAM_LOGS = 'STREAM_LOGS',
}
export type PrivateRunLocation = {
type: 'PRIVATE',
id: string,
slugName: string,
}
export type PublicRunLocation = {
type: 'PUBLIC',
region: keyof Region,
}
export type RunLocation = PublicRunLocation | PrivateRunLocation
export type CheckRunId = string
export type SequenceId = string
export const DEFAULT_CHECK_RUN_TIMEOUT_SECONDS = 600
const DEFAULT_SCHEDULING_DELAY_EXCEEDED_MS = 20000
export default abstract class AbstractCheckRunner extends EventEmitter {
checks: Map<SequenceId, { check: any }>
testSessionId?: string
// If there's an error in the backend and no check result is sent, the check run could block indefinitely.
// To avoid this case, we set a per-check timeout.
timeouts: Map<SequenceId, NodeJS.Timeout>
schedulingDelayExceededTimeout?: NodeJS.Timeout
accountId: string
timeout: number
verbose: boolean
queue: PQueue
constructor (
accountId: string,
timeout: number,
verbose: boolean,
) {
super()
this.checks = new Map()
this.timeouts = new Map()
this.queue = new PQueue({ autoStart: false, concurrency: 1 })
this.timeout = timeout
this.verbose = verbose
this.accountId = accountId
}
abstract scheduleChecks (checkRunSuiteId: string):
Promise<{
testSessionId?: string,
checks: Array<{ check: any, sequenceId: SequenceId }>,
}>
async run () {
let socketClient = null
try {
socketClient = await SocketClient.connect()
const checkRunSuiteId = uuid.v4()
// Configure the socket listener and allChecksFinished listener before starting checks to avoid race conditions
await this.configureResultListener(checkRunSuiteId, socketClient)
const { testSessionId, checks } = await this.scheduleChecks(checkRunSuiteId)
this.testSessionId = testSessionId
this.checks = new Map(
checks.map(({ check, sequenceId }) => [sequenceId, { check }]),
)
// `processMessage()` assumes that `this.timeouts` always has an entry for non-timed-out checks.
// To ensure that this is the case, we call `setAllTimeouts()` before `queue.start()`.
// Otherwise, we risk a race condition where check results are received before the timeout is set.
// This would cause `processMessage()` to mistakenly skip check results and consider the checks timed-out.
this.setAllTimeouts()
// Add timeout to fire an event after DEFAULT_SCHEDULING_DELAY_EXCEEDED_MS to let reporters know it's time
// to display a hint messages if some checks are still being scheduled.
this.startSchedulingDelayTimeout()
// `allChecksFinished` should be started before processing check results in `queue.start()`.
// Otherwise, there could be a race condition causing check results to be missed by `allChecksFinished()`.
const allChecksFinished = this.allChecksFinished()
/// / Need to structure the checks depending on how it went
this.emit(Events.RUN_STARTED, checks, testSessionId)
// Start the queue after the test session run rest call is completed to avoid race conditions
this.queue.start()
await allChecksFinished
this.emit(Events.RUN_FINISHED, testSessionId)
} catch (err) {
this.disableAllTimeouts()
this.emit(Events.ERROR, err)
} finally {
if (socketClient) {
await socketClient.endAsync()
}
}
}
private async configureResultListener (checkRunSuiteId: string, socketClient: MqttClient): Promise<void> {
socketClient.on('message', (topic: string, rawMessage: string|Buffer) => {
const message = JSON.parse(rawMessage.toString('utf8'))
const topicComponents = topic.split('/')
const sequenceId = topicComponents[4]
const checkRunId = topicComponents[5]
const subtopic = topicComponents[6]
this.queue.add(() => this.processMessage(sequenceId, subtopic, message))
})
await socketClient.subscribeAsync(`account/${this.accountId}/ad-hoc-check-results/${checkRunSuiteId}/+/+/+`)
}
private async processMessage (sequenceId: SequenceId, subtopic: string, message: any) {
if (!sequenceId) {
// There should always be a sequenceId, but let's have an early return to make forwards-compatibility easier.
return
}
if (!this.timeouts.has(sequenceId)) {
// The check has already timed out. We return early to avoid reporting a duplicate result.
return
}
if (!this.checks.get(sequenceId)) {
return
}
const { check } = this.checks.get(sequenceId)!
if (subtopic === 'run-start') {
this.emit(Events.CHECK_INPROGRESS, check, sequenceId)
} else if (subtopic === 'result') {
const { result, testResultId, resultType } = message
await this.processCheckResult(result)
const links = testResultId && result.hasFailures && await this.getShortLinks(testResultId)
if (resultType === 'FINAL') {
this.disableTimeout(sequenceId)
this.emit(Events.CHECK_SUCCESSFUL, sequenceId, check, result, testResultId, links)
this.emit(Events.CHECK_FINISHED, check)
} else if (resultType === 'ATTEMPT') {
this.emit(Events.CHECK_ATTEMPT_RESULT, sequenceId, check, result, links)
}
} else if (subtopic === 'error') {
this.disableTimeout(sequenceId)
this.emit(Events.CHECK_FAILED, sequenceId, check, message)
this.emit(Events.CHECK_FINISHED, check)
} else if (subtopic === 'stream-logs') {
const { logs } = message
this.emit(Events.STREAM_LOGS, check, sequenceId, logs)
}
}
async processCheckResult (result: any) {
const {
region,
logPath,
checkRunDataPath,
} = result.assets
if (logPath && (this.verbose || result.hasFailures)) {
result.logs = await assets.getLogs(region, logPath)
}
if (checkRunDataPath && (this.verbose || result.hasFailures)) {
result.checkRunData = await assets.getCheckRunData(region, checkRunDataPath)
}
}
private allChecksFinished (): Promise<void> {
let finishedCheckCount = 0
const numChecks = this.checks.size
if (numChecks === 0) {
return Promise.resolve()
}
return new Promise((resolve) => {
this.on(Events.CHECK_FINISHED, () => {
finishedCheckCount++
if (finishedCheckCount === numChecks) resolve()
})
})
}
private setAllTimeouts () {
Array.from(this.checks.entries()).forEach(([sequenceId, { check }]) =>
this.timeouts.set(sequenceId, setTimeout(() => {
this.timeouts.delete(sequenceId)
let errorMessage = `Reached timeout of ${this.timeout} seconds waiting for check result.`
// Checkly should always report a result within 240s.
// If the default timeout was used, we should point the user to the status page and support email.
if (this.timeout === DEFAULT_CHECK_RUN_TIMEOUT_SECONDS) {
errorMessage += ' Checkly may be experiencing problems. Please check https://is.checkly.online or reach out to support@checklyhq.com.'
}
this.emit(Events.CHECK_FAILED, sequenceId, check, errorMessage)
this.emit(Events.CHECK_FINISHED, check)
}, this.timeout * 1000),
))
}
private disableAllTimeouts () {
if (!this.checks) {
return
}
Array.from(this.checks.entries()).forEach(([checkRunId]) => this.disableTimeout(checkRunId))
if (this.schedulingDelayExceededTimeout) {
clearTimeout(this.schedulingDelayExceededTimeout)
this.schedulingDelayExceededTimeout = undefined
}
}
private startSchedulingDelayTimeout () {
let scheduledCheckCount = 0
const numChecks = this.checks.size
if (numChecks === 0) {
return
}
this.schedulingDelayExceededTimeout = setTimeout(
() => {
this.emit(Events.MAX_SCHEDULING_DELAY_EXCEEDED)
this.schedulingDelayExceededTimeout = undefined
},
DEFAULT_SCHEDULING_DELAY_EXCEEDED_MS,
)
this.on(Events.CHECK_INPROGRESS, () => {
scheduledCheckCount++
if (scheduledCheckCount === numChecks) clearTimeout(this.schedulingDelayExceededTimeout)
})
}
private disableTimeout (timeoutKey: string) {
const timeout = this.timeouts.get(timeoutKey)
clearTimeout(timeout)
this.timeouts.delete(timeoutKey)
}
private async getShortLinks (testResultId: string): Promise<TestResultsShortLinks|undefined> {
try {
if (!this.testSessionId) {
return
}
const { data: links } = await testSessions.getResultShortLinks(this.testSessionId, testResultId)
return links
} catch {
}
}
}