From 5b2416a527a82f5c4d90197e909ed91cbcf6ab21 Mon Sep 17 00:00:00 2001 From: Unitech Date: Mon, 23 Jul 2018 18:41:51 +0200 Subject: [PATCH 1/4] disable watchdog on exit - make call to cb 'synchronous' #88 --- src/InteractorDaemon.js | 28 +++++++++++++++------------- src/WatchDog.js | 4 ++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/InteractorDaemon.js b/src/InteractorDaemon.js index b79fafa..5b3b6e9 100644 --- a/src/InteractorDaemon.js +++ b/src/InteractorDaemon.js @@ -60,9 +60,9 @@ const InteractorDaemon = module.exports = class InteractorDaemon { /** * Terminate connections and exit - * @param {Error} err if provided, the exit code will be set to cst.ERROR_EXIT + * @param {cb} callback called at the end */ - exit (err) { + exit (cb) { log('Exiting Interactor') // clear workers if (this._workerEndpoint) clearInterval(this._workerEndpoint) @@ -71,13 +71,11 @@ const InteractorDaemon = module.exports = class InteractorDaemon { if (this.reverse) this.reverse.stop() if (this.push) this.push.stop() + if (this._ipm2) this._ipm2.disconnect() + if (this.watchDog) this.watchDog.stop() // stop transport if (this.transport) this.transport.disconnect() - this.getPM2Client().disconnect(() => { - log('Closed connection to PM2 bus and RPC server') - }) - try { fs.unlinkSync(cst.INTERACTOR_RPC_PORT) fs.unlinkSync(cst.INTERACTOR_PID_PATH) @@ -87,10 +85,14 @@ const InteractorDaemon = module.exports = class InteractorDaemon { return process.exit(cst.ERROR_EXIT) } - this._rpc.sock.close(() => { - log('RPC server closed') - process.exit(err ? cst.ERROR_EXIT : cst.SUCCESS_EXIT) - }) + cb() + + setTimeout(() => { + this._rpc.sock.close(() => { + log('RPC server closed') + process.exit(cst.SUCCESS_EXIT) + }) + }, 10) } /** @@ -106,8 +108,7 @@ const InteractorDaemon = module.exports = class InteractorDaemon { rpcServer.expose({ kill: function (cb) { log('Shutdown request received via RPC') - cb(null) - return self.exit() + return self.exit(cb) }, getInfos: function (cb) { if (self.opts && self.DAEMON_ACTIVE === true) { @@ -356,12 +357,13 @@ const InteractorDaemon = module.exports = class InteractorDaemon { this.watchDog = WatchDog setTimeout(() => { + log('PM2 Watchdog started') this.watchDog.start({ conf: { ipm2: this.getPM2Client() } }) - }, 3 * 60 * 1000) + }, 1000 * 60 * 3) this.push = new PushInteractor(this.opts, this.getPM2Client(), this.transport) this.reverse = new ReverseInteractor(this.opts, this.getPM2Client(), this.transport) diff --git a/src/WatchDog.js b/src/WatchDog.js index 3c82447..2203380 100644 --- a/src/WatchDog.js +++ b/src/WatchDog.js @@ -37,6 +37,10 @@ module.exports = class WatchDog { }) } + static stop() { + clearInterval(this.dump_interval) + } + static resurrect () { debug(`Trying to launch PM2: ${path.resolve(__dirname, '../../../../bin/pm2')}`) child.exec(`node ${path.resolve(__dirname, '../../../../bin/pm2')} resurrect`, _ => { From d6215fb9b2044ef0467facf85976810ed464885b Mon Sep 17 00:00:00 2001 From: Unitech Date: Mon, 23 Jul 2018 18:45:08 +0200 Subject: [PATCH 2/4] change --- src/InteractorDaemon.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/InteractorDaemon.js b/src/InteractorDaemon.js index 5b3b6e9..b3c60a5 100644 --- a/src/InteractorDaemon.js +++ b/src/InteractorDaemon.js @@ -59,10 +59,10 @@ const InteractorDaemon = module.exports = class InteractorDaemon { } /** - * Terminate connections and exit + * Terminate aconnections and exit * @param {cb} callback called at the end */ - exit (cb) { + exit (err, cb) { log('Exiting Interactor') // clear workers if (this._workerEndpoint) clearInterval(this._workerEndpoint) @@ -85,12 +85,12 @@ const InteractorDaemon = module.exports = class InteractorDaemon { return process.exit(cst.ERROR_EXIT) } - cb() + cb && typeof(cb) === 'function' ? cb() : null setTimeout(() => { this._rpc.sock.close(() => { log('RPC server closed') - process.exit(cst.SUCCESS_EXIT) + process.exit(err ? cst.ERROR_EXIT : cst.SUCCESS_EXIT) }) }, 10) } @@ -108,7 +108,7 @@ const InteractorDaemon = module.exports = class InteractorDaemon { rpcServer.expose({ kill: function (cb) { log('Shutdown request received via RPC') - return self.exit(cb) + return self.exit(null, cb) }, getInfos: function (cb) { if (self.opts && self.DAEMON_ACTIVE === true) { From c4bc91e8c21b1cd125b1f138f21fe1c0a277faa2 Mon Sep 17 00:00:00 2001 From: Unitech Date: Tue, 24 Jul 2018 12:23:17 +0200 Subject: [PATCH 3/4] chore: remove some logs --- src/InteractorClient.js | 5 +---- src/InteractorDaemon.js | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/InteractorClient.js b/src/InteractorClient.js index 539eaad..ee0e960 100644 --- a/src/InteractorClient.js +++ b/src/InteractorClient.js @@ -252,9 +252,6 @@ module.exports = class InteractorDaemonizer { return cb(msg) } - if (msg.km_data && msg.km_data.active === true && !process.env.PM2_SILENT) { - console.log('%s %s', chalk.cyan('[PM2.IO]'), msg.km_data.new ? 'Agent created' : 'Agent updated') - } return cb(null, msg, child) }) } @@ -448,7 +445,7 @@ module.exports = class InteractorDaemonizer { if (err || !conf) return cb(err || new Error('Cant retrieve configuration')) if (!process.env.PM2_SILENT) { - console.log(chalk.cyan('[PM2.IO]') + ' Using: Public key: %s | Private key: %s | Machine name: %s', conf.public_key, conf.secret_key, conf.machine_name) + console.log(chalk.cyan('[PM2 I/O]') + ' Using: Public key: %s | Private key: %s | Machine name: %s', conf.public_key, conf.secret_key, conf.machine_name) } return this.launchOrAttach(cst, conf, cb) }) diff --git a/src/InteractorDaemon.js b/src/InteractorDaemon.js index b3c60a5..8fb139e 100644 --- a/src/InteractorDaemon.js +++ b/src/InteractorDaemon.js @@ -238,12 +238,11 @@ const InteractorDaemon = module.exports = class InteractorDaemon { * @param {Function} cb invoked with */ _verifyEndpoint (cb) { - log('Verifying endpoints') if (typeof cb !== 'function') cb = function () {} this._pingRoot((err, data) => { if (err) { - log('Got an a error on ping root') + log('Got an a error on ping root', err) return cb(err) } @@ -258,7 +257,6 @@ const InteractorDaemon = module.exports = class InteractorDaemon { return cb(null, data) } - log('Connect transport with endpoints') this.DAEMON_ACTIVE = true this.transport.connect(data.endpoints, cb) }) From 1d0305aa5630a631f70982d3438ec5b3157bf76b Mon Sep 17 00:00:00 2001 From: Unitech Date: Tue, 24 Jul 2018 12:27:22 +0200 Subject: [PATCH 4/4] feature: add version to process name --- src/InteractorDaemon.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/InteractorDaemon.js b/src/InteractorDaemon.js index 8fb139e..abca3b8 100644 --- a/src/InteractorDaemon.js +++ b/src/InteractorDaemon.js @@ -16,6 +16,7 @@ const WatchDog = require('./WatchDog') const InteractorClient = require('./InteractorClient') const semver = require('semver') const path = require('path') +const pkg = require('../package.json') // use noop if not launched via IPC if (!process.send) { @@ -408,7 +409,7 @@ if (require.main === module) { }) }) d.run(_ => { - process.title = 'PM2 Agent (' + cst.PM2_HOME + ')' + process.title = `PM2 Agent v${pkg.version}: (${cst.PM2_HOME})` console.log('[PM2 Agent] Launching agent') new InteractorDaemon().start()