smartexit/readme.md
2024-04-18 13:36:09 +02:00

4.8 KiB

@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.

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 hello@task.vc.

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.