node package manager
Painless code sharing. npm Orgs help your team discover, share, and reuse code. Create a free org »

sd-notify

sd-notify

NPM version License Code style

Extremely minimal wrapper around sd_notify

Installation

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

$ sudo apt install libsystemd-dev

...then using npm or yarn:

$ npm install --save sd-notify

or:

$ yarn add sd-notify

Please note that this has currently only been tested on Ubuntu 16.04.

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 quarantee 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.