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

pm2-messages

v4.0.2

Published

Package to request messages from processes managed by pm2.

Downloads

4,531

Readme

Library to retrieve messages within a PM2 cluster

npm npm

This package provides a communication interface to retrieve messages of processes managed by pm2. It can be used to aggregate Prometheus metrics such that the aggregated registry represents cluster metrics.

Installation

Install the package with:

npm install pm2-messages --save

or

yarn add pm2-messages

Quick start

  1. Attach a message handler
const pm2mes = require('pm2-messages');
const prom = require('prom-client');

pm2mes.onMessage('get_prom_register', () => prom.register.getMetricsAsJSON());
  1. Collect all messages
await pm2mes.connect();

const metricsArr = await pm2mes.getMessages('get_prom_register');

await pm2mes.disconnect();
  1. Aggregate the metrics
const metrics = prom.AggregatorRegistry.aggregate(metricsArr).metrics();

API

Connection

Use the connect and disconnect methods to connect and disconnect from pm2, respectively. Use isConnected to determine whether there is a connection with pm2.

  • You must call connect() before using getMessages.
  • You must call disconnect() after having called connect() when you would like the process to gracefully exit.

The following example demonstrates how to properly use connect and disconnect once, instead of calling them around every getMessages call.

Note that the latter will cause issues when concurrent requests are happening, i.e. when two processes in a cluster are using getMessages at the same time. Calling disconnect() in one process will wrongly affect the connections by other processes, resulting in an error when trying to use the connection (see issue #5).

const pm2mes = require('pm2-messages');

const gracefulShutdown = async function gracefulShutdown() {
  try {
    if (pm2mes.isConnected()) await pm2mes.disconnect();

    process.exit();
  } catch (err) {
    console.error(err);

    process.exit(1);
  }
};

process.on('SIGINT', gracefulShutdown);
process.on('SIGTERM', gracefulShutdown);

(async () => {
  await pm2mes.connect();

  // Your code here, e.g. init server
})().catch(async (err) => {
  console.error(err);

  await gracefulShutdown();
});

Message handlers

Message handlers return the requested message for a specific topic. Handlers may be asynchronous, i.e. return a promise. Requests may carry additional data, available as first argument.

pm2mes.onMessage('get_something', async (data) => {
  if (data.val === myVal) return doSomethingAsync();
});

Collecting messages

Messages can be collected using the getMessages function. The first argument is required and denotes the topic, the second argument is optional and contains additional request data, and the third argument is optional and contains configuration options.

const somethingArr = await pm2mes.getMessages(
  'get_something',
  { val: 'value' },
  { filter: (process) => process.name === 'my process', timeout: 100 }
);

Configuration

  • filter: Filter function to select the processes managed by pm2 from which messages need to be requested. Defaults to processes with same name as the active process, i.e. (process): boolean => process.name === process.env.name.
  • includeSelfIfUnmanaged: Indicates whether messages need to be requested from the active process when it is not managed by pm2 (see issue #4). Defaults to false.
  • timeout: Timeout in milliseconds (ms). Default to 1000 ms.