From 754cb5524ba840658ba9126bf247874cf336b2a1 Mon Sep 17 00:00:00 2001 From: Slava Ganzin Date: Sat, 4 Feb 2023 03:33:46 +0100 Subject: [PATCH] fix healthchecks --- callmemaybe.js | 2 +- package.json | 2 +- run.js | 30 ++++++++++++++++-------------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/callmemaybe.js b/callmemaybe.js index e58fbed..2612baa 100755 --- a/callmemaybe.js +++ b/callmemaybe.js @@ -108,7 +108,7 @@ program.command('start') .catch(console.error) }) - return c.healthcheck ? healthcheck(c, question.name, opts, true) : wait(300) + return c.healthcheck ? healthcheck(c, question.name, 10, opts) : wait(300) }) .then(async () => { if(c.redirect) { diff --git a/package.json b/package.json index bb6dc4d..c818145 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "callmemaybe", - "version": "0.6.0", + "version": "0.7.0", "description": "", "main": "index.js", "scripts": { diff --git a/run.js b/run.js index 70b115f..b92c7d9 100644 --- a/run.js +++ b/run.js @@ -27,14 +27,13 @@ const run = async (command, name, opts = {}) => { return Promise.resolve(r) } - const shell = String(opts.shell || 'bash') + const [shell, ...args] = String(opts.shell || 'bash -c').split(/\s+/) opts.shell = false delete opts.input console.log(shell, opts) // r = execa('"""'+command+'"""', [], opts) - r = execa(shell, ['-c', `"""${command}"""`], opts) - .then(console.log) - .catch(console.error) + console.log(shell, args) + r = execa(shell, concat(args, [command]), opts) if (name) running[name] = r r.stdout.pipe(process.stdout) r.stderr.pipe(process.stderr) @@ -42,18 +41,21 @@ const run = async (command, name, opts = {}) => { return r } -const healthcheck = (c, name, wait, opts) => { - const r = running[name] - if (r) return Promise.resolve() +const healthcheck = (c, name, retry, opts) => { + const r = running[name] + if (r) + return Promise.resolve() - if (c.healthcheck) - return run(c.healthcheck, `healthcheck ${name}`, opts) - .catch(e => { - if (wait) return healthcheck(c, name, wait) - throw e - }) + if (!c.healthcheck) + return Promise.reject({}) + + return run(c.healthcheck, `healthcheck ${name}`, opts) + .catch(e => { + if (retry > 0) return wait(100) + .then(() => healthcheck(c, name, retry-1, opts)) + throw e + }) - return Promise.reject({}) } const user2uid = user => execa(`id -u ${user}`)