Skip to content

Commit 9dddc80

Browse files
committed
feat: allow pm2 to install a set of module as one single command and add deep-monitoring.
1 parent 4bbeec3 commit 9dddc80

File tree

1 file changed

+79
-51
lines changed

1 file changed

+79
-51
lines changed

lib/API/Modules/Modularizer.js

Lines changed: 79 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ var mkdirp = require('mkdirp');
2121

2222
var MODULE_CONF_PREFIX = 'module-db-v2';
2323

24+
var KNOWN_MODULES = {
25+
'deep-monitoring': {
26+
'dependencies': ['v8-profiler-node8', 'gc-stats', 'event-loop-inspector']
27+
},
28+
'gc-stats': {name: 'gc-stats'},
29+
'event-loop-inspector': {name: 'event-loop-inspector'}
30+
};
31+
2432
/**
2533
* PM2 Module System.
2634
* Features:
@@ -32,100 +40,100 @@ var MODULE_CONF_PREFIX = 'module-db-v2';
3240
* - Auto discover script to launch (first it checks the apps field, then bin and finally main attr)
3341
* - Generate sample module via pm2 module:generate <module_name>
3442
*/
35-
Modularizer.install = function(CLI, module_name, opts, cb) {
36-
Common.printOut(cst.PREFIX_MSG_MOD + 'Installing module ' + module_name);
37-
38-
var canonic_module_name = Utility.getCanonicModuleName(module_name);
39-
40-
if (module_name == 'v8-profiler' || module_name == 'profiler') {
41-
installLangModule('v8-profiler-node8', function(err) {
42-
if (err) {
43-
Common.printError(cst.PREFIX_MSG_MOD_ERR + chalk.bold.green('Profiling installation has FAILED (checkout previous logs)'));
44-
return cb(err);
43+
Modularizer.install = function (CLI, moduleName, opts, cb) {
44+
Common.printOut(cst.PREFIX_MSG_MOD + 'Installing module ' + moduleName);
45+
46+
var canonicModuleName = Utility.getCanonicModuleName(moduleName);
47+
48+
if (KNOWN_MODULES.hasOwnProperty(moduleName)) {
49+
var currentModule = KNOWN_MODULES[moduleName];
50+
51+
if (currentModule && currentModule.hasOwnProperty('dependencies')) {
52+
var functionList = [];
53+
for (var i = 0; i < currentModule.dependencies.length; i++) {
54+
functionList.push((function (index) {
55+
return function (callback) {
56+
installModuleByName(currentModule.dependencies[index], function (err) {
57+
callback(null, {module: currentModule.dependencies[index], err: err});
58+
}, false);
59+
};
60+
})(i));
4561
}
4662

47-
Common.printOut(cst.PREFIX_MSG + chalk.bold.green('V8 profiling ENABLED'));
48-
return cb();
49-
});
50-
return false;
51-
}
63+
async.parallel(functionList, function (err, results) {
64+
for (var i = 0; i < results.length; i++) {
65+
if (results[i].err) {
66+
err = results[i].err;
67+
Common.printError(cst.PREFIX_MSG_MOD_ERR + chalk.bold.green(results[i].module + ' installation has FAILED (checkout previous logs)'));
68+
} else {
69+
Common.printOut(cst.PREFIX_MSG + chalk.bold.green(results[i].module + ' ENABLED'));
70+
}
71+
}
5272

53-
if (module_name === 'gc-stats') {
54-
installLangModule('gc-stats', function(err) {
55-
if (err) {
56-
Common.printError(cst.PREFIX_MSG_MOD_ERR + chalk.bold.green('gc-stats installation has FAILED (checkout previous logs)'));
57-
return cb(err);
58-
}
73+
cb(err);
74+
});
75+
} else {
76+
installModuleByName(currentModule.name, cb);
77+
}
5978

60-
Common.printOut(cst.PREFIX_MSG + chalk.bold.green('gc-stats ENABLED'));
61-
return cb();
62-
});
6379
return false;
6480
}
6581

66-
if (module_name === 'event-loop-inspector') {
67-
installLangModule('event-loop-inspector', function(err) {
68-
if (err) {
69-
Common.printError(cst.PREFIX_MSG_MOD_ERR + chalk.bold.green('event-loop-inspector installation has FAILED (checkout previous logs)'));
70-
return cb(err);
71-
}
72-
73-
Common.printOut(cst.PREFIX_MSG + chalk.bold.green('event-loop-inspector ENABLED'));
74-
return cb();
75-
});
82+
if (moduleName === 'v8-profiler' || moduleName === 'profiler') {
83+
installModuleByName('v8-profiler-node8', cb);
7684
return false;
7785
}
7886

79-
if (module_name.indexOf('typescript') > -1) {
87+
if (moduleName.indexOf('typescript') > -1) {
8088
// Special dependency install
81-
return installLangModule(module_name, function(e) {
82-
installLangModule('ts-node@latest', function(e) {
89+
return installLangModule(moduleName, function (e) {
90+
installLangModule('ts-node@latest', function (e) {
8391
Common.printOut(cst.PREFIX_MSG + chalk.bold.green('Typescript support enabled'));
8492
return cb(e);
8593
});
8694
});
8795
}
8896

89-
if (module_name == 'livescript') {
90-
return installLangModule('livescript', function(e) {
97+
if (moduleName === 'livescript') {
98+
return installLangModule('livescript', function (e) {
9199
Common.printOut(cst.PREFIX_MSG + chalk.bold.green('Livescript support enabled'));
92100
return cb(e);
93101
});
94102
}
95103

96-
if (module_name.indexOf('coffee-script') > -1) {
97-
return installLangModule(module_name, function(e) {
104+
if (moduleName.indexOf('coffee-script') > -1) {
105+
return installLangModule(moduleName, function (e) {
98106
Common.printOut(cst.PREFIX_MSG + chalk.bold.green('Coffeescript v1 support enabled'));
99107
return cb(e);
100108
});
101109
}
102110

103-
if (module_name.indexOf('coffeescript') > -1) {
104-
return installLangModule(module_name, function(e) {
111+
if (moduleName.indexOf('coffeescript') > -1) {
112+
return installLangModule(moduleName, function (e) {
105113
Common.printOut(cst.PREFIX_MSG + chalk.bold.green('Coffeescript v2 support enabled'));
106114
return cb(e);
107115
});
108116
}
109117

110-
moduleExist(CLI, canonic_module_name, function(exists) {
118+
moduleExist(CLI, canonicModuleName, function (exists) {
111119
if (exists) {
112120
// Update
113121
Common.printOut(cst.PREFIX_MSG_MOD + 'Module already installed. Updating.');
114122

115123
// Create a backup
116-
Rollback.backup(module_name);
124+
Rollback.backup(moduleName);
117125

118126
return uninstallModule(CLI, {
119-
module_name : canonic_module_name,
120-
deep_uninstall : false
121-
}, function(err) {
122-
return Modularizer.installModule(CLI, module_name, opts, cb);
127+
module_name: canonicModuleName,
128+
deep_uninstall: false
129+
}, function () {
130+
return Modularizer.installModule(CLI, moduleName, opts, cb);
123131
});
124132
}
125133

126134
// Install
127-
Modularizer.installModule(CLI, module_name, opts, cb);
128-
})
135+
Modularizer.installModule(CLI, moduleName, opts, cb);
136+
});
129137
};
130138

131139
Modularizer.installModule = function(CLI, module_name, opts, cb) {
@@ -569,6 +577,26 @@ Modularizer.generateSample = function(app_name, cb) {
569577
});
570578
};
571579

580+
function installModuleByName (moduleName, cb, verbose) {
581+
if (!moduleName || moduleName.length === 0) {
582+
return cb(new Error('No module name !'));
583+
}
584+
585+
if (typeof verbose === 'undefined') {
586+
verbose = true;
587+
}
588+
589+
installLangModule(moduleName, function (err) {
590+
if (err) {
591+
if (verbose) { Common.printError(cst.PREFIX_MSG_MOD_ERR + chalk.bold.green(moduleName + ' installation has FAILED (checkout previous logs)')); }
592+
return cb(err);
593+
}
594+
595+
if (verbose) { Common.printOut(cst.PREFIX_MSG + chalk.bold.green(moduleName + ' ENABLED')); }
596+
return cb();
597+
});
598+
}
599+
572600
function installLangModule(module_name, cb) {
573601
var node_module_path = path.resolve(path.join(__dirname, '../../../'));
574602
Common.printOut(cst.PREFIX_MSG_MOD + 'Calling ' + chalk.bold.red('[NPM]') + ' to install ' + module_name + ' ...');

0 commit comments

Comments
 (0)