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

sd-notify

v2.8.0

Published

wrapper around sd_notify for using systemd as a node process manager

Downloads

24,666

Readme

sd-notify Build Status js-standard-style NPM version License

Extremely minimal wrapper around sd_notify

Requirements

  • any Linux distribution that supports systemd
  • C/C++ tool stack (GCC/Clang, etc...)
  • Node.js >= 8.0.0

Installation

Firstly you need some systemd development files, on Ubuntu these can be installed via:

$ sudo apt install libsystemd-dev

...then using npm:

$ npm install --save sd-notify

Usage

Example:

const notify = require('sd-notify')

// call notify after some async start up process
// such as in the `http` or `express` listen callback

app.listen(PORT, () => {
  console.log('listening on port ' + PORT)
  notify.ready()
})

Calling .ready() will inform systemd that the process has started, when using notify type in a service definition file, eg:

[Unit]
Description=Simple notifying service

[Service]
Environment="NODE_ENV=production"
Type=notify
ExecStart=/usr/sbin/simple-notifying-service
TimeoutStartSec=30
Restart=always

[Install]
WantedBy=multi-user.target

"Watchdog" mode:

In the service file add WatchdogSec=n where n is the amount of seconds systemd should stop (or restart) the service if there is no contact.

[Service]
Environment="NODE_ENV=production"
Type=notify
ExecStart=/usr/sbin/simple-notifying-service
TimeoutStartSec=30
Restart=always
WatchdogSec=3

...and in Node, you can call the native method .watchdog() directly in a setInterval or any other mechanism depending on what kind of application you are developing, or you can use the helper function startWatchdogMode(milliseconds):

const notify = require('sd-notify')

app.listen(PORT, () => {
  console.log('listening on port ' + PORT)
  notify.ready()
  notify.startWatchdogMode(2800)
})

...above the number supplied to the startWatchdogMode method is the amount of milliseconds we want to ping systemd, in the example this is 200ms less than the 3 seconds set in the service file. Due to the event loop there is no guarantee the setInterval underneath will fire exactly 2800ms, this will change depending on how many functions are being called in the process, though this has a nice side effect, as if the process gets that busy, that blocked, systemd will kill it (and restart it with the Restart= config set); and in the context of having multiple processes being load balanced with Nginx (as an example) and across multiple machines, ensures that no one process is blocking for any significant amount of time.

You can also check if the process was called by systemd with Watchdog mode enabled, using watchdogInterval() which returns the amount of milliseconds watchdog has been set to, or 0 if it has not been set:

app.listen(PORT, () => {
  console.log('listening on port ' + PORT)
  notify.ready()

  const watchdogInterval = notify.watchdogInterval()

  if (watchdogInterval > 0) {
    const interval = Math.floor(watchdogInterval / 2)
    notify.startWatchdogMode(interval)
  }
})

...this way the Node process will behave in the correct manner in either situation.

Status:

You can also send some status string to systemd, which will append to the service's log.

const notify = require('sd-notify')

// ...

notify.sendStatus('send some status to systemd')

// ...

...then, for example:

$ journalctl -u node-status

...
Apr 22 17:29:41 lenovo node[8275]: (8275) listening on 8000
Apr 22 17:29:41 lenovo systemd[1]: Started Express Node.js.
Apr 22 17:35:50 lenovo node[8275]: send some status to systemd
...