Gathering detailed insights and metrics for leader-election-mongo
Gathering detailed insights and metrics for leader-election-mongo
Gathering detailed insights and metrics for leader-election-mongo
Gathering detailed insights and metrics for leader-election-mongo
A leader election package which uses MongoDB
npm install leader-election-mongo
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
1 Commits
3 Watchers
1 Branches
4 Contributors
Updated on Sep 04, 2020
Latest Version
0.0.2
Package Id
leader-election-mongo@0.0.2
Unpacked Size
20.39 kB
Size
6.85 kB
File Count
5
NPM Version
6.11.3
Node Version
10.17.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
A leader election package which uses MongoDB. A single leader is chosen from a group of instances in a single, non-recurring election.
This package is useful when doing a single election for tasks such as a once-daily cron job (as opposed to having a group of instances continuously trying to determine a leader). It is intended for a relatively small number of instances, such as 5 or 10, not 10,000.
1npm install leader-election-mongo
Here is an example using promises:
1const crypto = require('crypto') 2const { Leader } = require('leader-election-mongo') 3const { MongoClient } = require('mongodb') 4 5const url = 'mongodb://localhost:27017' 6 7MongoClient.connect( 8 url, 9 { 10 useNewUrlParser: true, 11 useUnifiedTopology: true 12 }, 13 function (err, client) { 14 const id = process.pid + '-' + crypto.randomBytes(8).toString('hex') 15 const db = client.db('leadertest') 16 const candidate = new Leader(db, { id, ttl: 10000 }) 17 18 candidate 19 .initDatabase() 20 .then(collectionExists => { 21 if (collectionExists) { 22 return candidate.elect() 23 } 24 console.log(`${id} collection was not initialized!`) 25 process.exit() 26 }) 27 .then(isLeader => { 28 if (isLeader) { 29 console.log(`${id} is the LEADER`) 30 // do your stuff here 31 return candidate.cleanup() 32 } else { 33 console.log(`${id} is not the leader`) 34 process.exit() 35 } 36 }) 37 .then(() => { 38 console.log(`${id} Cleanup finished`) 39 process.exit() 40 }) 41 } 42)
This package can also be used with async/await:
1const crypto = require('crypto') 2const { Leader } = require('leader-election-mongo') 3const { MongoClient } = require('mongodb') 4 5const url = 'mongodb://localhost:27017' 6 7MongoClient.connect( 8 url, 9 { 10 useNewUrlParser: true, 11 useUnifiedTopology: true 12 }, 13 async function (err, client) { 14 const id = process.pid + '-' + crypto.randomBytes(8).toString('hex') 15 const db = client.db('leadertest') 16 const candidate = new Leader(db, { id }) 17 18 const collectionExists = await candidate.initDatabase() 19 if (!collectionExists) { 20 console.log(`${id} collection was not initialized!`) 21 process.exit() 22 } 23 24 const isLeader = await candidate.elect() 25 if (!isLeader) { 26 console.log(`${id} is not the leader`) 27 process.exit() 28 } 29 30 console.log(`${id} is the LEADER`) 31 // do your stuff here 32 33 await candidate.cleanup() 34 console.log(`${id} Cleanup finished`) 35 process.exit() 36 } 37)
Create a new Leader class.
db
is a MongoClient.Db object.
options.id
is an optional ID for this instance. Default is a random hex string.
options.ttl
is the lock time-to-live in milliseconds. Will be automatically
released after that time. The default and minimum value is 5000.
If the provided MongoClient.Db object doesn't have admin privileges, the TTL
cleanup will only happen every 60 seconds (the MongoDB default) which might
cause a problem if the leader doesn't call cleanup()
and you try to run
multiple elections within 60 seconds.
options.key
is a unique identifier for the group of instances trying to be
elected as leader. Default value is 'default'
Initializes the election collection. Returns a promise that resolves to a boolean indicating whether the collection exists.
Performs the election and returns a promise that resolves to true
if the
instance is the leader; otherwise, false
.
Removes the election collection. Returns a promise that resolves to void.
elected
The event fired when the instance becomes a leader.
cleaned
The event fired when the cleanup is finished.
See the LICENSE file (spoiler alert, it's Apache-2.0).
Inspired by the mongo-leader package, but with a modified API and different election algorithm.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/1 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
17 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More