Browse Source

add forks on 'fork' event

master
AJ ONeal 8 years ago
parent
commit
7819398294
  1. 14
      README.md
  2. 5
      master.js
  3. 2
      test.js

14
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'
});

5
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;
};

2
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();
}

Loading…
Cancel
Save