mongo-leader

    1.0.104 • Public • Published

    mongo-leader

    Build Status Dependencies Status Codacy Badge Maintainability NPM NPM downloads js-standard-style

    Leader election backed by MongoDB, inspired by the redis-leader. Class Leader extends the Node.js EventEmmiter class.

    Install

    npm install mongo-leader

    Example

    const { Leader } = require("mongo-leader");
    const { MongoClient } = require("mongodb");
    
    const url = "mongodb://localhost:27017";
    
    MongoClient.connect(url, { useNewUrlParser: true }, function (err, client) {
      const db = client.db("test");
      const leader = new Leader(db, { ttl: 5000, wait: 1000 });
      setInterval(() => {
        leader.isLeader().then((leader) => console.log(`Am I leader? : ${leader}`));
      }, 100);
    });

    API

    new Leader(db, options)

    Create a new Leader class

    db is a MongoClient object

    options.ttl Lock time to live in milliseconds.
    Will be automatically released after that time.
    Default and minimum values are 1000.

    options.wait Time between tries getting elected in milliseconds.
    Default and minimum values are 100.

    options.key Unique identifier for the group of instances trying to be elected as leader.
    Default value is 'default'

    isLeader()

    The function determines whether the instance is a leader.

    Returns promise that resolved to true if the instance is a leader; otherwise, false.

    Events

    elected The event fired when the instance become a leader.

    revoked The event fired when the instance revoked from it's leadership.

    License

    This project is licensed under the MIT License.

    Keywords

    Install

    npm i mongo-leader

    DownloadsWeekly Downloads

    233

    Version

    1.0.104

    License

    MIT

    Unpacked Size

    9.39 kB

    Total Files

    9

    Last publish

    Collaborators

    • andrewmolyuk