From 4f882acb33dbae4e812c3b4c7e0d3075e15c700c Mon Sep 17 00:00:00 2001 From: Unitech Date: Tue, 18 Nov 2014 15:35:35 +0100 Subject: [PATCH] refactor --- lib/Common.js | 7 +------ lib/Satan.js | 23 +++++++++++------------ lib/Utility.js | 5 +++++ 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/Common.js b/lib/Common.js index 08d6621d..9229f736 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -14,6 +14,7 @@ var extItps = require('./interpreter.json'); var p = path; var Stringify = require('json-stringify-safe'); +var Satan = require('./Satan.js'); var InteractorDaemonizer = require('./Interactor/InteractorDaemonizer.js'); /** @@ -190,8 +191,6 @@ Common.validateApp = function(appConf, outputter) { * @return CallExpression */ Common.exitCli = function(code) { - var Satan = require('./Satan.js'); - InteractorDaemonizer.disconnectRPC(function() { Satan.disconnectRPC(function() { process.exit(code || 0); @@ -224,7 +223,6 @@ Common.printOut = function() { Common.getAllProcess = function(cb) { var found_proc = []; - var Satan = require('./Satan.js'); Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { @@ -243,7 +241,6 @@ Common.getAllProcess = function(cb) { Common.getAllProcessId = function(cb) { var found_proc = []; - var Satan = require('./Satan.js'); Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { @@ -261,7 +258,6 @@ Common.getAllProcessId = function(cb) { Common.getProcessIdByName = function(name, cb) { var found_proc = []; - var Satan = require('./Satan.js'); Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { @@ -282,7 +278,6 @@ Common.getProcessIdByName = function(name, cb) { Common.getProcessByName = function(name, cb) { var found_proc = []; - var Satan = require('./Satan.js'); Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { diff --git a/lib/Satan.js b/lib/Satan.js index 3d3d87d8..b129e616 100644 --- a/lib/Satan.js +++ b/lib/Satan.js @@ -6,15 +6,14 @@ /** * Dependencies */ - -var rpc = require('pm2-axon-rpc'); -var axon = require('pm2-axon'); -var debug = require('debug')('pm2:satan'); -var util = require('util'); -var fs = require('fs'); -var p = require('path'); -var cst = require('../constants.js'); -var Common = require('./Common.js'); +var rpc = require('pm2-axon-rpc'); +var axon = require('pm2-axon'); +var debug = require('debug')('pm2:satan'); +var util = require('util'); +var fs = require('fs'); +var p = require('path'); +var cst = require('../constants.js'); +var Utility = require('./Utility.js'); /** * Export @@ -243,7 +242,7 @@ Satan.remoteWrapper = function() { God.clusters_db[msg.process.pm_id].pm2_env.name = msg.data.name; Object.keys(msg.data).forEach(function(conf_key) { - God.clusters_db[msg.process.pm_id].pm2_env.axm_options[conf_key] = Common.serialize(msg.data[conf_key]); + God.clusters_db[msg.process.pm_id].pm2_env.axm_options[conf_key] = Utility.serialize(msg.data[conf_key]); }); } catch(e) { console.error(e.stack || e); @@ -261,7 +260,7 @@ Satan.remoteWrapper = function() { if (!msg.process || !God.clusters_db[msg.process.pm_id]) return console.error('Unknown id %s', msg.process.pm_id); - God.clusters_db[msg.process.pm_id].pm2_env.axm_monitor = Common.serialize(msg.data); + God.clusters_db[msg.process.pm_id].pm2_env.axm_monitor = Utility.serialize(msg.data); delete msg; }); @@ -276,7 +275,7 @@ Satan.remoteWrapper = function() { delete data_v; return false; } - return pub.emit(this.event, Common.serialize(data_v)); + return pub.emit(this.event, Utility.serialize(data_v)); }); }; diff --git a/lib/Utility.js b/lib/Utility.js index 89784256..61d9a764 100644 --- a/lib/Utility.js +++ b/lib/Utility.js @@ -1,6 +1,11 @@ +var Stringify = require('json-stringify-safe'); + var Utility = module.exports = { getDate : function() { return Math.round(Date.now() / 1000); + }, + serialize : function(data) { + return JSON.parse(Stringify(data)); } };