From aab4a50aaca2156ad62730066c3ee15362d7d177 Mon Sep 17 00:00:00 2001 From: Vladimir Dronnikov Date: Tue, 1 Mar 2011 08:01:12 -0500 Subject: [PATCH 1/3] coffee support --- lib/master.js | 3 ++- lib/plugins/reload.js | 2 +- lib/worker.js | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/master.js b/lib/master.js index 93527aa..80287e4 100644 --- a/lib/master.js +++ b/lib/master.js @@ -41,7 +41,7 @@ if (!net.isIP) net.isIP = binding.isIP; * Node binary. */ -var node = process.execPath +var node = process.execPath; /** * Start a new `Master` with the given `server` or filename to @@ -94,6 +94,7 @@ var Master = module.exports = function Master(server) { // grab server root this.cmd = process.argv.slice(1); + if (this.cmd[0].slice(-7) === '.coffee') node = 'coffee' this.dir = dirname(this.cmd[0]); // defaults diff --git a/lib/plugins/reload.js b/lib/plugins/reload.js index 0a50bae..ea67268 100644 --- a/lib/plugins/reload.js +++ b/lib/plugins/reload.js @@ -97,7 +97,7 @@ exports = module.exports = function(files, options){ // watch file for changes function watch(file) { // js-only for now - if (!~file.indexOf('.js')) return; + if (!~file.indexOf('.js') && !~file.indexOf('.coffee')) return; fs.watchFile(file, { interval: interval }, function(curr, prev){ if (restarting) return; if (curr.mtime > prev.mtime) { diff --git a/lib/worker.js b/lib/worker.js index 6cbf794..937d824 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -22,7 +22,7 @@ net.Socket = net.Stream; * Node binary. */ -var node = process.execPath +var node = process.execPath; /** * Initialize a new `Worker` with the given `master`. @@ -173,6 +173,7 @@ Worker.prototype.spawn = function(){ , customFds = [fds[0]].concat(this.master.customFds); // spawn worker process + if (this.master.cmd[0].slice(-7) === '.coffee') node = 'coffee' this.proc = spawn(node, this.master.cmd, { customFds: customFds }); this.proc.stdin = new net.Socket(fds[0]); From 713268aa89a4bcbbd87233e6973b8c03933b9aff Mon Sep 17 00:00:00 2001 From: Vladimir Dronnikov Date: Tue, 1 Mar 2011 09:21:21 -0500 Subject: [PATCH 2/3] attempt to support spawn(-n) to reduce workers --- lib/master.js | 9 ++++++++- lib/plugins/repl.js | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/master.js b/lib/master.js index 80287e4..be77459 100644 --- a/lib/master.js +++ b/lib/master.js @@ -428,7 +428,14 @@ Master.prototype.maintainWorkerCount = function(){ */ Master.prototype.spawn = function(n){ - while (n--) this.spawnWorker(); + // add fresh workers + if (n > 0) { + while (n--) this.spawnWorker(); + // remove excess workers + } else if (n < 0) { + this.options.workers = Math.max(this.options.workers + n, 0); + this.kill();//'SIGQUIT'); + } }; /** diff --git a/lib/plugins/repl.js b/lib/plugins/repl.js index f7d7880..fb77b8e 100644 --- a/lib/plugins/repl.js +++ b/lib/plugins/repl.js @@ -135,7 +135,7 @@ define('spawn', function(master, sock, n){ n = n || 1; sock.write('spawning ' + n + ' worker' + (n > 1 ? 's' : '') + '\n'); master.spawn(n); -}, 'Spawn one or more additional workers'); +}, 'Spawn one or more additional workers, or kill excess workers'); /** * Output process ids. From 6f27768499188e1770363444eae9107ec1e94a92 Mon Sep 17 00:00:00 2001 From: Vladimir Dronnikov Date: Tue, 5 Apr 2011 08:15:44 -0400 Subject: [PATCH 3/3] REPL for console --- lib/plugins/repl.js | 28 ++++++++++++++++++++-------- lib/plugins/stats.js | 2 +- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/plugins/repl.js b/lib/plugins/repl.js index 7c410eb..b25573a 100644 --- a/lib/plugins/repl.js +++ b/lib/plugins/repl.js @@ -32,7 +32,7 @@ var net = require('net') exports = module.exports = function(){ var args = arguments; - if (!args.length) throw new Error('repl() plugin requires port/host or path'); + //if (!args.length) throw new Error('repl() plugin requires port/host or path'); return function(master){ var server , sockets = []; @@ -40,10 +40,13 @@ exports = module.exports = function(){ // start repl function start(){ - // TCP or unix-domain socket repl - server = net.createServer(function(sock){ - sockets.push(sock); + + function REPL(sock) { var ctx = repl.start('cluster> ', sock).context; + // console.repl? + if (!sock) { sock = process.stdout; } + + sockets.push(sock); master.emit('repl socket', sock); // augment socket to provide some formatting methods @@ -59,10 +62,19 @@ exports = module.exports = function(){ return exports[cmd].apply(master, args); }; }); - }); + } + + // console repl + if (!args.length) { + process.stdin.on('close', process.exit); + REPL(); + // TCP or unix-domain socket repl + } else { + server = net.createServer(REPL); + // Apply all arguments given + server.listen.apply(server, args); + } - // Apply all arguments given - server.listen.apply(server, args); } // initial master starts immediately @@ -86,7 +98,7 @@ exports = module.exports = function(){ sockets.forEach(function(sock){ sock.fd && sock.end(); }); - if (server.fd) server.close(); + if (server && server.fd) server.close(); }); } }; diff --git a/lib/plugins/stats.js b/lib/plugins/stats.js index 052033f..6dc55fc 100644 --- a/lib/plugins/stats.js +++ b/lib/plugins/stats.js @@ -10,7 +10,7 @@ */ var fs = require('fs') - , Log = require('log') +// , Log = require('log') , repl = require('./repl') , utils = require('../utils') , os;