Skip to content

Commit

Permalink
March 2
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcli committed Mar 3, 2015
1 parent b3fcd6b commit ee789fb
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
11 changes: 11 additions & 0 deletions node-7-scaling/child-exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var exec = require('child_process').exec;

var child = exec('uptime | 23cut -d "," -f 1', function(err, stdout, stderr) {
if (err) {
console.log(stderr);
} else {
console.log('Output is: ' + stdout);
}
});

console.log("PID is: " + child.pid);
13 changes: 13 additions & 0 deletions node-7-scaling/child-fork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var fork = require('child_process').fork;

var child = fork(__dirname + '/honourstudent.js');

child.on('message', function(m) {
console.log('The answer is: ', m.answer);
child.send({cmd: 'done'});
});

child.send({cmd: 'double', number:20});


console.log('PID is ' + child.pid);
14 changes: 14 additions & 0 deletions node-7-scaling/child-spawn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var spawn = require('child_process').spawn,
ps = spawn('ps', ['ax']),
grep = spawn('grep', ['node']);

ps.stdout.pipe(grep.stdin);
grep.stdout.pipe(process.stdout);

ps.stderr.on('data', function(data) {
console.log('ps stderr: ' + data);
});

grep.stderr.on('data', function (data) {
console.log('grep stderr: ' + data);
});
47 changes: 47 additions & 0 deletions node-7-scaling/cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var cluster = require('cluster');
var http = require('http');
var numWorkers = 2;

if (cluster.isMaster) {
// Fork Workers
for (var i=0; i<numWorkers; i++) {
console.log('master: about to fork a worker');
cluster.fork();
}

cluster.on('fork', function(worker) {
console.log('master: fork event (worker ' + worker.id + ')');
});

cluster.on('online', function(worker) {
console.log('master: online event (worker ' + worker.id + ')');
});

cluster.on('listening', function(worker, address) {
console.log('master: listening event (worker ' + worker.id + ', pid ' + worker.process.pid + ', ' + address.address + ':' + address.port + ')');
});

cluster.on('exit', function(worker, code, signal) {
console.log('master: exit event (worker ' + worker.id + ')');
});
} else {

console.log('worker: worker#' + cluster.worker.id + ' ready!');

var count = 0;

// Workers can share any TCP connection
// In this case its a HTTP server
http.createServer(function(req, res) {
res.writeHead(200);
// count.dir(req);
count++;
console.log('Worker # ' + cluster.worker.id + 'is incrementing count to ' + count);
res.end('Hello world from worker #' + cluster.worker.id + ' (pid ' + cluster.worker.process.id + ')');
if (count === 3) {
cluster.worker.destroy();
}
}).listen(process.env.PORT, process.env.IP);

}

13 changes: 13 additions & 0 deletions node-7-scaling/honourstudent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var fun = require('./mathfun');

process.on('message', function(m) {
console.log("Child pid is: " + process.pid);
if (m.cmd === 'double') {
console.log('hs: I was asked to double ' + m.number);
fun.evenDoubler(m.number, function(err, res) {
process.send({answer: res});
});
} else if (m.cmd === 'done') {
process.exit();
}
});
27 changes: 27 additions & 0 deletions node-7-scaling/mathfun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var maxTime = 200;

var evenDoubler = function(v, callback){
var randomTime = Math.floor(Math.random()*(maxTime+1));
if( v % 2 == 0) {
setTimeout(function(){
callback(null, v * 2, randomTime);
}, randomTime);
} else {
var err = new Error("odd number");
setTimeout(function(){
callback(err);
}, randomTime);
}
};

var evenDoublerSync = function(v) {
if (v%2) throw (new Error("ODD INPUT"));
else return v*2;
}

module.exports.evenDoubler = evenDoubler;
module.exports.evenDoublerSync = evenDoublerSync;
module.exports.foo = "bar";

//evenDoubler(2, handleResults);
//evenDoubler(1, handleResults);

0 comments on commit ee789fb

Please sign in to comment.