proteus-cluster
v1.1.4
Published
proteus-cluster ==============================
Downloads
9
Readme
proteus-cluster
Languages
English
About
Proteus Cluster is a module to use Node.js cluster module more conveniently.
This module collaborate with Proteus Logger to support logging under the clustered environment. See Proteus Logger for more detail.
The following are the features.
- Worker will be automatically restarted when the process aborted.
- Graceful restart/shutdown for workers.
- Collaborate with Proteus Logger to unify the log management by master.
- Message sending mechanism between mater and workers.
Usage
settings([] are the default value)
- worker
- number of workers to run [number of CPUs]
- pid
- process id file
- exec
- startup JS file for workers [__filename]
- args
- arguments to be sent when calling startup JS file (array)
- disconnectTimeout
- timeout milliseconds for worker to wait for graceful shutdown [10000]
- maxForkCount
- max fork count for worker (in case of endless restart) [100]
- api
- listen
- hostname to accept connections for web-based API
- port
- port to accept connections for web-based API [8111]
start cluster
master implementation (cluster.js)
var cluster = require('proteus-cluster');
var conf = {};
conf.worker = 4;
conf.pid = '/tmp/proteus.pid';
conf.exec = 'worker.js';
conf.args = ['-c', 'config.json'];
conf.disconnectTimeout = 5000;
conf.maxForkCount = 30;
conf.api = {
listen : '0.0.0.0',
port : 8111
};
cluster(conf);
worker implementation (worker.js)
var express = require('express');
var app = express();
var port = 8080;
app.get('/', function(req, res) {
res.send('running worker '+process.pid);
});
app.listen(port);
graceful restart
run from shell
$ node cluster.js
$ ps ax | grep node | grep -v 'grep'
2051 s000 S+ 0:00.09 node cluster.js
2052 s000 R+ 0:00.42 node /proteus-cluster/test/worker.js
2053 s000 R+ 0:00.42 node /proteus-cluster/test/worker.js
$ kill -SIGUSR2 2051
run from program
cluster.restart();
run by calling API
curl http://localhost:8111/restart
graceful shutdown
run from shell
$ node cluster.js
$ ps ax | grep node | grep -v 'grep'
2051 s000 S+ 0:00.09 node cluster.js
2052 s000 R+ 0:00.42 node /proteus-cluster/test/worker.js
2053 s000 R+ 0:00.42 node /proteus-cluster/test/worker.js
$ kill -SIGINT 2051
run from program
cluster.shutdown();
force shutdown
run from program
cluster.forceShutdown();
send messages from worker to master
worker implementation (worker.js)
process.send({cmd: 'fromWorker', msg: 'sending message to master'});
master implementation (cluster.js)
cluster.addMessageListener('fromWorker', function(msg) {
logger.debug(msg.msg); // sending message to master
});
// you can remove messageListener
cluster.removeMessageListener('fromWorker');
send messages from master to workers
master implementation (cluster.js)
cluster.sendMessage({cmd: 'fromMaster', msg: 'sending message to worker'});
worker implementation (worker.js)
process.on('message', function(msg) {
logger.debug(msg.msg); // sending message to worker
});
日本語
説明
Proteus Clusterは、Node.jsのclusterモジュールの利便性を高めたモジュールです。
Proteus Logger と連携し、node.js の cluster 環境におけるログ出力をサポートします。詳細は Proteus Logger をご参照下さい。
以下の特徴を持ちます。
- worker異常終了時の自動再起動
- workerのgraceful restart/shudown
- proteus-loggerと連携したmasterプロセスのログ一元管理
- masterとworker間のメッセージ受け渡し機構
利用方法
settings([]はデフォルト値)
- worker
- 起動するworker数 [サーバのCPU数]
- pid
- プロセスID
- exec
- workerの起動JSファイル [__filename]
- args
- workerの起動JSファイル呼び出し時に渡す引数
- disconnectTimeout
- workerを安全に停止するためのタイムアウト時間。時間を過ぎると強制停止される。 [10000]
- maxForkCount
- workerをforkする回数の上限値(永久に再起動を繰り返さないための対応) [100]
- api
- listen
- WebベースのAPIを起動する際のホスト名
- port
- WebベースのAPIを起動する際のポート番号 [8111]
cluster起動
masterの実装 (cluster.js)
var cluster = require('proteus-cluster');
var conf = {};
conf.worker = 4;
conf.pid = '/tmp/proteus.pid';
conf.exec = 'worker.js';
conf.args = ['-c', 'config.json'];
conf.disconnectTimeout = 5000;
conf.maxForkCount = 30;
conf.api = {
listen : '0.0.0.0',
port : 8111
};
cluster(conf);
workerの実装 (worker.js)
var express = require('express');
var app = express();
var port = 8080;
app.get('/', function(req, res) {
res.send('running worker '+process.pid);
});
app.listen(port);
graceful restart
シェルから実行
$ node cluster.js
$ ps ax | grep node | grep -v 'grep'
2051 s000 S+ 0:00.09 node cluster.js
2052 s000 R+ 0:00.42 node /proteus-cluster/test/worker.js
2053 s000 R+ 0:00.42 node /proteus-cluster/test/worker.js
$ kill -SIGUSR2 2051
プログラムから実行
cluster.restart();
APIを呼び出して実行
curl http://localhost:8111/restart
graceful shutdown
シェルから実行
$ node cluster.js
$ ps ax | grep node | grep -v 'grep'
2051 s000 S+ 0:00.09 node cluster.js
2052 s000 R+ 0:00.42 node /proteus-cluster/test/worker.js
2053 s000 R+ 0:00.42 node /proteus-cluster/test/worker.js
$ kill -SIGINT 2051
プログラムから実行
cluster.shutdown();
force shutdown
プログラムから実行
cluster.forceShutdown();
workerからmasterへのメッセージ送信
workerの実装 (worker.js)
process.send({cmd: 'fromWorker', msg: 'sending message to master'});
masterの実装 (cluster.js)
cluster.addMessageListener('fromWorker', function(msg) {
logger.info(msg.msg); // sending message to master
});
// messageListenerの削除も可能
cluster.removeMessageListener('fromWorker');
masterからworkerへのメッセージ送信
master implementation (cluster.js)
cluster.sendMessage({cmd: 'fromMaster', msg: 'sending message to worker'});
worker implementation (worker.js)
process.on('message', function(msg) {
logger.debug(msg.msg); // sending message to worker
});
License
Copyright 2012 CyberAgent, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.