Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f81b091f97 | ||
|
|
fc5859ddc8 | ||
|
|
e3aa7e90ad | ||
|
|
44ff858a0c | ||
|
|
7342ee2bb0 | ||
|
|
7a46803159 | ||
|
|
34b0aaf460 | ||
|
|
14c4788e8c | ||
|
|
7d9730c1c8 | ||
|
|
6711c73aa7 | ||
|
|
2850be580a |
97
README.md
97
README.md
@ -14,29 +14,48 @@ Install
|
|||||||
=======
|
=======
|
||||||
|
|
||||||
```
|
```
|
||||||
npm install --save memstore-cluster@2.x
|
npm install --save cluster-store@2.x
|
||||||
```
|
```
|
||||||
|
|
||||||
v1.x vs v2.x
|
v1.x vs v2.x
|
||||||
------------
|
------------
|
||||||
|
|
||||||
The old v1 used `ws` which makes it usable when clustering node processes without using `cluster`.
|
The [old v1](https://github.com/coolaj86/cluster-store/tree/v1.x)
|
||||||
|
used `ws` which makes it usable when clustering node processes without using `cluster`.
|
||||||
|
|
||||||
If you need that functionaliy, use it.
|
If you need that functionaliy, use v1 instead of v2.
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
=====
|
=====
|
||||||
|
|
||||||
|
In its simplest form, you use this module nearly exactly the way you would
|
||||||
|
the any other storage module, with the exception that you must wait for
|
||||||
|
the inter-process initialization to complete.
|
||||||
|
|
||||||
|
When not using any of the options the usage is the same for the master and the worker:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
require('cluster-store').create().then(function (store) {
|
||||||
|
// initialization is now complete
|
||||||
|
store.set('foo', 'bar');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
### standalone (non-cluster)
|
### standalone (non-cluster)
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
The usage is exactly the same as **master**, no changes necessary.
|
There is no disadvantage to using this module standalone.
|
||||||
|
The additional overhead of inter-process communication is only added when
|
||||||
|
a worker is added.
|
||||||
|
|
||||||
|
As such, the standalone usage is identical to usage in master process, as seen below.
|
||||||
|
|
||||||
### master
|
### master
|
||||||
|
|
||||||
In the **master**/**standalone** process you will create the real store instance
|
In the **master** process you will create the real store instance.
|
||||||
and then in the you must pass each worker via `addWorker()` so that it signals
|
|
||||||
the worker to create its own rpc-wrapped instance.
|
If you need to manually specify which worker will be enabled for this funcitonality
|
||||||
|
you must set `addOnFork` to `false` and call `addWorker()` manually.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
'use strict';
|
'use strict';
|
||||||
@ -44,16 +63,25 @@ the worker to create its own rpc-wrapped instance.
|
|||||||
var cluster = require('cluster');
|
var cluster = require('cluster');
|
||||||
|
|
||||||
var cstore = require('cluster-store/master').create({
|
var cstore = require('cluster-store/master').create({
|
||||||
name: 'foo-store'
|
name: 'foo-store' // necessary when using multiple instances
|
||||||
|
, store: null // use default in-memory store
|
||||||
|
, addOnFork: true // default
|
||||||
});
|
});
|
||||||
|
|
||||||
cstore.addWorker(cluster.fork());
|
// if you addOnFork is set to false you can add specific forks manually
|
||||||
|
//cstore.addWorker(cluster.fork());
|
||||||
|
|
||||||
cstore.then(function (store) {
|
cstore.then(function (store) {
|
||||||
store.set('foo', 'bar');
|
store.set('foo', 'bar');
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Note: `store` can be replaced with any `express/session`-compatible store, such as:
|
||||||
|
|
||||||
|
* `new require('express-session/session/memory')()`
|
||||||
|
* `require('level-session-store')(session)`
|
||||||
|
* and others
|
||||||
|
|
||||||
### worker
|
### worker
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
@ -103,46 +131,21 @@ Example
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var cluster = require('cluster');
|
var cluster = require('cluster');
|
||||||
var cstore;
|
|
||||||
|
|
||||||
|
require('cluster-store').create({
|
||||||
|
name: 'foo-store'
|
||||||
|
}).then(function (store) {
|
||||||
|
if (cluster.isMaster) {
|
||||||
|
store.set('foo', 'bar');
|
||||||
|
}
|
||||||
|
|
||||||
|
store.get('foo', function (err, result) {
|
||||||
|
console.log(result);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
if (cluster.isMaster) {
|
if (cluster.isMaster) {
|
||||||
|
cluster.fork();
|
||||||
|
cluster.fork();
|
||||||
cstore = require('./master').create({
|
|
||||||
name: 'foo-level'
|
|
||||||
});
|
|
||||||
cstore.addWorker(cluster.fork());
|
|
||||||
cstore.then(function (store) {
|
|
||||||
store.put('foo', 'bar');
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
|
|
||||||
|
|
||||||
cstore = require('./worker').create({
|
|
||||||
name: 'foo-level'
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
cstore.then(function (store) {
|
|
||||||
setTimeout(function () {
|
|
||||||
store.get('foo', function (err, result) {
|
|
||||||
console.log(cluster.isMaster && '0' || cluster.worker.id.toString(), "store.get('foo')", result);
|
|
||||||
|
|
||||||
if (!cluster.isMaster) {
|
|
||||||
process.exit(0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, 250);
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('unhandledRejection', function (err) {
|
|
||||||
console.log('unhandledRejection', err);
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|||||||
15
index.js
15
index.js
@ -1,12 +1,9 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
console.error("");
|
var cluster = require('cluster');
|
||||||
console.error("One does not simply require('memstore-cluster');");
|
|
||||||
console.error("");
|
|
||||||
console.error("Usage:");
|
|
||||||
console.error("\trequire('memstore-cluster/master').create({ name: ... });");
|
|
||||||
console.error("\trequire('memstore-cluster/worker').create({ name: ... });");
|
|
||||||
console.error("");
|
|
||||||
console.error("");
|
|
||||||
|
|
||||||
process.exit(1);
|
if (cluster.isMaster) {
|
||||||
|
module.exports = require('./master');
|
||||||
|
} else {
|
||||||
|
module.exports = require('./worker');
|
||||||
|
}
|
||||||
|
|||||||
@ -14,5 +14,6 @@ module.exports.create = function (opts) {
|
|||||||
]
|
]
|
||||||
, name: 'memstore.' + (opts.name || '')
|
, name: 'memstore.' + (opts.name || '')
|
||||||
, master: opts.master
|
, master: opts.master
|
||||||
|
, addOnFork: opts.addOnFork
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
18
package.json
18
package.json
@ -1,7 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "cluster-store",
|
"name": "cluster-store",
|
||||||
"version": "2.0.0",
|
"version": "2.0.8",
|
||||||
"description": "A wrapper to enable the use of any in-process store with node cluster via cluster process and worker messages (i.e. for Raspberry Pi servers).",
|
"description": "A wrapper to enable the use of any in-process store with node cluster via cluster process and worker messages (i.e. for Raspberry Pi servers).",
|
||||||
|
"homepage": "https://git.coolaj86.com/coolaj86/cluster-store.js",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node test-cluster.js",
|
"test": "node test-cluster.js",
|
||||||
@ -9,8 +10,10 @@
|
|||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/coolaj86/cluster-store.git"
|
"url": "git+https://git.coolaj86.com/coolaj86/cluster-store.js.git"
|
||||||
},
|
},
|
||||||
|
"bundleDependencies": false,
|
||||||
|
"deprecated": false,
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"store",
|
"store",
|
||||||
"session",
|
"session",
|
||||||
@ -20,13 +23,16 @@
|
|||||||
"cluster",
|
"cluster",
|
||||||
"rpi2"
|
"rpi2"
|
||||||
],
|
],
|
||||||
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.com/)",
|
"author": {
|
||||||
|
"name": "AJ ONeal",
|
||||||
|
"email": "coolaj86@gmail.com",
|
||||||
|
"url": "https://coolaj86.com/"
|
||||||
|
},
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/coolaj86/cluster-store/issues"
|
"url": "https://git.coolaj86.com/coolaj86/cluster-store.js/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/coolaj86/cluster-store#readme",
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cluster-rpc": "^1.0.3"
|
"cluster-rpc": "^v1.0.6"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
26
simplest.js
Normal file
26
simplest.js
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var cluster = require('cluster');
|
||||||
|
var cstore;
|
||||||
|
|
||||||
|
require('./').create({
|
||||||
|
name: 'foo-store'
|
||||||
|
}).then(function (store) {
|
||||||
|
if (cluster.isMaster) {
|
||||||
|
cluster.fork();
|
||||||
|
cluster.fork();
|
||||||
|
|
||||||
|
store.set('foo', 'bar');
|
||||||
|
}
|
||||||
|
|
||||||
|
store.get('foo', function (err, result) {
|
||||||
|
console.log(cluster.isMaster && '0' || cluster.worker.id.toString(), 'foo', result);
|
||||||
|
if (!cluster.isMaster) {
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on('unhandledRejection', function (err) {
|
||||||
|
console.log('unhandledRejection', err);
|
||||||
|
});
|
||||||
4
test.js
4
test.js
@ -11,11 +11,13 @@ if (cluster.isMaster) {
|
|||||||
cstore = require('./master').create({
|
cstore = require('./master').create({
|
||||||
name: 'foo-level'
|
name: 'foo-level'
|
||||||
});
|
});
|
||||||
cstore.addWorker(cluster.fork());
|
|
||||||
cstore.then(function (db) {
|
cstore.then(function (db) {
|
||||||
db.set('foo', 'bar');
|
db.set('foo', 'bar');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
cluster.fork();
|
||||||
|
cluster.fork();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user