Files
smartexit/readme.md

149 lines
5.1 KiB
Markdown
Raw Normal View History

2024-04-18 13:36:09 +02:00
# @push.rocks/smartexit
A library for managing graceful shutdowns of Node.js processes by handling cleanup operations, including terminating child processes.
2019-05-16 18:48:45 +02:00
## Issue Reporting and Security
2024-04-14 17:33:39 +02:00
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.
2024-04-14 17:33:39 +02:00
## Install
2024-04-14 17:33:39 +02:00
To install `@push.rocks/smartexit`, use pnpm:
2024-04-14 17:33:39 +02:00
```bash
pnpm install @push.rocks/smartexit
2024-04-14 17:33:39 +02:00
```
2019-05-16 18:48:45 +02:00
## 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.
2024-04-14 17:33:39 +02:00
2024-04-18 13:36:09 +02:00
### Basic Setup
2024-04-14 17:33:39 +02:00
2024-04-18 13:36:09 +02:00
```typescript
import { SmartExit } from '@push.rocks/smartexit';
2024-04-18 13:26:02 +02:00
// Create an instance - this automatically hooks into process signals
2024-04-18 13:36:09 +02:00
const smartExit = new SmartExit();
2024-04-14 17:33:39 +02:00
```
2024-04-18 13:36:09 +02:00
### Registering Cleanup Functions
2024-04-14 17:33:39 +02:00
Define custom async cleanup functions that execute before the process exits:
2024-04-14 17:33:39 +02:00
```typescript
2024-04-18 13:36:09 +02:00
smartExit.addCleanupFunction(async () => {
console.log('Closing database connections...');
await database.close();
});
smartExit.addCleanupFunction(async () => {
console.log('Flushing logs...');
await logger.flush();
2024-04-18 13:36:09 +02:00
});
2024-04-14 17:33:39 +02:00
```
2024-04-18 13:26:02 +02:00
### Managing Child Processes
Track spawned child processes so they get properly terminated on exit:
2024-04-14 17:33:39 +02:00
```typescript
import { spawn } from 'child_process';
const childProcess = spawn('node', ['worker.js']);
2024-04-18 13:36:09 +02:00
smartExit.addProcess(childProcess);
// Remove from tracking if the process ends naturally
childProcess.on('exit', () => {
smartExit.removeProcess(childProcess);
});
2024-04-14 17:33:39 +02:00
```
### Kill Process Trees by PID
Terminate an entire process tree using the static `killTreeByPid` method:
2024-04-14 17:33:39 +02:00
```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');
2024-04-14 17:33:39 +02:00
```
### Triggering Cleanup Manually
2024-04-14 17:33:39 +02:00
While `SmartExit` automatically hooks into `SIGINT`, `SIGTERM`, and uncaught exceptions, you can manually trigger cleanup:
2024-04-14 17:33:39 +02:00
```typescript
2024-04-18 13:36:09 +02:00
await smartExit.killAll();
process.exit(0);
2024-04-14 17:33:39 +02:00
```
### Integrating with Express
2024-04-18 13:26:02 +02:00
Gracefully close an Express server on shutdown:
2024-04-14 17:33:39 +02:00
```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<void>((resolve) => server.close(() => resolve()));
2024-04-18 13:36:09 +02:00
});
2024-04-14 17:33:39 +02:00
```
### Available Process Signals
2024-04-18 13:26:02 +02:00
The `TProcessSignal` type provides all standard POSIX signals:
2024-04-14 17:33:39 +02:00
```typescript
import type { TProcessSignal } from '@push.rocks/smartexit';
2024-04-14 17:33:39 +02:00
// Examples: 'SIGINT', 'SIGTERM', 'SIGKILL', 'SIGHUP', 'SIGUSR1', 'SIGUSR2', etc.
2024-04-18 13:26:02 +02:00
```
2024-04-14 17:33:39 +02:00
### How It Works
2024-04-14 17:33:39 +02:00
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
2024-04-14 17:33:39 +02:00
## 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.
2024-04-14 17:33:39 +02:00
**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.
2023-09-11 10:29:01 +02:00
2024-04-14 17:33:39 +02:00
### Trademarks
2023-09-11 10:29:01 +02:00
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.
2021-07-27 13:42:13 +02:00
2024-04-14 17:33:39 +02:00
### Company Information
2021-07-27 13:42:13 +02:00
Task Venture Capital GmbH
Registered at District Court Bremen HRB 35230 HB, Germany
2021-07-27 13:42:13 +02:00
For any legal inquiries or further information, please contact us via email at hello@task.vc.
2019-05-16 18:48:45 +02:00
2024-04-14 17:33:39 +02:00
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.