Skip to content

Commit 4f56cfc

Browse files
Typescript, CI, start/stop, dev script, and new options
1 parent c9d6520 commit 4f56cfc

File tree

9 files changed

+2741
-818
lines changed

9 files changed

+2741
-818
lines changed

.eslintrc.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
{
2+
"root": true,
23
"env": {
34
"es6": true,
45
"node": true
56
},
6-
"extends": "airbnb-base",
7+
"extends": ["airbnb-base", "airbnb-typescript/base"],
8+
"parserOptions": {
9+
"project": ["tsconfig.json"]
10+
},
711
"rules": {
812
"object-curly-spacing": ["warn", "never"],
9-
"no-underscore-dangle": 0
13+
"no-underscore-dangle": 0,
14+
"class-methods-use-this": 0,
15+
"max-len": 0,
16+
"@typescript-eslint/object-curly-spacing": 0
1017
}
1118
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Democracy CI
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
pull_request:
7+
branches: [ main, master ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
node-version: [12.x, 14.x, 16.x]
17+
18+
steps:
19+
- uses: actions/checkout@v2
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v2
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
- run: npm install
25+
- run: npm run build --if-present
26+
- run: npm run lint

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ In-process monitoring of distributed Node.js instances over UDP unicast. Simply
99
The below example is easy to run on your local machine (also found in the examples directory). The IP's can just as easily be swapped out for IP's of other servers/instances.
1010

1111
```javascript
12-
var Democracy = require('democracy');
12+
var { default: Democracy } = require('democracy');
13+
// OR import Democracy from 'democracy';
1314

1415
// Basic usage of democracy to manager leader and citizen nodes.
1516
var dem = new Democracy({
@@ -58,6 +59,8 @@ new Democracy({
5859
weight: Math.random() * Date.now(), // The highest weight is used to determine the new leader. Must be unique for each node.
5960
id: 'uuid', // (optional) This is generated automatically with uuid, but can optionally be set. Must be unique for each node.
6061
channels: [], // (optional) Array of channels for this node to listen to (for pub/sub).
62+
enableStrictWeightMode: false, // (optional) Ensure the highest weighted node becomes leader, even if one is already established.
63+
autoStart: true, // (optional) Whether to start networking upon instantiating or manually at a later time
6164
});
6265
```
6366

@@ -76,10 +79,18 @@ Sends a custom event to all other nodes.
7679
Subscribe to a channel for use with pub/sub.
7780
#### publish(channel, data)
7881
Publish to a channel and send specific data with pub/sub.
82+
#### start()
83+
Open a socket and join or start a democracy cluster
84+
#### stop()
85+
Close the socket and leave the democracy cluster
7986

8087
### Events
8188
All events return the data/configuration of the affected node as their first parameter.
8289

90+
#### started
91+
Fired when a socket has been opened and the node has begun communications with the cluster
92+
#### stopped
93+
Fired when the node has ceased communications with the cluster and closed the socket.
8394
#### added
8495
Fired when a new peer has been found.
8596
#### removed

examples/test.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,57 @@
88
* node test.js 12345
99
* node test.js 12346
1010
* node test.js 12347
11+
*
12+
* Alternatively, you may use the "npm run dev" script to run 3 nodes concurrently
1113
*/
1214

13-
var Democracy = require('democracy');
15+
var { default: Democracy } = require('../')
1416

1517
var dem = new Democracy({
1618
source: '0.0.0.0:' + process.argv[2],
17-
peers: ['0.0.0.0:12345', '0.0.0.0:12346', '0.0.0.0:12347']
19+
peers: ['0.0.0.0:12345', '0.0.0.0:12346', '0.0.0.0:12347', '0.0.0.0:12348', '0.0.0.0:12349', '0.0.0.0:12350'],
20+
weight: Number(process.argv[2]),
21+
id: process.argv[2],
22+
enableStrictWeightMode: true,
23+
autoStart: false,
1824
});
1925

2026
dem.on('added', function(data) {
21-
console.log('Added: ', data);
27+
console.log('Added: ', data.id);
2228
});
2329

2430
dem.on('removed', function(data) {
25-
console.log('Removed: ', data);
31+
console.log('Removed: ', { id: data.id, voters: data.voters });
2632
});
2733

2834
dem.on('elected', function(data) {
2935
console.log('You are elected leader!');
3036
});
3137

3238
dem.on('leader', function(data) {
33-
console.log('New Leader: ', data);
39+
console.log('New Leader: ', data.id);
40+
});
41+
42+
dem.on('started', function() {
43+
console.log('Started!');
44+
setTimeout(() => dem.stop(), Math.random() * 90 * 1000)
3445
});
46+
47+
dem.on('stopped', function() {
48+
console.log('Stopped!!');
49+
setTimeout(() => dem.start(), Math.random() * 30 * 1000)
50+
});
51+
52+
// (Duplicate calls and rapid toggling do not cause issues)
53+
dem.start();
54+
dem.stop();
55+
dem.stop();
56+
dem.start();
57+
dem.start();
58+
59+
setInterval(() => {
60+
console.log({
61+
leader: dem.leader() && dem.leader().id,
62+
nodes: Object.values(dem.nodes()).map(a => a.id),
63+
});
64+
}, 5000);

0 commit comments

Comments
 (0)