Package detail

hapi-pulse

futurestudio42.8kMIT3.0.1

hapi plugin that gracefully stops the hapi server on SIGINT

developer, hapi, hapi graceful, hapi pm2

readme

hapi-pulse logo

hapi plugin to gracefully stop your hapi server


Installation · Usage · Plugin Options



Build Status Known Vulnerabilities Latest Version Total downloads

Follow @marcuspoehls for updates!


The Future Studio University supports development of this hapi plugin 🚀
Join the Future Studio University and Skyrocket in Node.js


Introduction

A hapi plugin that gracefully stops the hapi server on SIGINT and SIGTERM and your custom signals.

hapi-pulse works great with PM2 and other Node.js process manager to accomplish zero-downtime deployments!

This serves existing requests before closing the connection and stopping the hapi server process. It uses hapi’s server.stop() method to close connections properly.

Requirements

hapi v19 (or later) and Node.js v12 (or newer)

This plugin requires hapi v19 (or later) and Node.js v12 or newer.

Compatibility

Major Release hapi.js version Node.js version
v3 >=17 hapi >=12
v2 >=17 hapi >=8

Installation

Add hapi-pulse as a dependency to your project:

npm i hapi-pulse

Usage

The most straight forward way to register the hapi-pulse plugin:

await server.register({
  plugin: require('hapi-pulse'),
  options: {
    // any option that is supported by hapi's "server.stop()"
    timeout: 15000,

    // plugin specific options
    logger: console,
    signals: ['SIGINT'],
    postServerStop: async function () {
      // await Database.close()
    }
  }
})

Plugin Registration Options

hapi-pulse passes the options through to hapi’s server.stop(options). Customize the behavior of server.stop(), like the timeout before forcefully stopping the process.

Additionally, you can pass along the following options:

  • logger: (Object), default: console — in case of an error, hapi-pulse logs the error with logger.error('message', error)
  • signals: (Array), default: ['SIGINT', 'SIGTERM'] — use this signals option to customize the events on which hapi-pulse will stop the server
  • preServerStop: (Function), default: Promise.resolve — an async function that runs before server.stop()
  • postServerStop: (Function), default: Promise.resolve — an async function that runs after server.stop()
  • preShutdown: (Function), default: Promise.resolve — an async function that runs after postServerStop() and before process.exit
  • timeout: (int), default: 5000 (5 seconds) — the timeout existing connections should be closed until they are forcefully interrupted. This option is passed through to hapi’s server.stop()

Example

await server.register({
  plugin: require('hapi-pulse'),
  options: {
    timeout: 25 * 1000,
    logger: console,
    signals: ['SIGINT', 'SIGTERM'],
    preServerStop: async function () {
      // this runs before server.stop()
    },
    postServerStop: async function () {
      // this runs after server.stop()
      // e.g., await Database.close()
    },
    preShutdown: async function () {
      // this runs after postServerStop() and before process.exit
    }
  }
})

// went smooth like chocolate :)

Feature Requests

Do you miss a feature? Please don’t hesitate to create an issue with a short description of your desired addition to this plugin.

Links & Resources

Contributing

  1. Create a fork
  2. Create your feature branch: git checkout -b my-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request 🚀

License

MIT © Future Studio


futurestud.io  ·  GitHub @futurestudio  ·  Twitter @futurestud_io

changelog

Changelog

3.0.1 - 2023-07-25

Updated

  • bump dependencies

3.0.0 - 2020-01-10

Breaking Changes

  • require Node.js v12
    • this change aligns with the hapi ecosystem requiring Node.js v12 with the release of hapi 19

2.4.0 - 2019-10-17

Added

  • basic TypeScript declarations in lib/index.d.ts

2.3.0 - 2019-10-12

Added

  • use Joi v16 to validate plugin options
  • Remove duplicated event names (previously hapi-pulse would register shutdown listeners for each event, even for duplicates)

Updated

  • bump dependencies
  • remove Node.js v11 from testing
  • log info message when already in stopping phase (previously hapi-pulse waited for the server stop without telling the user that the stopping phase is already in progress)

2.2.1 - 2019-04-24

Updated

  • updating to the scoped hapi dependencies
  • bump dependencies

2.2.0 - 2019-02-18

Added

  • increase max event listener count in case hapi-pulse would exceed it
  • save server.stop promise and don’t stop the hapi server more than once

Updated

  • bump dependencies

2.1.2 - 2019-01-26

Updated

  • Readme: rename GitHub references fs-opensource -> futurestudio

2.1.1 - 2019-01-22

Updated

  • test plugin for hapi 18
  • bump dependencies

2.1.0 - 2019-01-16

Added

  • validating the plugin options using Joi

Updated

  • refactor code to a class based structure
  • refactor methods and namings

2.0.0 - 2018-10-19

Added

  • new extension points:
    • preServerStop: async function that runs before the server stop
    • postServerStop: async function that runs after the server stop (this was previously onSignal)
    • preShutdown: async function that runs before the process exits

Updated

  • the onSignal extension point is now called postServerStop
  • remove all process listeners before exiting the process (to avoid memory leaks)
  • bump dependencies
  • add missing eslint devDependency

Deleted

  • removed useless .prettierignore file

Breaking Changes

  • onSignal becomes postServerStop: the method renaming makes it clear when the function gets called

1.1.1 - 2018-09-11

Added

  • new option:
    • logger: use a custom logger for a possible error message. If something goes south, hapi-pulse calls logger.error('message', error)

Updated

  • split plugin and hapi server options
  • extract plugin options and pass through the rest to hapi’s await server.stop(rest)
  • update readme: add examples and timeout option (passed through to hapi)

1.1.0 - 2018-08-20

Added

  • graceful server stop on SIGTERM
  • new options:
    • signals: define the list of signals you want your server to stop
    • onSignal: custom function with additional shutdown handling, called after server.stop

Updated

  • document new options in Readme

1.0.0 - 2018-04-06

Added

  • 1.0.0 release 🚀 🎉