Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #133 from fengmk2/issue132-config
Browse files Browse the repository at this point in the history
support custom config as a module, fixed issue #132
  • Loading branch information
fengmk2 committed Jan 8, 2014
2 parents 6371d60 + 3ca6248 commit bbafa02
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 30 deletions.
11 changes: 10 additions & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var config = {
user: 'address@gmail.com',
pass: 'your password',
ssl: true,
debug: false
debug: false
},

sourceNpmRegistry: 'http://registry.npmjs.org',
Expand Down Expand Up @@ -98,3 +98,12 @@ mkdirp.sync(config.logdir);
mkdirp.sync(config.uploadDir);

module.exports = config;

config.loadConfig = function (customConfig) {
if (!customConfig) {
return;
}
for (var key in customConfig) {
config[key] = customConfig[key];
}
};
68 changes: 39 additions & 29 deletions dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,46 @@ var workerPath = path.join(__dirname, 'worker.js');
var childProcess = require('child_process');
var syncPath = path.join(__dirname, 'sync');

if (config.enableCluster) {
cluster.setupMaster({
exec: workerPath
});

cluster.on('fork', function (worker) {
console.log('[%s] [worker:%d] new worker start', new Date(), worker.process.pid);
});

cluster.on('disconnect', function (worker) {
var w = cluster.fork();
console.error('[%s] [master:%s] wroker:%s disconnect! new worker:%s fork',
new Date(), process.pid, worker.process.pid, w.process.pid);
});

cluster.on('exit', function (worker, code, signal) {
var exitCode = worker.process.exitCode;
var err = new Error(util.format('worker %s died (code: %s, signal: %s)', worker.process.pid, exitCode, signal));
err.name = 'WorkerDiedError';
console.error(err);
});

var numCPUs = require('os').cpus().length;
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
function start(customConfig) {
config.loadConfig(customConfig);

if (config.enableCluster) {
cluster.setupMaster({
exec: workerPath
});

cluster.on('fork', function (worker) {
console.log('[%s] [worker:%d] new worker start', new Date(), worker.process.pid);
});

cluster.on('disconnect', function (worker) {
var w = cluster.fork();
console.error('[%s] [master:%s] wroker:%s disconnect! new worker:%s fork',
new Date(), process.pid, worker.process.pid, w.process.pid);
});

cluster.on('exit', function (worker, code, signal) {
var exitCode = worker.process.exitCode;
var err = new Error(util.format('worker %s died (code: %s, signal: %s)', worker.process.pid, exitCode, signal));
err.name = 'WorkerDiedError';
console.error(err);
});

var numCPUs = require('os').cpus().length;
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}

childProcess.fork(syncPath);
} else {
require(workerPath);
require(syncPath);
}
}

childProcess.fork(syncPath);
if (!module.parent) {
start();
} else {
require(workerPath);
require(syncPath);
module.exports = start;
}

0 comments on commit bbafa02

Please sign in to comment.