Skip to content

Commit 8822d7a

Browse files
committed
now tracking locals via global object to permit multiple cservice modules. added examples
1 parent d28d4bd commit 8822d7a

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

cluster-service.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var cluster = require("cluster");
22
var colors = require("colors");
3-
var locals = require("./lib/defaults");
3+
if (!('cservice' in global)) {
4+
global.cservice = { locals: require("./lib/defaults") };
5+
}
46

57
module.exports = exports;
68

@@ -29,13 +31,13 @@ Object.defineProperty(exports, "isWorker", {
2931

3032
Object.defineProperty(exports, "options", {
3133
get: function() {
32-
return locals.options;
34+
return global.cservice.locals.options;
3335
}
3436
});
3537

3638
Object.defineProperty(exports, "locals", {
3739
get: function() {
38-
return locals;
40+
return global.cservice.locals;
3941
}
4042
});
4143

@@ -65,7 +67,7 @@ if (
6567
// it's avail
6668
setTimeout(function() {
6769
cluster.worker.module = require(process.env.worker);
68-
if (locals.workerReady === undefined
70+
if (global.cservice.locals.workerReady === undefined
6971
&& process.env.ready.toString() === "false") {
7072
// if workerReady not invoked explicitly, inform master worker is ready
7173
exports.workerReady();

examples/lazy.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var cservice = require("cluster-service");
2+
3+
cservice.workerReady(false);
4+
5+
var http = require('http');
6+
http.createServer(function (req, res) {
7+
res.writeHead(200, {'Content-Type': 'text/plain'});
8+
res.end('Hello World\n');
9+
}).listen(1337, '127.0.0.1', onReady);
10+
11+
function onReady() {
12+
setTimeout(function() {
13+
console.log('Server running at http://127.0.0.1:1337/');
14+
cservice.workerReady();
15+
}, 1000);
16+
}

examples/server.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var http = require('http');
2+
http.createServer(function (req, res) {
3+
res.writeHead(200, {'Content-Type': 'text/plain'});
4+
res.end('Hello World\n');
5+
}).listen(1337, '127.0.0.1');
6+
console.log('Server running at http://127.0.0.1:1337/');

examples/slow.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var http = require('http');
2+
http.createServer(function (req, res) {
3+
setTimeout(function() {
4+
res.writeHead(200, {'Content-Type': 'text/plain'});
5+
res.end('Hello World\n');
6+
}, 5000);
7+
}).listen(1337, '127.0.0.1');
8+
console.log('Server running at http://127.0.0.1:1337/');

scripts/start.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("../cluster-service").start();

0 commit comments

Comments
 (0)