From c7cec7bb2912ecebb98f72a7e3f5f49d49303baa Mon Sep 17 00:00:00 2001 From: Unitech Date: Tue, 22 Oct 2019 12:44:29 +0200 Subject: [PATCH] feature: enable sysinfos + safeguard via ping/pong --- lib/Daemon.js | 4 +-- lib/God.js | 26 +++++++------- lib/Sysinfo/SystemInfo.js | 72 +++++++++++++++++++++++++++++++++------ package.json | 2 +- 4 files changed, 78 insertions(+), 26 deletions(-) diff --git a/lib/Daemon.js b/lib/Daemon.js index b9e10935..7a120586 100644 --- a/lib/Daemon.js +++ b/lib/Daemon.js @@ -259,7 +259,7 @@ Daemon.prototype.close = function(opts, cb) { msg : 'pm2 has been killed via CLI' }); - if (God.system_infos !== null) + if (God.system_infos_proc !== null) God.system_infos_proc.kill() /** @@ -336,7 +336,7 @@ Daemon.prototype.gracefullExit = function() { console.log('pm2 has been killed by signal, dumping process list before exit...'); - if (God.system_infos !== null) + if (God.system_infos_proc !== null) God.system_infos_proc.kill() God.dumpProcessList(function() { diff --git a/lib/God.js b/lib/God.js index ed0ab7b1..7e9881d8 100644 --- a/lib/God.js +++ b/lib/God.js @@ -75,21 +75,21 @@ God.init = function() { God.system_infos_proc = null - // try { - // var sysinfo = require('./Sysinfo/SystemInfo.js') - // God.system_infos_proc = new sysinfo() + try { + var sysinfo = require('./Sysinfo/SystemInfo.js') + God.system_infos_proc = new sysinfo() - // setInterval(() => { - // God.system_infos_proc.query((err, data) => { - // God.system_infos = data - // }) - // }, 1000) + setInterval(() => { + God.system_infos_proc.query((err, data) => { + God.system_infos = data + }) + }, 1000) - // God.system_infos_proc.fork() - // } catch(e) { - // console.log(e) - // God.system_infos_proc = null - // } + God.system_infos_proc.fork() + } catch(e) { + console.log(e) + God.system_infos_proc = null + } } God.writeExitSeparator = function(pm2_env, code, signal) { diff --git a/lib/Sysinfo/SystemInfo.js b/lib/Sysinfo/SystemInfo.js index 1bc2a0c5..f7f4dada 100644 --- a/lib/Sysinfo/SystemInfo.js +++ b/lib/Sysinfo/SystemInfo.js @@ -79,6 +79,7 @@ class SystemInfo { } } this.restart = true + this.ping_timeout = null } // Cast MeanCalc and other object to real value @@ -106,29 +107,53 @@ class SystemInfo { this.process.on('exit', (code) => { console.log(`systeminfos collection process offline with code ${code}`) - if (this.restart == true) - this.fork() + // if (this.restart == true) + // this.fork() }) this.process.on('error', (e) => { console.log(`Sysinfo errored`, e) }) + + this.process.on('message', (msg) => { + try { + msg = JSON.parse(msg) + } + catch (e) { + } + if (msg.cmd == 'ping') { + if (this.process.connected == true) { + try { + this.process.send('pong') + } catch(e) { + console.error('Cannot send message to Sysinfos') + } + } + } + }) } query(cb) { this.process.send('query') - this.process.once('message', (data) => { + var res = (msg) => { try { - data = JSON.parse(data) + msg = JSON.parse(msg) } catch (e) { } - return cb(null, data) - }) + + if (msg.cmd == 'query:res') { + listener.removeListener('message', res) + return cb(null, msg.data) + } + } + + var listener = this.process.on('message', res) } kill() { this.restart = false + console.log('>>>>>>>>>>>>>>>>>>>>>>>> SYSTEMINFO EXITED') this.process.kill() } @@ -162,11 +187,38 @@ class SystemInfo { this.cpuStatsWorker() this.fdStatsWorker() - process.on('message', (data) => { - if (data == 'query') { + setInterval(() => { + if (process.connected == false) { + console.error('Sysinfos not connected, exiting') + process.exit() + } + try { + process.send(JSON.stringify({cmd: 'ping'})) + } catch(e) { + console.error('PM2 is dead while doing process.send') + process.exit() + } + this.ping_timeout = setTimeout(() => { + console.error('PM2 is dead while waiting for a pong') + process.exit() + }, 500) + }, 1000) + + // Systeminfo receive command + process.on('message', (cmd) => { + if (cmd == 'query') { try { - process.send(JSON.stringify(this.report())) - } catch (e) {} + var res = JSON.stringify({ + cmd: 'query:res', + data: this.report() + }) + process.send(res) + } catch (e) { + console.error('Could not retrieve system informations', e) + } + } + else if (cmd == 'pong') { + clearTimeout(this.ping_timeout) } }) diff --git a/package.json b/package.json index 4b5f61ac..4d381f67 100644 --- a/package.json +++ b/package.json @@ -200,7 +200,7 @@ "should": "^13" }, "optionalDependencies": { - "systeminformation": "^4.14.11" + "systeminformation": "^4.14.16" }, "bugs": { "url": "https://github.com/Unitech/pm2/issues"