Skip to content

Commit

Permalink
improve healthchecks
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaGanzin committed Jan 30, 2023
1 parent 8cbc605 commit a635dfa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
41 changes: 28 additions & 13 deletions callmemaybe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node-dev
#!/usr/bin/env node

global.config = {}
Error.stackTraceLimit = Infinity
Expand All @@ -13,6 +13,8 @@ const { program } = require('commander')
const server = require('./server')
const {run, healthcheck} = require('./run')

const wait = t => new Promise(r => setTimeout(r, t))

const pp = x =>
process.stdout.write(yaml.stringify(x || {}))

Expand All @@ -32,9 +34,9 @@ program

program.command('server')
.option('-r, --redirect <URL>')
.action((str, options) => {
return server(str)
})
.option('-t, --title <title>')
.parse()
.action((str, options) => server(str))

program.command('start')
.action(async (str, options) => {
Expand All @@ -47,23 +49,36 @@ program.command('start')
const c = config[question.name]

if (c) {
pp({matched: c})
// pp({matched: c})
response.answers.push({
name: question.name,
type: dns2.Packet.TYPE.A,
class: dns2.Packet.CLASS.IN,
ttl: 1,
address: c.ip || '127.0.0.1'
});
})

return await healthcheck(c, question.name)
.catch(x => run(c.start, question.name, {cwd: c.folder}))
.catch(({stderr, stdout}) => {
run(`callmemaybe server --name "${question.name} error"`, 'error-server', {input: stderr+stdout})
}).then(() => {
dns2.pp(response)
send(response)
.catch(x => {
run(c.start, question.name, {cwd: c.folder})
.then(() => console.log(3))
.catch(({stderr, stdout}) => {
console.log('failed to start', {stderr, stdout})
run(`callmemaybe server --title "${question.name} error"`, 'error-server', {input: stderr+stdout, restart: true})
.catch(console.error)
})

return c.healthcheck ? healthcheck(c, question.name, true) : wait(300)
})
.then(async () => {
if(c.redirect)
await run(`callmemaybe server --redirect ${c.redirect}`, 'redirect', {restart: true})

dns2.pp(response)
send(response)


})
}

if (blocklist.has(question.name)) {
Expand Down Expand Up @@ -96,7 +111,7 @@ program.command('start')
})

server.on('request', (request, response, rinfo) => {
console.log(request.header.id, request.questions[0])
// console.log(request.header.id, request.questions[0])
})

.on('requestError', (error) => {
Expand Down
21 changes: 14 additions & 7 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ setInterval(() => {
}
}, 100)

const run = (command, name, opts) => {
// console.log({running})
const run = (command, name, opts = {}) => {
console.log({run: command, name, opts})

let r = running[name]

if (r) {
// console.log(running[name].killed, running[name].closed)
if (opts.restart) r.kill()
if (opts.restart && r) {
r.kill()
delete running[name]
r = null
}

if (r) {
console.log('already runnning ' + name)
return Promise.resolve(r)
}
Expand All @@ -31,12 +34,16 @@ const run = (command, name, opts) => {
return r
}

const healthcheck = (c, name) => {
const healthcheck = (c, name, wait) => {
const r = running[name]
if (r) return Promise.resolve(r)
if (r) return Promise.resolve()

if (c.healthcheck)
return run(c.healthcheck, `healthcheck ${name}`, {cwd: c.folder || '~'})
.catch(e => {
if (wait) return healthcheck(c, name, wait)
throw e
})

return Promise.reject({})
}
Expand Down

0 comments on commit a635dfa

Please sign in to comment.