You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

capture-exit

Package Overview
Dependencies
Maintainers
4
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

capture-exit

safely cleanup in signal handlers


Version published
Weekly downloads
4.8M
decreased by-3.96%
Maintainers
4
Install size
637 kB
Created
Weekly downloads
 

Package description

What is capture-exit?

The capture-exit npm package is designed to enable graceful shutdown and cleanup of Node.js processes by capturing and managing exit signals. It allows developers to register multiple cleanup functions that will be executed when the process exits, either normally or due to an uncaught exception, or when signals like SIGTERM or SIGINT are received. This is particularly useful for ensuring that resources are properly released and that critical finalization steps are completed before the process exits.

What are capture-exit's main functionalities?

Registering cleanup functions

This feature allows developers to register functions that will be executed when the process exits. The code sample demonstrates how to register a simple cleanup function that logs a message to the console before the process exits.

const captureExit = require('capture-exit');
captureExit.captureExit();

function cleanup() {
  console.log('Cleanup code executed before exit.');
}

captureExit.onExit(cleanup);

Handling multiple cleanup functions

This feature supports the registration of multiple cleanup functions. The code sample shows how to register two separate functions to handle different cleanup tasks, such as closing database connections and clearing cache.

const captureExit = require('capture-exit');
captureExit.captureExit();

function cleanupDatabase() {
  console.log('Database connections closed.');
}

function cleanupCache() {
  console.log('Cache cleared.');
}

captureExit.onExit(cleanupDatabase);
captureExit.onExit(cleanupCache);

Other packages similar to capture-exit

Readme

Source

capture-exit

Build status Build Status

Allow cooprative async exit handlers, we unfortunately must hijack process.exit.

It allows a handler to ensure exit, without that exit handler impeding other similar handlers

for example, see: sindresorhus/ora#27

Differences between process.on('exit') and captureExit.onExit(...) => https://github.com/ember-cli/capture-exit/issues/12

Installation

yarn add capture-exit
// or
npm install --save capture-exit

Usage

// as early in startup as possible
require('capture-exit').captureExit();

// when you want to schedule some work on exit:
function onExit() {
  return something.processWillExit(); // you can return promises, which will pause exit until fulfilled
}

require('capture-exit').onExit(onExit); // add an exit handler
require('capture-exit').offExit(onExit); // allows one to remove an exit handle if it is not longer required

FAQs

Package last updated on 21 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc