Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dresende committed Dec 14, 2011
0 parents commit f7a49d7
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (C) 2011 by Diogo Resende

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
20 changes: 20 additions & 0 deletions README.md
@@ -0,0 +1,20 @@
## NodeJS Cluster Abstraction Layer

This is an abstraction layer based on EventEmitter to provide a
simple way of dealing with cluster support added on v0.6 of NodeJS.

## Install

npm install rack

## Usage

var rack = require("rack").create();

rack.on("master-start", function () {
console.log("master starting..");
});
rack.on("worker-start", function () {
console.log("worker starting..");
});
rack.start();
26 changes: 26 additions & 0 deletions example/messages.js
@@ -0,0 +1,26 @@
var rack = require("../lib/rack").create();

rack.on("master-start", startMaster);
rack.on("worker-start", startWorker);
rack.start();

function startMaster(rack) {
console.log("[%d] master started", rack.pid);

rack.on("worker-message", function (msg, worker) {
console.log("[%d (worker)] -> [%d (master)] %j", worker.pid, rack.pid, msg);
});
}

function startWorker(worker) {
console.log("[%d] worker started", worker.pid);

require("http").createServer(function (req, res) {
worker.send({ url: req.url });

console.log("[%d] %s", worker.pid, req.url);

res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Your requested " + req.url);
}).listen(1337);
}
20 changes: 20 additions & 0 deletions example/simple.js
@@ -0,0 +1,20 @@
var rack = require("../lib/rack").create();

rack.on("master-start", startMaster);
rack.on("worker-start", startWorker);
rack.start();

function startMaster(rack) {
console.log("[%d] master started", rack.pid);
}

function startWorker(worker) {
console.log("[%d] worker started", worker.pid);

require("http").createServer(function (req, res) {
console.log("[%d] %s", worker.pid, req.url);

res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Your requested " + req.url);
}).listen(1337);
}
55 changes: 55 additions & 0 deletions lib/rack.js
@@ -0,0 +1,55 @@
var util = require("util"),
events = require("events"),
cluster = require("cluster"),
rack = null;

module.exports = rack = {
cores: function () {
return require("os").cpus().length;
},
create: function () {
return new Rack();
}
}

function Rack(cores) {
events.EventEmitter.call(this);
}
util.inherits(Rack, events.EventEmitter);

Rack.prototype.start = function (workers) {
workers || (workers = rack.cores());

if (cluster.isMaster) {
Object.defineProperty(this, "pid", {
value: process.pid,
writable: false
});
this.emit("master-start", this);

cluster.on("death", (function (rack) {
return function (worker) {
rack.emit("worker-end", worker, rack);
}
})(this));

for (var i = 0; i < workers; i++) {
this.worker();
}
} else {
this.emit("worker-start", process);
}
};
Rack.prototype.worker = function () {
var worker = cluster.fork();

worker.on("message", (function (rack) {
return function (msg) {
if (msg.hasOwnProperty("_queryId")) {
// ignore core messages
return;
}
rack.emit("worker-message", msg, worker, rack);
}
})(this));
};
20 changes: 20 additions & 0 deletions package.json
@@ -0,0 +1,20 @@
{
"name": "rack",
"version": "0.1.0",
"description": "NodeJS Cluster Abstraction Layer",
"keywords": [ "cluster" ],
"author": "Diogo Resende <dresende@thinkdigital.pt>",
"engines": { "node": ">= 0.6.0" },
"repository": {
"type": "git",
"url": "http://github.com/dresende/node-rack.git"
},
"directories": {
"lib": "./lib"
},
"main": "./lib/index",
"licenses": [{
"type": "MIT",
"url": "http://github.com/dresende/node-rack/raw/master/LICENSE"
}]
}

0 comments on commit f7a49d7

Please sign in to comment.