forked from cypress-io/github-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.js
72 lines (64 loc) · 2.07 KB
/
ping.js
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
const core = require('@actions/core')
const got = require('got')
/**
* A small utility for checking when an URL responds, kind of
* a poor man's https://www.npmjs.com/package/wait-on. This version
* is implemented using https://github.com/sindresorhus/got
*/
const ping = (url, timeout) => {
if (!timeout) {
throw new Error('Expected timeout in ms')
}
// make copy of the error codes that "got" retries on
const errorCodes = [...got.defaults.options.retry.errorCodes]
errorCodes.push('ESOCKETTIMEDOUT')
// we expect the server to respond within a time limit
// and if it does not - retry up to total "timeout" duration
const individualPingTimeout = Math.min(timeout, 30000)
const limit = Math.ceil(timeout / individualPingTimeout)
core.debug(`total ping timeout ${timeout}`)
core.debug(`individual ping timeout ${individualPingTimeout}ms`)
core.debug(`retries limit ${limit}`)
const start = +new Date()
return got(url, {
headers: {
Accept: 'text/html, application/json, text/plain, */*'
},
timeout: individualPingTimeout,
errorCodes,
retry: {
limit,
calculateDelay({ error, attemptCount }) {
if (error) {
core.debug(`got error ${JSON.stringify(error)}`)
}
const now = +new Date()
const elapsed = now - start
core.debug(
`${elapsed}ms ${error.method} ${error.host} ${error.code} attempt ${attemptCount}`
)
if (elapsed > timeout) {
console.error(
'%s timed out on retry %d of %d',
url,
attemptCount,
limit
)
return 0
}
// if the error code is ECONNREFUSED use shorter timeout
// because the server is probably starting
if (error.code === 'ECONNREFUSED') {
return 1000
}
// default "long" timeout
return individualPingTimeout
}
}
}).then(() => {
const now = +new Date()
const elapsed = now - start
core.debug(`pinging ${url} has finished ok after ${elapsed}ms`)
})
}
module.exports = { ping }