Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript, CI, start/stop methods, dev script, and new options #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
{
"root": true,
"env": {
"es6": true,
"node": true
},
"extends": "airbnb-base",
"extends": ["airbnb-base", "airbnb-typescript/base"],
"parserOptions": {
"project": ["tsconfig.json"]
},
"rules": {
"object-curly-spacing": ["warn", "never"],
"no-underscore-dangle": 0
"no-underscore-dangle": 0,
"class-methods-use-this": 0,
"max-len": 0,
"@typescript-eslint/object-curly-spacing": 0
}
}
26 changes: 26 additions & 0 deletions .github/workflows/linting_and_typechecks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Democracy CI

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run build --if-present
- run: npm run lint
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ In-process monitoring of distributed Node.js instances over UDP unicast. Simply
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.

```javascript
var Democracy = require('democracy');
var { default: Democracy } = require('democracy');
// OR import Democracy from 'democracy';

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

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

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

#### started
Fired when a socket has been opened and the node has begun communications with the cluster
#### stopped
Fired when the node has ceased communications with the cluster and closed the socket.
#### added
Fired when a new peer has been found.
#### removed
Expand Down
40 changes: 35 additions & 5 deletions examples/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,57 @@
* node test.js 12345
* node test.js 12346
* node test.js 12347
*
* Alternatively, you may use the "npm run dev" script to run 3 nodes concurrently
*/

var Democracy = require('democracy');
var { default: Democracy } = require('../')

var dem = new Democracy({
source: '0.0.0.0:' + process.argv[2],
peers: ['0.0.0.0:12345', '0.0.0.0:12346', '0.0.0.0:12347']
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'],
weight: Number(process.argv[2]),
id: process.argv[2],
enableStrictWeightMode: true,
autoStart: false,
});

dem.on('added', function(data) {
console.log('Added: ', data);
console.log('Added: ', data.id);
});

dem.on('removed', function(data) {
console.log('Removed: ', data);
console.log('Removed: ', { id: data.id, voters: data.voters });
});

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

dem.on('leader', function(data) {
console.log('New Leader: ', data);
console.log('New Leader: ', data.id);
});

dem.on('started', function() {
console.log('Started!');
setTimeout(() => dem.stop(), Math.random() * 90 * 1000)
});

dem.on('stopped', function() {
console.log('Stopped!!');
setTimeout(() => dem.start(), Math.random() * 30 * 1000)
});

// (Duplicate calls and rapid toggling do not cause issues)
dem.start();
dem.stop();
dem.stop();
dem.start();
dem.start();

setInterval(() => {
console.log({
leader: dem.leader() && dem.leader().id,
nodes: Object.values(dem.nodes()).map(a => a.id),
});
}, 5000);
Loading