Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change Rack.start() to accept a number as the total workers to launch

and a callback to run as the master (instead of listenning for
"master-start"). Both parameters are optional and can be used in any
order.

Also, change Rack.worker() to have a string parameter as the (relative)
path to a node file to run as worker. This way you also don't have to
listen for the "worker-start" event.

Check require.js example.
  • Loading branch information...
commit 88866d5fe2a0bb5a8f423b8c5ca11b50419eb44d 1 parent d59a48f
@dresende authored
Showing with 52 additions and 4 deletions.
  1. +9 −0 examples/require.js
  2. +11 −0 examples/require_worker.js
  3. +32 −4 lib/rack.js
View
9 examples/require.js
@@ -0,0 +1,9 @@
+var rack = require("../lib/rack").create();
+
+// path to run for every worker
+rack.worker("./require_worker");
+
+// start 3 workers
+rack.start(3, function (rack) {
+ console.log("[%d] master started", rack.pid);
+});
View
11 examples/require_worker.js
@@ -0,0 +1,11 @@
+// do not run this directly. Although it works, the
+// intention is to run: node require.js
+
+console.log("[%d] worker started", process.pid);
+
+require("http").createServer(function (req, res) {
+ console.log("[%d] %s", process.pid, req.url);
+
+ res.writeHead(200, { "Content-Type": "text/plain" });
+ res.end("Your requested " + req.url);
+}).listen(1337);
View
36 lib/rack.js
@@ -1,6 +1,7 @@
var util = require("util"),
events = require("events"),
cluster = require("cluster"),
+ path = require("path"),
rack = null;
module.exports = rack = {
@@ -16,11 +17,23 @@ function Rack(cores) {
events.EventEmitter.call(this);
this._workers = {};
+ this._worker_path = null;
}
util.inherits(Rack, events.EventEmitter);
-Rack.prototype.start = function (workers) {
- workers || (workers = rack.cores());
+Rack.prototype.start = function () {
+ var cb = function () {},
+ workers = rack.cores();
+
+ for (var i = 0; i < arguments.length; i++) {
+ switch (typeof arguments[i]) {
+ case "function":
+ cb = arguments[i];
+ break;
+ case "number":
+ workers = arguments[i];
+ }
+ }
if (cluster.isMaster) {
Object.defineProperty(this, "pid", {
@@ -38,8 +51,14 @@ Rack.prototype.start = function (workers) {
for (var i = 0; i < workers; i++) {
this.worker();
}
+
+ cb(this);
} else {
- this.emit("worker-start", process);
+ if (this._worker_path !== null) {
+ require(this._worker_path);
+ } else {
+ this.emit("worker-start", process);
+ }
process.on("message", (function (rack) {
return function (msg) {
@@ -51,6 +70,8 @@ Rack.prototype.start = function (workers) {
}
})(this));
}
+
+ return this;
};
Rack.prototype.monitor = function (max, freq) {
if (!this._monitor) {
@@ -61,7 +82,14 @@ Rack.prototype.monitor = function (max, freq) {
this._monitor.start(max, freq);
};
Rack.prototype.worker = function () {
- var worker = cluster.fork();
+ if (arguments.length > 0 && typeof arguments[0] == "string") {
+ // do not start a worker, just save a worker
+ // file path to require later
+ this._worker_path = path.join(path.dirname(require.main.filename), arguments[0]);
+ return this;
+ }
+
+ var worker = cluster.fork();
worker.on("message", (function (rack) {
return function (msg) {
Please sign in to comment.
Something went wrong with that request. Please try again.