diff --git a/README.md b/README.md index d5dcbb4..b6b7a11 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,8 @@ you're trying to use (for example express-session/session/memory, sqlite3, level and then you will supply the names of the methods you wish to export to **worker** processes. -You must pass each worker via `addWorker()` so that it signals the worker to creates -its own rpc-wrapped instance. +By default each worker will be added when `cluster` emits a `fork` event. +If needed you can set `addOnFork` to `false` and call `addWorker(worker)` manually. ### master @@ -32,14 +32,15 @@ var db = require('level')('./mydb') // Wrap the instance var crpc = require('cluster-rpc/master').create({ - instance: db + addOnFork: true // default +, instance: db , methods: [ 'get', 'put' ] , name: 'foo-level' }); -// You must add each worker -crpc.addWorker(cluster.fork()); +// If you set addOnFork to false, You must manually add each worker +// crpc.addWorker(cluster.fork()); crpc.then(function (db) { @@ -77,7 +78,8 @@ if (cluster.isMaster) { crpc = require('cluster-rpc/master').create({ - instance: require('level')('./mydb') + addOnFork: false + , instance: require('level')('./mydb') , methods: [ 'get', 'put' ] , name: 'foo-level' }); diff --git a/master.js b/master.js index 896dd07..9e89799 100644 --- a/master.js +++ b/master.js @@ -89,6 +89,7 @@ function setup(opts) { } module.exports.create = function (opts) { + var cluster = require('cluster'); var PromiseA = opts.PromiseA || global.Promise || require('bluebird'); var init = false; @@ -101,5 +102,9 @@ module.exports.create = function (opts) { return opts.master.addWorker(w); }; + if (false !== opts.addOnFork) { + cluster.on('fork', opts._promise.addWorker); + } + return opts._promise; }; diff --git a/test.js b/test.js index a7f5bd2..475a09d 100644 --- a/test.js +++ b/test.js @@ -21,10 +21,10 @@ if (cluster.isMaster) { , methods: [ 'get', 'put' ] , name: 'foo-level' }); - crpc.addWorker(cluster.fork()); crpc.then(function () { db.put('foo', 'bar'); }); + cluster.fork(); }