Skip to content

Commit

Permalink
Merge pull request #3 from statful/add-pm2-internal-dependency
Browse files Browse the repository at this point in the history
Add pm2 internal dependency
  • Loading branch information
mistic authored Oct 6, 2016
2 parents 0fc7e98 + 950944e commit 6f7cc30
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
40 changes: 39 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var Config = require('./lib/config.js').default;
var StatfulCollectorAws = require('./lib/collector.js').default;
var fs = require('fs-extra');
var path = require('path');
var pm2 = require('pm2');

var generateConfig = function(configPath) {
var normalizedPath = path.normalize(configPath);
Expand Down Expand Up @@ -43,16 +44,43 @@ var start = function(configPath) {
});
};

var startManaged = function(configPath) {
return new Promise(function(resolve, reject) {
pm2.connect(function(err) {
if (err) {
reject(err);
} else {
pm2.start({
script: 'statful-collector-aws',
args: 'start ' + configPath
}, function(err) {
pm2.disconnect();

if (err) {
reject(err);
} else {
resolve();
}
});
}
});


});
};

var cli = function() {
var yargs = require('yargs')
.usage('Usage: $0 [command] <path>')
.command('generate-config <path>', 'Generate a default config for Statful Collector AWS on given path.')
.command('start <path>', 'Start the Statful Collector AWS with a config on the given path.')
.command('start-managed <path>', 'Start the Statful Collector AWS, managed by pm2, with a config on the given path.')
.demand(1)
.strict()
.example('$0 generate-config /etc/statful-collector-aws/conf', 'Generates a default Statful Collector AWS ' +
'config file on /etc/statful-collector-aws/conf/ with name statful-collector-aws-conf.json')
.example('$0 start /etc/statful-collector-aws/conf/statful-collector-aws-conf.json', 'Starts the Statful Collector AWS with given config.')
.example('$0 start /etc/statful-collector-aws/conf/statful-collector-aws-conf.json', 'Starts the Statful Collector AWS with the given config.')
.example('$0 start-managed /etc/statful-collector-aws/conf/statful-collector-aws-conf.json', 'Starts the Statful Collector AWS, managed by pm2, with the given config.')
.help('help')
.alias('h', 'help')
.epilog('Copyright 2016 Statful.');
Expand All @@ -69,6 +97,15 @@ var cli = function() {
return console.error(error);
}
);
} else if (argv._[0] === "start-managed") {
startManaged(path).then(
function() {
return console.log('Pm2 successfully request a spawn for Statful Collector AWS process.');
},
function(error) {
return console.error(error);
}
);
} else if (argv._[0] === "generate-config") {
generateConfig(path).then(
function(returnedPath) {
Expand All @@ -84,4 +121,5 @@ var cli = function() {

exports.generateConfig = generateConfig;
exports.start = start;
exports.startManaged = startManaged;
exports.cli = cli;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@
"fs-extra": "^0.30.0",
"jsonschema": "^1.1.0",
"lodash": "^4.15.0",
"pm2": "^2.0.18",
"source-map-support": "^0.4.0",
"statful-client": "~4.1.2",
"statful-client": "~4.1.3",
"yargs": "^5.0.0"
},
"repository": {
Expand Down

0 comments on commit 6f7cc30

Please sign in to comment.