npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

is-master

v1.3.0

Published

Find the master node process in a multi server cluster.

Downloads

436

Readme

is-master

Find the master node process in a multi server cluster.

This module finds the master node in a cluster by inserting the nodes in a mongodb and choosing the master by which node is the oldest. Each node checks into mongodb on a set timeout (default 1 minute). If the master node dies for whatever reason, mongodb will expire the record and the next node in line will become the master. Mongoose and a connection to a mongodb database is REQUIRED for is-master to work.

Use cases for this module:

  • If you run your node cluster with a cluster manager like PM2 or even if you run your clusters on multiple servers (they just need to all report into the same mongodb), you can find which node process is the master.
  • This will allow you to assign one node process as the master so that it can run tasks that should only be ran by one process, such as scheduled tasks and database cleanup.

Installation

npm install is-master

Usage / Examples

'use strict';

var mongoose = require('../node_modules/mongoose');
var im = require('../is-master.js');

// Start the mongoose db connection
mongoose.connect('mongodb://127.0.0.1:27017/im', function(err) {
    if (err) {
        console.error('\x1b[31m', 'Could not connect to MongoDB!');
        throw (err);
    }
});

// Start the is-master worker
im.start();

// Check if this current process is the master using the callback method
setInterval(function() {
    im.isMaster(function(err, results) {
        if (err) return console.error(err);
        console.log('Callback master: ', results);
    });
}, 5000);

// Check if this current process is the master using the im.master method, this method only updates every time the process checks in
setInterval(function() {
        console.log('Variable master: ', im.master);
}, 5000);

// Event emmiters that you can listen for
im.on('connected', function() {
    console.log('The is-master worker has connected and insterted into mongodb.');
});

im.on('synced', function() {
    console.log('The is-master worker has synced to mongodb.');
});

im.on('changed', function() {
    console.log('The master variable has changed');
});

im.on('master', function() {
    console.log('The process has been promoted to master');
});

im.on('secondary', function(){
    console.log('The process has been demoted to secondary');
});

Options

When starting the worker, you can specify options in an object to update the default values.

im.start({
    timeout: 120, // How often the nodes check into the database. This value is in seconds, default 60.
    hostname: 'devServer1', // Sets the hostname of the node, without this value it will get the hostname using os.hostname.
    collection: 'proc' // The mongodb collection is-master will use. Please note that by default mongoose adds an 's' to the end to make it plural. Default value is 'node'.
});

FAQ

Q. I updated the timeout option, but mongodb is not expiring the node in that timeout specified.

A. 60 seconds is added to the mongodb expire timeout to ensure the master has time to checkin. Also please note, if this value is changed from the initial creation of the table, it will not be able to update the index. You will need to delete the table and then restart your server to re-create it.

Compatibility

For backward compatibility, the secondary event is also emitted with the historical name slave. This maybe removed in a future release.

More info

http://mattpker.com/2015/08/07/How-to-schedule-jobs-in-NodeJS/

Tests

npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Release History

  • 1.3.0 Deprecating slave terminolgy for secondary #18, mongoose security update #19, and fix for mongoose deprecation warnings #17
  • 1.2.2 Fix #11, issue with inserts/upserts duplicate key errors
  • 1.2.1 Added blog posting to README.md for more information
  • 1.2.0 Added EventEmitter's for more functionality
  • 1.1.4 Better performance and resliancy in high-concurrency settings (#5, Thanks to @bendalton)
  • 1.1.3 Fixed critical infinite loop when there are duplicate start dates, resulting in elevated CPU and rapid log growth. All users are urged to upgrade. (#4, Thanks to @markstos)
  • 1.1.2 Removed unnecessary dev dependencies
  • 1.1.1 Fixed the tests to mock mongoose
  • 1.1.0 Added option for changing the collection
  • 1.0.0 Initial release