@mcf/provision-process

0.2.2 • Public • Published

npm version

@mcf/provision-process

Provisions the process for a graceful termination.

Installation

npm i @mcf/provision-process;

OR

yarn add @mcf/provision-process;

Usage

Without middleware

provision-process can be used to listen for SIGINT and SIGTERM operating system signals.

Example

const provisionProcess = require('@mcf/provision-process`);

// ...

// do the deed
provisionProcess();

With middleware

Use the function .addMiddleware() to add methods which should run before the application terminates.

The .addMiddleware() function accepts one parameter which should be a function that returns a Promise. These Promises will resolved just before the process exits. The .addMiddleware() function is also chainable.

These middlewares should do things such as ensuring connection pools are closed, server being gracefully terminated, sending the correct responses to in-processing requests, and having nothing left in the Node event queue.

Example

const provisionProcess = require('@mcf/provision-process');
// fictitious database instance
const db = require('./path/to/db-instance');
// your logging module
const logger = require('./path/to/logger');
// fictitious server
const server = require('express')().listen();

provisionProcess
  .addMiddleware(() => (new Promise((resolve, reject) => {
    logger.info('closing database connections...');
    db
      .destroy()
      .then((error) => {
        if (error) {
          reject(error);
        } else {
          resolve();
          logger.info('database connections closed');
        }
      });
  })))
  .addMiddleware(() => (new Promise((resolve, reject) => {
    logger.info('closing server gracefully...');
    server
      .close((error) => {
        if (error) {
          reject(error);
        } else {
          resolve();
          logger.info('server closed');
        }
      });
  })));

// do the deed
provisionProcess();

Note

To speed things up, the middlewares you add are executed in parallel. To execute things in sequential order, resolve your own promises and return the final result:

provisionProcess
  .addMiddleware(() => (
    (new Promise((resolve, reject) => {
      // ...
    })).then((result1) => {
      return new Promise((resolve, reject) => {
        // ...
      })
    }).then((result2) => {
      return new Promise((resolve, reject) => {
        // ...
      })
    })
  ));

Development

Installing Dependencies

Run the following from the root of the repository to initialise the dependencies since Lerna manages the dependencies for us across the multiple packages:

lerna bootstrap;

Running Tests

To run the tests during development, use:

npm run test-watch;

To run the tests on the built package, use:

npm run test;

Building

npm run build;

ChangeLog

0.1.0

  • Initial release

Readme

Keywords

none

Package Sidebar

Install

npm i @mcf/provision-process

Weekly Downloads

8

Version

0.2.2

License

ISC

Unpacked Size

8.77 kB

Total Files

4

Last publish

Collaborators

  • at_govtech
  • boonsiangteh
  • vicheng.gt
  • npm-govtechsg-mcf-sh