proteus-cluster
Advanced tools
Comparing version 1.0.6 to 1.1.0
@@ -46,73 +46,70 @@ /** | ||
// set worker settings | ||
var setup = { | ||
exec : conf.exec, | ||
args : conf.args | ||
}; | ||
logger.info('setup master', JSON.stringify(setup)); | ||
cluster.setupMaster(setup); | ||
logger.debug('starting master ' + process.pid); | ||
// setup event will be called only once | ||
cluster.on('setup', function(){ | ||
_disconnectTimeout = conf.disconnectTimeout || DEFAULT_DISCONNECT_TIMEOUT; | ||
_maxForkCount = conf.maxForkCount || DEFAULT_MAX_FORK_COUNT; | ||
_disconnectTimeout = conf.disconnectTimeout || DEFAULT_DISCONNECT_TIMEOUT; | ||
_maxForkCount = conf.maxForkCount || DEFAULT_MAX_FORK_COUNT; | ||
logger.info('disconnect timeout : ' + _disconnectTimeout); | ||
logger.info('max fork count : ' + _maxForkCount); | ||
logger.info('disconnect timeout : ' + _disconnectTimeout); | ||
logger.info('max fork count : ' + _maxForkCount); | ||
// start all workers | ||
var workerNum = conf.worker || os.cpus().length; | ||
for (var i = 0; i < workerNum; i++) { | ||
fork(); | ||
} | ||
// start all workers | ||
var workerNum = conf.worker || os.cpus().length; | ||
for (var i = 0; i < workerNum; i++) { | ||
fork(); | ||
} | ||
// create pid file | ||
if (conf.pid) { | ||
_pidfile = conf.pid; | ||
logger.info('create pid file : ' + _pidfile); | ||
// create pid file | ||
if (conf.pid) { | ||
_pidfile = conf.pid; | ||
logger.info('create pid file : ' + _pidfile); | ||
fs.writeFileSync(_pidfile, String(process.pid), 'utf8'); | ||
} | ||
fs.writeFileSync(_pidfile, String(process.pid), 'utf8'); | ||
} | ||
// initialize API listener | ||
if ('api' in conf) { | ||
setupAPI(conf.api); | ||
} | ||
// initialize API listener | ||
if ('api' in conf) { | ||
setupAPI(conf.api); | ||
} | ||
// when worker has been exited | ||
cluster.on('exit', function(worker, code, signal) { | ||
// when worker has been exited | ||
cluster.on('exit', function(worker, code, signal) { | ||
logger.debug('worker exited', { pid: worker.process.pid, id: worker.id }); | ||
logger.debug('worker exited', { pid: worker.process.pid, id: worker.id }); | ||
// clear disconnect timer if exists | ||
if (_disconnectTimer[worker.id]) { | ||
clearTimeout(_disconnectTimer[worker.id]); | ||
delete _disconnectTimer[worker.id]; | ||
} | ||
// clear disconnect timer if exists | ||
if (_disconnectTimer[worker.id]) { | ||
clearTimeout(_disconnectTimer[worker.id]); | ||
delete _disconnectTimer[worker.id]; | ||
} | ||
// if worker was set as restart, or killed accidentally fork/shutdown | ||
if (worker.restart || !worker.suicide) { | ||
// if worker was set as restart, or killed accidentally fork/shutdown | ||
if (worker.restart || !worker.suicide) { | ||
// if worker was forked less than max count, fork again | ||
if (_forkedCount < _maxForkCount) { | ||
return fork(); | ||
} | ||
logger.info('too many forked workers. going to shutdown.'); | ||
return shutdown(); | ||
// if worker was forked less than max count, fork again | ||
if (_forkedCount < _maxForkCount) { | ||
return fork(); | ||
} | ||
logger.info('too many forked workers. going to shutdown.'); | ||
return shutdown(); | ||
} | ||
// if worker was killed and not set as restart, do nothing | ||
}); | ||
// attache signals | ||
process.on('SIGUSR2', restart); // restart workers | ||
process.on('SIGHUP', function() {}); // ignore SIGHUP for background processing | ||
process.on('SIGINT', shutdown); // exit with signal 0 | ||
process.on('SIGTERM', shutdown); // exit with signal 0 | ||
// if worker was killed and not set as restart, do nothing | ||
}); | ||
// set worker settings | ||
var setup = { | ||
exec : conf.exec, | ||
args : conf.args | ||
}; | ||
logger.info('setup master', JSON.stringify(setup)); | ||
// attache signals | ||
process.on('SIGUSR2', restart); // restart workers | ||
process.on('SIGHUP', function() {}); // ignore SIGHUP for background processing | ||
process.on('SIGINT', shutdown); // exit with signal 0 | ||
process.on('SIGTERM', shutdown); // exit with signal 0 | ||
cluster.setupMaster(setup); | ||
} | ||
@@ -158,3 +155,3 @@ | ||
server.listen(port, function() { | ||
logger.info('Cluster HTTP-API is listening on 0.0.0.0:' + listen); | ||
logger.info('Cluster HTTP-API is listening on 0.0.0.0:' + port); | ||
}); | ||
@@ -161,0 +158,0 @@ } |
{ | ||
"name": "proteus-cluster", | ||
"version": "1.0.6", | ||
"scripts": { | ||
"start": "node app", | ||
"test": "find test -type f -name 'test*.js' -print | xargs ./node_modules/.bin/mocha --reporter tap" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ameba-proteus/proteus-cluster.git" | ||
}, | ||
"homepage": "https://github.com/ameba-proteus/proteus-cluster", | ||
"dependencies": { | ||
"proteus-logger": "<2.0.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"should": "*" | ||
} | ||
"name": "proteus-cluster", | ||
"version": "1.1.0", | ||
"scripts": { | ||
"start": "node app", | ||
"test": "XUNIT_FILE=test-report.xml node_modules/.bin/mocha --reporter xunit-file", | ||
"cobertura": "node_modules/.bin/istanbul cover --report cobertura node_modules/mocha/bin/_mocha -- --reporter dot", | ||
"cover": "node_modules/.bin/istanbul cover --print none --report html node_modules/mocha/bin/_mocha && open coverage/index.html", | ||
"checkstyle": "jshint --checkstyle-reporter . > checkstyle-jshint.xml || exit 0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ameba-proteus/proteus-cluster.git" | ||
}, | ||
"homepage": "https://github.com/ameba-proteus/proteus-cluster", | ||
"dependencies": { | ||
"proteus-logger": "<2.0.0" | ||
}, | ||
"devDependencies": { | ||
"expect.js": "^0.3.0", | ||
"istanbul": "^0.1.45", | ||
"mocha": "*", | ||
"should": "*", | ||
"xunit-file": "^0.0.5" | ||
} | ||
} |
@@ -57,3 +57,3 @@ proteus-cluster | ||
```js | ||
var cluster = require('proteus').cluster; | ||
var cluster = require('proteus-cluster'); | ||
var conf = {}; | ||
@@ -69,3 +69,3 @@ conf.worker = 4; | ||
port : 8111 | ||
}); | ||
}; | ||
cluster(conf); | ||
@@ -80,6 +80,4 @@ ``` | ||
var port = 8080; | ||
app.configure(function() { | ||
app.get('/', function(req, res) { | ||
res.send('running worker '+process.pid); | ||
}); | ||
app.get('/', function(req, res) { | ||
res.send('running worker '+process.pid); | ||
}); | ||
@@ -226,3 +224,3 @@ app.listen(port); | ||
```js | ||
var cluster = require('proteus').cluster; | ||
var cluster = require('proteus-cluster'); | ||
var conf = {}; | ||
@@ -236,5 +234,5 @@ conf.worker = 4; | ||
conf.api = { | ||
listen : '0.0.0.0', | ||
port : 8111 | ||
}); | ||
listen : '0.0.0.0', | ||
port : 8111 | ||
}; | ||
cluster(conf); | ||
@@ -246,10 +244,7 @@ ``` | ||
```js | ||
// 通常通りの実装を行う | ||
var express = require('express'); | ||
var app = express(); | ||
var port = 8080; | ||
app.configure(function() { | ||
app.get('/', function(req, res) { | ||
res.send('running worker '+process.pid); | ||
}); | ||
app.get('/', function(req, res) { | ||
res.send('running worker '+process.pid); | ||
}); | ||
@@ -256,0 +251,0 @@ app.listen(port); |
@@ -0,2 +1,5 @@ | ||
'use strict'; | ||
/* global describe,it,before,beforeEach */ | ||
var cluster = require('..'); | ||
var expect = require('expect.js'); | ||
var http = require('http'); | ||
@@ -6,22 +9,21 @@ | ||
loggers.configure({ | ||
appenders: { | ||
console: { | ||
type: 'console' | ||
}, | ||
file: { | ||
type: 'rotate_file', | ||
layout: { | ||
pattern: "%yyyy-%MM-%dd%T%HH:%mm:%ss %level %logger %msg %args (%line)%nstack%n" | ||
} | ||
} | ||
}, | ||
loggers: { | ||
"default": { | ||
appenders: ["file","console"], | ||
level: "debug" | ||
} | ||
} | ||
appenders: { | ||
console: { | ||
type: 'console' | ||
}, | ||
file: { | ||
type: 'rotate_file', | ||
layout: { | ||
pattern: "%yyyy-%MM-%dd%T%HH:%mm:%ss %level %logger %msg %args (%line)%nstack%n" | ||
} | ||
} | ||
}, | ||
loggers: { | ||
"default": { | ||
appenders: ["file","console"], | ||
level: "debug" | ||
} | ||
} | ||
}); | ||
// } | ||
//}); | ||
var logger = loggers.get('cluster-test'); | ||
@@ -38,39 +40,57 @@ logger.info("TEST"); | ||
conf.api = { | ||
listen: '0.0.0.0', | ||
port: 8881 | ||
listen: '0.0.0.0', | ||
port: 8881 | ||
}; | ||
var cnt = 0; | ||
cluster.addMessageListener('fromWorker', function() { | ||
// logger.debug('[master] message received : ' + JSON.stringify(obj.msg)); | ||
cnt++; | ||
if (cnt >= 4) { | ||
cluster.shutdown(); | ||
} | ||
}); | ||
describe('cluster', function() { | ||
describe('sendMessage', function() { | ||
var cnt; | ||
cluster(conf); | ||
before(function() { | ||
cluster(conf); | ||
}); | ||
// logger.debug('[master] going to send message from master'); | ||
cluster.sendMessage({msg: 'message from master'}); | ||
beforeEach(function() { | ||
cnt = 0; | ||
}); | ||
process.nextTick(function(){ | ||
// logger.debug('[master] going to send message from master using HTTP API'); | ||
var data = JSON.stringify({'msg': 'message from master using HTTP API'}); | ||
var post = http.request({ | ||
host: 'localhost', | ||
port: 8881, | ||
path: '/send', | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Content-Length': data.length | ||
} | ||
}, function(res) { | ||
console.log("RESPONSE CLUSTER",res.statusCode); | ||
}); | ||
post.write(data); | ||
post.end(); | ||
}); | ||
cluster.addMessageListener('pong', function(obj) { | ||
logger.debug('[master] message received : ' + JSON.stringify(obj)); | ||
expect(obj.cmd).to.eql('pong'); | ||
cnt++; | ||
}); | ||
it('send message from master', function(done) { | ||
cluster.sendMessage({cmd: 'ping', msg: 'message from master'}); | ||
setTimeout(function() { | ||
expect(cnt).to.eql(conf.worker); | ||
done(); | ||
}, 2000); | ||
}); | ||
it('send message from master using HTTP API', function(done) { | ||
logger.debug('[master] going to send message from master using HTTP API'); | ||
var data = JSON.stringify({cmd: 'ping',msg: 'message from master using HTTP API'}); | ||
var post = http.request({ | ||
host: 'localhost', | ||
port: 8881, | ||
path: '/send', | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Content-Length': data.length | ||
} | ||
}, function(res) { | ||
logger.debug("RESPONSE CLUSTER",res.statusCode); | ||
}); | ||
post.write(data); | ||
post.end(); | ||
setTimeout(function() { | ||
expect(cnt).to.eql(conf.worker); | ||
done(); | ||
}, 2000); | ||
}); | ||
}); | ||
}); |
@@ -0,40 +1,42 @@ | ||
'use strict'; | ||
var loggers = require('proteus-logger'); | ||
loggers.configure({ | ||
appenders: { | ||
console: { | ||
type: 'console' | ||
}, | ||
file: { | ||
type: 'rotate_file', | ||
layout: { | ||
pattern: "%yyyy-%MM-%dd%T%HH:%mm:%ss %level %logger %msg %args (%line)%nstack%n" | ||
} | ||
} | ||
}, | ||
loggers: { | ||
"default": { | ||
appenders: ["file","console"], | ||
level: "debug" | ||
} | ||
} | ||
appenders: { | ||
console: { | ||
type: 'console' | ||
}, | ||
file: { | ||
type: 'rotate_file', | ||
layout: { | ||
pattern: "%yyyy-%MM-%dd%T%HH:%mm:%ss %level %logger %msg %args (%line)%nstack%n" | ||
} | ||
} | ||
}, | ||
loggers: { | ||
"default": { | ||
appenders: ["file","console"], | ||
level: "debug" | ||
} | ||
} | ||
}); | ||
var logger = loggers.get('cluster-test'); | ||
logger.debug(process.argv); | ||
//logger.debug(process.argv); | ||
function sendMessage() { | ||
try { | ||
// logs will be sent to the master process | ||
// logger.debug('[worker] going to send message from worker'); | ||
function sendPong() { | ||
try { | ||
logger.debug('[worker] going to send message from worker'); | ||
process.send({cmd: 'fromWorker', msg: 'message from worker ' + process.pid}); | ||
process.exit(0); | ||
} catch(e){ | ||
process.exit(0); | ||
} | ||
process.send({cmd: 'pong', msg: 'message from worker ' + process.pid}); | ||
process.exit(0); | ||
} catch(e){ | ||
process.exit(0); | ||
} | ||
} | ||
process.on('message', function(msg) { | ||
// logger.debug('[worker] message received : ' + JSON.stringify(msg)); | ||
sendMessage(); | ||
process.on('message', function(obj) { | ||
logger.debug('[worker] message received : ' + JSON.stringify(obj)); | ||
if (obj.cmd === 'ping') { | ||
sendPong(); | ||
} | ||
}); |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
19570
407
5
355