From 4ca87debf1f322c4254b01ac39177eb5b7000177 Mon Sep 17 00:00:00 2001 From: tknew2 Date: Sat, 9 Aug 2014 14:08:38 +0200 Subject: [PATCH] add programmatic examples - fixes --- examples/auto-bench.js | 46 ++++++++++++++++++++++++++ examples/auto-save.js | 30 +++++++++++++++++ examples/custom_action.js | 8 ++++- examples/http-trace.js | 46 ++++++++++++++++++++++++++ examples/keymetrics-test.js | 24 ++++++++++++++ lib/CLI.js | 7 +++- lib/God/ClusterMode.js | 2 +- lib/Interactor/InteractorDaemonizer.js | 2 +- lib/Satan.js | 4 +++ 9 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 examples/auto-bench.js create mode 100644 examples/auto-save.js create mode 100644 examples/http-trace.js create mode 100644 examples/keymetrics-test.js diff --git a/examples/auto-bench.js b/examples/auto-bench.js new file mode 100644 index 000000000..ab9d2dc62 --- /dev/null +++ b/examples/auto-bench.js @@ -0,0 +1,46 @@ + + +var http = require('http'); + +http.createServer(function(req, res) { + res.writeHead(200); + res.end('transaction'); +}).listen(9923); + + +setInterval(function() { + request(); +}, Math.floor((Math.random() * 1000))); + + +function makeid() +{ + var text = ""; + var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + for( var i=0; i < 5; i++ ) + text += possible.charAt(Math.floor(Math.random() * possible.length)); + + return text; +} + +function request(path) { + var options = { + hostname: '127.0.0.1' + ,port: 9923 + ,path: path || '/users' + ,method: 'GET' + ,headers: { 'Content-Type': 'application/json' } + }; + + var req = http.request(options, function(res) { + res.setEncoding('utf8'); + res.on('data', function (data) { + console.log(data); // I can't parse it because, it's a string. why? + }); + }); + req.on('error', function(e) { + console.log('problem with request: ' + e.message); + }); + req.end(); +} diff --git a/examples/auto-save.js b/examples/auto-save.js new file mode 100644 index 000000000..79de09f55 --- /dev/null +++ b/examples/auto-save.js @@ -0,0 +1,30 @@ + + +// Expose action +// And "touch" file every 1.4s to restart the file + +var axm = require('axm'); + +function makeid() { + var text = ""; + var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + for( var i=0; i < 5; i++ ) + text += possible.charAt(Math.floor(Math.random() * possible.length)); + + return text; +} + + +axm.action('cmd:' + makeid(), {comment : 'Refresh main database'}, function(reply) { + console.log('Refreshing'); + reply({success : true}); + }); + +setTimeout(function() { + var fs = require('fs'); + + + var a = fs.readFileSync(__filename); + fs.writeFileSync(__filename, a); +}, 1400); diff --git a/examples/custom_action.js b/examples/custom_action.js index eea4a418d..5daf95c16 100644 --- a/examples/custom_action.js +++ b/examples/custom_action.js @@ -1,11 +1,17 @@ var axm = require('axm'); -axm.action('refresh:db', {comment : 'Refresh main database'}, function(reply) { + +axm.action('refresh:db2', {comment : 'Refresh main database'}, function(reply) { console.log('Refreshing'); reply({success : true}); }); +axm.action('refresh:db3', {comment : 'Comment'}, function(reply) { + throw new Error('asdadsadsasd'); + reply({success : false}); +}); + axm.action('refresh:db', {comment : 'Refresh main database'}, function(reply) { console.log('Refreshing'); reply({success : true}); diff --git a/examples/http-trace.js b/examples/http-trace.js new file mode 100644 index 000000000..9fcca1b52 --- /dev/null +++ b/examples/http-trace.js @@ -0,0 +1,46 @@ + +var axm = require('axm'); +axm.http(); + +var http = require('http'); + +http.createServer(function(req, res) { + res.writeHead(200); + res.end('transaction'); +}).listen(9010); + +setInterval(function() { + request(['/user', '/bla', '/user/lol/delete', '/POST/POST'][Math.floor((Math.random() * 4))]); +}, 1000); + +function makeid() +{ + var text = ""; + var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + for( var i=0; i < 5; i++ ) + text += possible.charAt(Math.floor(Math.random() * possible.length)); + + return text; +} + +function request(path) { + var options = { + hostname: '127.0.0.1' + ,port: 9010 + ,path: path || '/users' + ,method: 'GET' + ,headers: { 'Content-Type': 'application/json' } + }; + + var req = http.request(options, function(res) { + res.setEncoding('utf8'); + res.on('data', function (data) { + console.log(data); // I can't parse it because, it's a string. why? + }); + }); + req.on('error', function(e) { + console.log('problem with request: ' + e.message); + }); + req.end(); +} diff --git a/examples/keymetrics-test.js b/examples/keymetrics-test.js new file mode 100644 index 000000000..d5e623cfd --- /dev/null +++ b/examples/keymetrics-test.js @@ -0,0 +1,24 @@ + +var pm2 = require('..'); + +pm2.connect(function() { + pm2.delete('all', function() { + pm2.start('examples/human_event.js', function() { + pm2.start('examples/child.js', {instances:2},function() { + pm2.start('examples/custom_action.js', function() { + pm2.start('examples/custom_action_with_params.js', function() { + pm2.start('examples/auto-save.js', {watch : true, name :'auto-save-modify'}, function() { + pm2.start('examples/http-trace.js', {name:'trace'}, function() { + //pm2.start('examples/auto-bench.js', {instances : 'max'}, function() { + pm2.start('examples/throw.js', {name:'auto-throw'}, function() { + pm2.disconnect(function() { process.exit(1); }); + }); + //}); + }); + }); + }); + }); + }); + }); + }); +}); diff --git a/lib/CLI.js b/lib/CLI.js index 58ca23920..15ef31cc5 100644 --- a/lib/CLI.js +++ b/lib/CLI.js @@ -36,6 +36,11 @@ CLI.start = function(script, opts, cb) { name : p.basename(script, '.js') }; + if (typeof opts == "function") { + cb = opts; + opts = {}; + } + if (opts.nodeArgs) //maintain backwards compat for space delimited string args if (Array.isArray(opts.nodeArgs)){ @@ -136,7 +141,7 @@ CLI.start = function(script, opts, cb) { if (cb) return cb({msg : err}); else return speedList(); } - printOut(cst.PREFIX_MSG + 'Process launched'); + printOut(cst.PREFIX_MSG + 'Process %s launched', script); if (cb) return cb(null, data); else return speedList(); }); diff --git a/lib/God/ClusterMode.js b/lib/God/ClusterMode.js index 093df8f60..ec133d5aa 100644 --- a/lib/God/ClusterMode.js +++ b/lib/God/ClusterMode.js @@ -43,7 +43,7 @@ module.exports = function(God) { try { clu = cluster.fork(env_copy); - } catch(e) { console.error(e); } + } catch(e) { return God.logAndGenerateError(e); } clu.pm2_env = env_copy; God.clusters_db[env_copy.pm_id] = clu; diff --git a/lib/Interactor/InteractorDaemonizer.js b/lib/Interactor/InteractorDaemonizer.js index a2979b1e1..a6d598347 100644 --- a/lib/Interactor/InteractorDaemonizer.js +++ b/lib/Interactor/InteractorDaemonizer.js @@ -270,7 +270,7 @@ InteractorDaemonizer.launchAndInteract = function(opts, cb) { launchOrAttach(data, function(status) { - Common.printOut(chalk.cyan.bold('[Keymetrics.io]') + ' Interactor successfully launched'); + //Common.printOut(chalk.cyan.bold('[Keymetrics.io]') + ' Interactor successfully launched'); return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); diff --git a/lib/Satan.js b/lib/Satan.js index 93472e2f4..236f88a5d 100644 --- a/lib/Satan.js +++ b/lib/Satan.js @@ -376,6 +376,10 @@ Satan.executeRemote = function(method, env, fn) { Satan.restartWatch(method, env, fn); } + if (!Satan.client || !Satan.client.call) { + console.error('Did you forgot to call pm2.connect(function() { }) before interacting with PM2 ?'); + return process.exit(0); + } Satan.client.call(method, env, fn); };