Socket
Socket
Sign inDemoInstall

close-with-grace

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

close-with-grace

Exit your process, gracefully (if possible)


Version published
Weekly downloads
175K
increased by1.5%
Maintainers
1
Weekly downloads
 
Created

What is close-with-grace?

The 'close-with-grace' npm package helps manage graceful shutdowns in Node.js applications. It allows you to handle termination signals and perform cleanup tasks before the application exits, ensuring that resources are properly released and ongoing operations are completed.

What are close-with-grace's main functionalities?

Graceful Shutdown on SIGINT

This feature allows the application to handle the SIGINT signal (usually triggered by Ctrl+C) and perform cleanup tasks before shutting down. The provided code sample demonstrates how to set up a graceful shutdown with a delay and cleanup logic.

const closeWithGrace = require('close-with-grace');

const closeListeners = closeWithGrace({ delay: 500 }, async ({ err }) => {
  if (err) {
    console.error('Error during shutdown', err);
  }
  console.log('Cleaning up before shutdown...');
  await new Promise(resolve => setTimeout(resolve, 100));
  console.log('Cleanup complete.');
});

process.on('SIGINT', () => {
  console.log('Received SIGINT');
  closeListeners.uninstall();
});

Handling Multiple Signals

This feature allows the application to handle multiple termination signals (e.g., SIGTERM and SIGINT) and perform cleanup tasks before shutting down. The provided code sample demonstrates how to set up graceful shutdown handling for both SIGTERM and SIGINT signals.

const closeWithGrace = require('close-with-grace');

const closeListeners = closeWithGrace({ delay: 500 }, async ({ err }) => {
  if (err) {
    console.error('Error during shutdown', err);
  }
  console.log('Cleaning up before shutdown...');
  await new Promise(resolve => setTimeout(resolve, 100));
  console.log('Cleanup complete.');
});

process.on('SIGTERM', () => {
  console.log('Received SIGTERM');
  closeListeners.uninstall();
});

process.on('SIGINT', () => {
  console.log('Received SIGINT');
  closeListeners.uninstall();
});

Custom Cleanup Logic

This feature allows the application to define custom cleanup logic that will be executed before the application shuts down. The provided code sample demonstrates how to set up a graceful shutdown with custom cleanup logic and a delay.

const closeWithGrace = require('close-with-grace');

const closeListeners = closeWithGrace({ delay: 1000 }, async ({ err }) => {
  if (err) {
    console.error('Error during shutdown', err);
  }
  console.log('Performing custom cleanup...');
  // Custom cleanup logic here
  await new Promise(resolve => setTimeout(resolve, 500));
  console.log('Custom cleanup complete.');
});

Other packages similar to close-with-grace

Keywords

FAQs

Package last updated on 07 Apr 2023

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

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc