Skip to content

Commit

Permalink
add checkUp
Browse files Browse the repository at this point in the history
  • Loading branch information
airyland committed Mar 26, 2021
1 parent 6a5e530 commit 8de9c5d
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 6 deletions.
38 changes: 38 additions & 0 deletions helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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) {
console.log(e.message)
await sleep(CHECK_TIME)
}
}
console.log('duration:', new Date().getTime() - startTime)
resolve()
})
}

module.exports = {
sleep,
checkup
}
20 changes: 14 additions & 6 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const NginxManager = require('nginx-upstream')
const chalk = require('chalk')
const http = require('http')
const cwd = process.cwd()
const { sleep, checkUp } = require('./helper')

const config = {
instances: [{
Expand All @@ -18,18 +19,13 @@ const config = {
port: 8002,
title: 8002
}],
runningInstances: [],
startCommand: 'service nginx start',
reloadCommand: 'nginx -s reload',
nginxConfig: 'nginx.conf',
waitStopTime: 5000
}

const sleep = function (time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time)
})
}

const info = function (str) {
console.log(chalk.green(str))
}
Expand Down Expand Up @@ -146,11 +142,23 @@ names.forEach(name => {
shell.exit(1)
}

await checkUp(`http://localhost:${instance.port}`)

// append backend to nginx and reload
await libs.addBackend(`localhost:${instance.port}`)
shell.exec(config.reloadCommand)

// done for current backend
info(`[deploy info] instance:${instance.title} reload done;`)
}

if (config.runningInstances && config.runningInstances.length) {
config.instances.forEach(instance => {
await libs.removeBackend(`localhost:${instance.port}`)
})
config.runningInstances.forEach(instance => {
await libs.addBackend(`localhost:${instance.port}`)
})
shell.exec(config.reloadCommand)
}
})()
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"axios": "^0.21.1",
"chalk": "^2.4.1",
"nginx-upstream": "^0.1.4",
"shelljs": "^0.8.1"
Expand Down
Loading

0 comments on commit 8de9c5d

Please sign in to comment.