Product
Introducing Java Support in Socket
We're excited to announce that Socket now supports the Java programming language.
capture-exit
Advanced tools
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.
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);
exit-hook is a similar package that provides functionality to execute cleanup code when a Node.js process exits. Compared to capture-exit, exit-hook has a simpler API but does not handle uncaught exceptions by default.
async-exit-hook extends the basic functionality of exit-hook by supporting asynchronous cleanup functions. This is useful for handling tasks that require a bit more time to complete, such as asynchronous database shutdowns. It is similar to capture-exit in managing async operations but differs in API and additional configuration options.
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
yarn add capture-exit
// or
npm install --save capture-exit
// 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
safely cleanup in signal handlers
The npm package capture-exit receives a total of 3,815,670 weekly downloads. As such, capture-exit popularity was classified as popular.
We found that capture-exit demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
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.
Product
We're excited to announce that Socket now supports the Java programming language.
Security News
Socket detected a malicious Python package impersonating a popular browser cookie library to steal passwords, screenshots, webcam images, and Discord tokens.
Security News
Deno 2.0 is now available with enhanced package management, full Node.js and npm compatibility, improved performance, and support for major JavaScript frameworks.