-
Notifications
You must be signed in to change notification settings - Fork 8
/
helper.js
43 lines (40 loc) · 952 Bytes
/
helper.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
const axios = require('axios')
const CHECK_TIME = 5000
const TIMEOUT = 60000
const sleep = function (time) {
return new Promise((resolve) => {
setTimeout(resolve, time)
})
}
async function checkUp(url) {
return new Promise(async (resolve, reject) => {
let up = false
const startTime = new Date().getTime()
while (!up) {
if (new Date().getTime() - startTime > TIMEOUT) {
up = true
return reject('check up fail')
}
try {
console.log('检查是否在线: ', url)
await axios.get(url)
console.log('已经在线')
up = true
} catch (e) {
// 404 is also valid
if (e.message.includes('404')) {
up = true
} else {
console.log(e.message)
await sleep(CHECK_TIME)
}
}
}
console.log('duration:', new Date().getTime() - startTime)
resolve()
})
}
module.exports = {
sleep,
checkUp
}