npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@push.rocks/smartexit

v1.0.23

Published

A library for managing graceful shutdowns of Node.js processes by handling cleanup operations, including terminating child processes.

Downloads

544

Readme

@push.rocks/smartexit

A library for performing cleanup operations before exiting a Node.js process, ensuring graceful shutdowns.

Install

To install @push.rocks/smartexit, use npm or Yarn as follows:

npm install @push.rocks/smartexit --save

or

yarn add @push.rocks/smartexit

Usage

This library is designed to facilitate graceful shutdowns in Node.js applications by allowing developers to easily perform cleanup operations (like closing database connections or stopping child processes) before the process exits. Below is a guide on integrating @push.rocks/smartexit using TypeScript.

Basic Setup

First, import SmartExit from the package:

import { SmartExit } from '@push.rocks/smartexit';

Create an instance of SmartExit:

const smartExit = new SmartExit();

Registering Cleanup Functions

SmartExit enables you to define custom cleanup functions that are executed before the process exits. These functions should return a promise to ensure all asynchronous cleanup operations complete successfully.

smartExit.addCleanupFunction(async () => {
  console.log("Performing custom cleanup...");
  // Your cleanup operations here
});

Managing Child Processes

It's common for a Node.js application to spawn child processes. SmartExit can also manage these, ensuring all child processes cleanly exit before the parent process exits.

To add a child process to the management list:

import { spawn } from 'child_process';

const childProcess = spawn('your_child_process');
smartExit.addProcess(childProcess);

If necessary, you can remove a previously added child process:

smartExit.removeProcess(childProcess);

Triggering Cleanup

SmartExit automatically hooks into several process signal events (like SIGTERM and SIGINT) to start the cleanup procedure. However, you can manually trigger the cleanup and exit processes:

await smartExit.killAll();
process.exit(0); // or process.exit(1) to indicate an error state if needed

Advanced Usage

In more complex scenarios, you might need to conditionally add or remove cleanup functions and child processes, or integrate SmartExit with other libraries and frameworks for more comprehensive process management and shutdown procedures. Here you can leverage the full flexibility of JavaScript and TypeScript to tailor the shutdown behavior to your application's specific needs.

Example: Shutdown on Uncaught Exceptions

process.on('uncaughtException', async (error) => {
  console.error("Uncaught Exception:", error);
  await smartExit.killAll(); // Ensures all cleanup functions and child processes are managed
  process.exit(1); // Exits with error
});

Integrating with Express

If your application uses Express, you might want to close the server gracefully:

const server = app.listen(port, () => {
  console.log(`Server started on port ${port}`);
});

smartExit.addCleanupFunction(async () => {
  console.log("Closing Express server...");
  await new Promise((resolve) => server.close(resolve));
});

This documentation provides a foundational understanding of how to utilize @push.rocks/smartexit for managing graceful shutdowns in Node.js applications with TypeScript. Remember to adjust the code examples as necessary to fit your specific project requirements.

License and Legal Information

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at [email protected].

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.