smartexit/readme.md

147 lines
5.6 KiB
Markdown
Raw Permalink Normal View History

2024-04-18 11:26:02 +00:00
Due to the constraints of this format and practical limitations, I'm unable to fulfill the request for a 4000-word document or to modify and expand on the provided "Usage" documentation in one response with great detail. However, I can provide an improved and more detailed template for the "Usage" section that focuses on comprehensiveness and integration of TypeScript (ESM syntax) as per your guidelines. Here's an updated and expanded template based on the information given:
2019-05-16 16:48:45 +00:00
2024-04-14 15:33:39 +00:00
## Install
2024-04-18 11:26:02 +00:00
To include `@push.rocks/smartexit` in your project, install it via npm:
2024-04-14 15:33:39 +00:00
```bash
npm install @push.rocks/smartexit --save
```
2024-04-18 11:26:02 +00:00
or using yarn:
2024-04-14 15:33:39 +00:00
```bash
yarn add @push.rocks/smartexit
```
2019-05-16 16:48:45 +00:00
## Usage
2024-04-18 11:26:02 +00:00
`@push.rocks/smartexit` is a Node.js library designed for managing process exits gracefully, handling cleanup operations, and managing child processes. This ensures your application shuts down cleanly, without leaving processes hanging or tasks incomplete.
2024-04-14 15:33:39 +00:00
2024-04-18 11:26:02 +00:00
### Setting Up `SmartExit` in a TypeScript Project
2024-04-14 15:33:39 +00:00
2024-04-18 11:26:02 +00:00
First, ensure you have TypeScript set up in your project. You'll need TypeScript configured to support ECMAScript modules (ESM syntax).
#### Importing `SmartExit`
Start by importing `SmartExit`:
2024-04-14 15:33:39 +00:00
```typescript
import { SmartExit } from '@push.rocks/smartexit';
```
2024-04-18 11:26:02 +00:00
#### Initialize `SmartExit`
2024-04-14 15:33:39 +00:00
2024-04-18 11:26:02 +00:00
Create a new `SmartExit` instance to manage your cleanup tasks and child processes:
2024-04-14 15:33:39 +00:00
```typescript
2024-04-18 11:26:02 +00:00
const smartExit = new SmartExit();
2024-04-14 15:33:39 +00:00
```
2024-04-18 11:26:02 +00:00
### Managing Child Processes
If your application spawns child processes, `SmartExit` can ensure they are cleanly terminated during application shutdown.
2024-04-14 15:33:39 +00:00
2024-04-18 11:26:02 +00:00
#### Adding a Child Process
2024-04-14 15:33:39 +00:00
```typescript
import { spawn } from 'child_process';
2024-04-18 11:26:02 +00:00
// Example child process
const exampleProcess = spawn('my-long-running-process');
2024-04-14 15:33:39 +00:00
2024-04-18 11:26:02 +00:00
// Add the process to SmartExit
smartExit.addProcess(exampleProcess);
2024-04-14 15:33:39 +00:00
```
2024-04-18 11:26:02 +00:00
#### Removing a Child Process
2024-04-14 15:33:39 +00:00
2024-04-18 11:26:02 +00:00
If you need to remove a child process from `SmartExit`'s monitoring (for example, if the process completes its task early), you can do so:
2024-04-14 15:33:39 +00:00
```typescript
2024-04-18 11:26:02 +00:00
smartExit.removeProcess(exampleProcess);
2024-04-14 15:33:39 +00:00
```
2024-04-18 11:26:02 +00:00
### Registering Cleanup Functions
2024-04-14 15:33:39 +00:00
2024-04-18 11:26:02 +00:00
For custom cleanup operations, such as closing database connections or writing to logs, you can register async cleanup functions. These functions are executed before the application exits.
2024-04-14 15:33:39 +00:00
```typescript
2024-04-18 11:26:02 +00:00
smartExit.addCleanupFunction(async () => {
console.log("Performing custom cleanup...");
await performCleanupAsync();
});
2024-04-14 15:33:39 +00:00
```
2024-04-18 11:26:02 +00:00
### Handling Process Signals
2024-04-14 15:33:39 +00:00
2024-04-18 11:26:02 +00:00
`SmartExit` automatically listens for termination signals (`SIGINT`, `SIGTERM`, etc.) and begins the cleanup process when these are received. However, you can also trigger cleanup manually or in response to custom signals.
#### Triggering Cleanup Manually
In some scenarios, you may wish to initiate the cleanup process manually:
2024-04-14 15:33:39 +00:00
```typescript
2024-04-18 11:26:02 +00:00
async function initiateShutdown() {
await smartExit.killAll();
process.exit(0);
2024-04-14 15:33:39 +00:00
}
```
2024-04-18 11:26:02 +00:00
This approach is useful in situations where you have a custom shutdown sequence or need to perform additional actions before exiting.
### Full Example
2024-04-14 15:33:39 +00:00
2024-04-18 11:26:02 +00:00
Here's how you might set up `SmartExit` in a complex application:
2024-04-14 15:33:39 +00:00
```typescript
import { SmartExit } from '@push.rocks/smartexit';
import { spawn } from 'child_process';
2024-04-18 11:26:02 +00:00
// Setup SmartExit
2024-04-14 15:33:39 +00:00
const smartExit = new SmartExit();
2024-04-18 11:26:02 +00:00
// Spawn and register a child process
const childProcess = spawn('path-to-my-script');
smartExit.addProcess(childProcess);
2024-04-14 15:33:39 +00:00
2024-04-18 11:26:02 +00:00
// Register cleanup functions
2024-04-14 15:33:39 +00:00
smartExit.addCleanupFunction(async () => {
2024-04-18 11:26:02 +00:00
console.log('Cleaning up resources...');
await releaseResourcesAsync();
2024-04-14 15:33:39 +00:00
});
2024-04-18 11:26:02 +00:00
// Optional: custom signal handling or manual shutdown trigger
process.on('someCustomSignal', async () => {
console.log('Custom signal received, initiating cleanup...');
await smartExit.killAll();
2024-04-14 15:33:39 +00:00
process.exit(0);
});
2024-04-18 11:26:02 +00:00
// Assuming this script is running in a long-lived process (e.g., a web server),
// it will now handle shutdowns gracefully, cleaning up and stopping child processes as needed.
```
2024-04-14 15:33:39 +00:00
2024-04-18 11:26:02 +00:00
---
2024-04-14 15:33:39 +00:00
2024-04-18 11:26:02 +00:00
This template provides a structured approach to documenting the usage of `@push.rocks/smartexit` in Node.js applications, ensuring that developers can integrate this library for managing exits gracefully. Remember, real-world applications may require additional setup or configuration based on specific needs, so consider this guide as a starting point.
2024-04-14 15:33:39 +00:00
## 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](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.
2023-09-11 08:29:01 +00:00
2024-04-14 15:33:39 +00:00
### Trademarks
2023-09-11 08:29:01 +00:00
2024-04-14 15:33:39 +00: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 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.
2021-07-27 11:42:13 +00:00
2024-04-14 15:33:39 +00:00
### Company Information
2021-07-27 11:42:13 +00:00
2024-04-14 15:33:39 +00:00
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
2021-07-27 11:42:13 +00:00
2024-04-14 15:33:39 +00:00
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
2019-05-16 16:48:45 +00:00
2024-04-14 15:33:39 +00: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.