Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable watchdog on exit - make call to cb 'synchronous' #88 #89

Merged
merged 4 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/InteractorClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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)
})
Expand Down
37 changes: 19 additions & 18 deletions src/InteractorDaemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -59,10 +60,10 @@ 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
* Terminate aconnections and exit
* @param {cb} callback called at the end
*/
exit (err) {
exit (err, cb) {
log('Exiting Interactor')
// clear workers
if (this._workerEndpoint) clearInterval(this._workerEndpoint)
Expand All @@ -71,13 +72,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)
Expand All @@ -87,10 +86,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 && typeof(cb) === 'function' ? cb() : null

setTimeout(() => {
this._rpc.sock.close(() => {
log('RPC server closed')
process.exit(err ? cst.ERROR_EXIT : cst.SUCCESS_EXIT)
})
}, 10)
}

/**
Expand All @@ -106,8 +109,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(null, cb)
},
getInfos: function (cb) {
if (self.opts && self.DAEMON_ACTIVE === true) {
Expand Down Expand Up @@ -237,12 +239,11 @@ const InteractorDaemon = module.exports = class InteractorDaemon {
* @param {Function} cb invoked with <Error, Boolean>
*/
_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)
}

Expand All @@ -257,7 +258,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)
})
Expand Down Expand Up @@ -356,12 +356,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)
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions src/WatchDog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`, _ => {
Expand Down