# @push.rocks/smartexit A library for managing graceful shutdowns of Node.js processes by handling cleanup operations, including terminating child processes. ## Issue Reporting and Security For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly. ## Install To install `@push.rocks/smartexit`, use pnpm: ```bash pnpm install @push.rocks/smartexit ``` ## Usage `@push.rocks/smartexit` helps you gracefully shut down Node.js applications by running cleanup operations before the process exits. It handles signal interception (`SIGINT`, `SIGTERM`), uncaught exception management, and child process termination. ### Basic Setup ```typescript import { SmartExit } from '@push.rocks/smartexit'; // Create an instance - this automatically hooks into process signals const smartExit = new SmartExit(); ``` ### Registering Cleanup Functions Define custom async cleanup functions that execute before the process exits: ```typescript smartExit.addCleanupFunction(async () => { console.log('Closing database connections...'); await database.close(); }); smartExit.addCleanupFunction(async () => { console.log('Flushing logs...'); await logger.flush(); }); ``` ### Managing Child Processes Track spawned child processes so they get properly terminated on exit: ```typescript import { spawn } from 'child_process'; const childProcess = spawn('node', ['worker.js']); smartExit.addProcess(childProcess); // Remove from tracking if the process ends naturally childProcess.on('exit', () => { smartExit.removeProcess(childProcess); }); ``` ### Kill Process Trees by PID Terminate an entire process tree using the static `killTreeByPid` method: ```typescript import { SmartExit, type TProcessSignal } from '@push.rocks/smartexit'; // Kill with default SIGKILL await SmartExit.killTreeByPid(12345); // Kill with a specific signal await SmartExit.killTreeByPid(12345, 'SIGTERM'); ``` ### Triggering Cleanup Manually While `SmartExit` automatically hooks into `SIGINT`, `SIGTERM`, and uncaught exceptions, you can manually trigger cleanup: ```typescript await smartExit.killAll(); process.exit(0); ``` ### Integrating with Express Gracefully close an Express server on shutdown: ```typescript import express from 'express'; import { SmartExit } from '@push.rocks/smartexit'; const app = express(); const smartExit = new SmartExit(); const server = app.listen(3000, () => { console.log('Server running on port 3000'); }); smartExit.addCleanupFunction(async () => { console.log('Closing Express server...'); await new Promise((resolve) => server.close(() => resolve())); }); ``` ### Available Process Signals The `TProcessSignal` type provides all standard POSIX signals: ```typescript import type { TProcessSignal } from '@push.rocks/smartexit'; // Examples: 'SIGINT', 'SIGTERM', 'SIGKILL', 'SIGHUP', 'SIGUSR1', 'SIGUSR2', etc. ``` ### How It Works When you create a `SmartExit` instance, it automatically: 1. **Hooks `SIGINT`** (Ctrl+C): Runs cleanup and exits with code 0 2. **Hooks process `exit`**: Runs cleanup when `process.exit(0)` is called 3. **Catches uncaught exceptions**: Logs the error, runs cleanup, and exits with code 1 On `killAll()`, it: - Sends `SIGINT` to all tracked child processes - Waits 10 seconds, then sends `SIGKILL` if processes are still alive - Executes all registered cleanup functions ## License and Legal Information This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file. **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 or third parties, 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 or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar. ### Company Information Task Venture Capital GmbH Registered at District Court Bremen HRB 35230 HB, Germany For any legal inquiries or 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.