Skip to content

Commit

Permalink
add programmatic examples - fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed Aug 9, 2014
1 parent 826e106 commit 4ca87de
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 4 deletions.
46 changes: 46 additions & 0 deletions examples/auto-bench.js
Original file line number Diff line number Diff line change
@@ -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();
}
30 changes: 30 additions & 0 deletions examples/auto-save.js
Original file line number Diff line number Diff line change
@@ -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);
8 changes: 7 additions & 1 deletion examples/custom_action.js
Original file line number Diff line number Diff line change
@@ -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});
Expand Down
46 changes: 46 additions & 0 deletions examples/http-trace.js
Original file line number Diff line number Diff line change
@@ -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();
}
24 changes: 24 additions & 0 deletions examples/keymetrics-test.js
Original file line number Diff line number Diff line change
@@ -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); });
});
//});
});
});
});
});
});
});
});
});
7 changes: 6 additions & 1 deletion lib/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)){
Expand Down Expand Up @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion lib/God/ClusterMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/Interactor/InteractorDaemonizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
4 changes: 4 additions & 0 deletions lib/Satan.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down

0 comments on commit 4ca87de

Please sign in to comment.