Skip to content

Commit

Permalink
daemon: catch error and restart itself #42
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech authored and vmarchaud committed May 15, 2018
1 parent b632712 commit a722234
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

module.exports = require('./src/InteractorClient.js');
module.exports = require('./src/InteractorClient.js')
20 changes: 10 additions & 10 deletions src/InteractorClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const axon = require('pm2-axon')
const chalk = require('chalk')
const os = require('os')
const constants = require('../constants')
const child_process = require('child_process')
const childProcess = require('child_process')

const printError = (msg) => {
if (process.env.PM2_SILENT || process.env.PM2_PROGRAMMATIC) return false
Expand Down Expand Up @@ -174,7 +174,7 @@ module.exports = class InteractorDaemonizer {
const out = testEnv ? 1 : fs.openSync(constants.INTERACTOR_LOG_FILE_PATH, 'a')
const err = testEnv ? 2 : fs.openSync(constants.INTERACTOR_LOG_FILE_PATH, 'a')

const child = child_process.spawn(process.env.NODEJS_EXECUTABLE || 'node', [InteractorJS], {
const child = childProcess.spawn(process.env.NODEJS_EXECUTABLE || 'node', [InteractorJS], {
silent: false,
detached: true,
cwd: process.cwd(),
Expand Down Expand Up @@ -220,31 +220,31 @@ module.exports = class InteractorDaemonizer {
// Handle and show to user the different error message that can happen
if (msg.error === true) {
if (!process.env.PM2_SILENT) {
console.log(chalk.red('[Keymetrics.io][ERROR]'), msg.msg)
console.log(chalk.cyan('[Keymetrics.io]') + ' Contact support [email protected] and send us the error message')
console.log(chalk.red('[PM2.IO][ERROR]'), msg.msg)
console.log(chalk.cyan('[PM2.IO]') + ' Contact support [email protected] and send us the error message')
}
return cb(msg)
} else if (msg.km_data && msg.km_data.disabled === true) {
if (!process.env.PM2_SILENT) {
console.log(chalk.cyan('[Keymetrics.io]') + ' Server DISABLED BY ADMINISTRATION contact support [email protected] with reference to your public and secret keys)')
console.log(chalk.cyan('[PM2.IO]') + ' Server DISABLED BY ADMINISTRATION contact support [email protected] with reference to your public and secret keys)')
}
return cb(msg)
} else if (msg.km_data && msg.km_data.error === true) {
if (!process.env.PM2_SILENT) {
console.log('%s %s (Public: %s) (Secret: %s) (Machine name: %s)', chalk.red('[Keymetrics.io][ERROR]'),
console.log('%s %s (Public: %s) (Secret: %s) (Machine name: %s)', chalk.red('[PM2.IO][ERROR]'),
msg.km_data.msg, msg.public_key, msg.secret_key, msg.machine_name)
}
return cb(msg)
} else if (msg.km_data && msg.km_data.active === false && msg.km_data.pending === true) {
if (!process.env.PM2_SILENT) {
console.log('%s %s - Web Access: https://app.keymetrics.io/', chalk.red('[Keymetrics.io]'), chalk.bold.red('Agent PENDING'))
console.log('%s You must upgrade your bucket in order to monitor more servers.', chalk.red('[Keymetrics.io]'))
console.log('%s %s - Web Access: https://app.keymetrics.io/', chalk.red('[PM2.IO]'), chalk.bold.red('Agent PENDING'))
console.log('%s You must upgrade your bucket in order to monitor more servers.', chalk.red('[PM2.IO]'))
}
return cb(msg)
}

if (msg.km_data && msg.km_data.active === true && !process.env.PM2_SILENT) {
console.log('%s [%s] Agent ACTIVE - Web Access: https://app.keymetrics.io/', chalk.cyan('[Keymetrics.io]'), msg.km_data.new ? 'Agent created' : 'Agent updated')
console.log('%s [%s] Agent ACTIVE - Web Access: https://app.keymetrics.io/', chalk.cyan('[PM2.IO]'), msg.km_data.new ? 'Agent created' : 'Agent updated')
}
return cb(null, msg, child)
})
Expand Down Expand Up @@ -424,7 +424,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('[Keymetrics.io]') + ' Using (Public key: %s) (Private key: %s)', conf.public_key, conf.secret_key)
console.log(chalk.cyan('[PM2.IO]') + ' Using (Public key: %s) (Private key: %s)', conf.public_key, conf.secret_key)
}
return this.launchOrAttach(cst, conf, cb)
})
Expand Down
32 changes: 29 additions & 3 deletions src/InteractorDaemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const PM2Client = require('./PM2Client.js')
const TransporterInterface = require('./TransporterInterface.js')
const domain = require('domain') // eslint-disable-line
const WatchDog = require('./WatchDog')
const InteractorClient = require('./InteractorClient')

// use noop if not launched via IPC
if (!process.send) {
Expand Down Expand Up @@ -276,12 +277,37 @@ const InteractorDaemon = module.exports = class InteractorDaemon {
// otherwise we just required it to use a function
if (require.main === module) {
const d = domain.create()

d.on('error', function (err) {
console.error(err)
console.error('-- FATAL EXCEPTION happened --')
console.error(new Date())
console.error(err.stack)
console.log('Re-initiating Agent')

InteractorClient.getOrSetConf(cst, null, (err, infos) => {
if (err || !infos) {
if (err) {
console.error('[PM2 Agent] Failed to rescue agent :')
console.error(err || new Error(`Cannot find configuration to connect to backend`))
return process.exit(1)
}
}
console.log(`[PM2 Agent] Using (Public key: ${infos.public_key}) (Private key: ${infos.secret_key}) (Info node: ${infos.info_node})`)
InteractorClient.daemonize(cst, infos, (err) => {
if (err) {
console.error('[PM2 Agent] Failed to rescue agent :')
console.error(err)
return process.exit(1)
}
console.log(`Succesfully launched new agent`)
process.exit(0)
})
})
})
d.run(_ => {
process.title = 'PM2: KM Agent (' + process.env.PM2_HOME + ')'
log('[Keymetrics.io] Launching agent')
process.title = 'PM2 Agent (' + process.env.PM2_HOME + ')'

console.log('[PM2 Agent] Launching agent')
new InteractorDaemon().start()
})
}

0 comments on commit a722234

Please sign in to comment.