fix(core): update

This commit is contained in:
Philipp Kunz 2024-04-18 13:36:09 +02:00
parent d3fe10d282
commit 13f556abe5
4 changed files with 86 additions and 94 deletions

View File

@ -5,19 +5,25 @@
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "smartexit",
"description": "A library for performing cleanup operations before exiting a Node.js process, ensuring graceful shutdowns.",
"description": "A library for managing graceful shutdowns of Node.js processes by handling cleanup operations, including terminating child processes.",
"npmPackagename": "@push.rocks/smartexit",
"license": "MIT",
"projectDomain": "push.rocks",
"keywords": [
"Node.js",
"cleanup",
"graceful shutdown",
"process management",
"signal handling",
"child process termination",
"TypeScript",
"npm library"
"process management",
"graceful shutdown",
"cleanup operations",
"child process termination",
"signal handling",
"library",
"npm package",
"async cleanup",
"module",
"SIGINT handling",
"uncaught exception management",
"process exit management"
]
}
},

View File

@ -2,7 +2,7 @@
"name": "@push.rocks/smartexit",
"version": "1.0.22",
"private": false,
"description": "A library for performing cleanup operations before exiting a Node.js process, ensuring graceful shutdowns.",
"description": "A library for managing graceful shutdowns of Node.js processes by handling cleanup operations, including terminating child processes.",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"author": "Lossless GmbH",
@ -43,12 +43,18 @@
"type": "module",
"keywords": [
"Node.js",
"cleanup",
"graceful shutdown",
"process management",
"signal handling",
"child process termination",
"TypeScript",
"npm library"
"process management",
"graceful shutdown",
"cleanup operations",
"child process termination",
"signal handling",
"library",
"npm package",
"async cleanup",
"module",
"SIGINT handling",
"uncaught exception management",
"process exit management"
]
}
}

134
readme.md
View File

@ -1,14 +1,16 @@
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:
# @push.rocks/smartexit
A library for performing cleanup operations before exiting a Node.js process, ensuring graceful shutdowns.
## Install
To include `@push.rocks/smartexit` in your project, install it via npm:
To install `@push.rocks/smartexit`, use npm or Yarn as follows:
```bash
npm install @push.rocks/smartexit --save
```
or using yarn:
or
```bash
yarn add @push.rocks/smartexit
@ -16,115 +18,93 @@ yarn add @push.rocks/smartexit
## Usage
`@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.
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.
### Setting Up `SmartExit` in a TypeScript Project
### Basic Setup
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`:
First, import `SmartExit` from the package:
```typescript
import { SmartExit } from '@push.rocks/smartexit';
```
#### Initialize `SmartExit`
Create a new `SmartExit` instance to manage your cleanup tasks and child processes:
Create an instance of `SmartExit`:
```typescript
const smartExit = new SmartExit();
```
### Managing Child Processes
If your application spawns child processes, `SmartExit` can ensure they are cleanly terminated during application shutdown.
#### Adding a Child Process
```typescript
import { spawn } from 'child_process';
// Example child process
const exampleProcess = spawn('my-long-running-process');
// Add the process to SmartExit
smartExit.addProcess(exampleProcess);
```
#### Removing a Child Process
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:
```typescript
smartExit.removeProcess(exampleProcess);
```
### Registering Cleanup Functions
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.
`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.
```typescript
smartExit.addCleanupFunction(async () => {
console.log("Performing custom cleanup...");
await performCleanupAsync();
// Your cleanup operations here
});
```
### Handling Process Signals
### Managing Child Processes
`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.
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.
#### Triggering Cleanup Manually
In some scenarios, you may wish to initiate the cleanup process manually:
To add a child process to the management list:
```typescript
async function initiateShutdown() {
await smartExit.killAll();
process.exit(0);
}
```
This approach is useful in situations where you have a custom shutdown sequence or need to perform additional actions before exiting.
### Full Example
Here's how you might set up `SmartExit` in a complex application:
```typescript
import { SmartExit } from '@push.rocks/smartexit';
import { spawn } from 'child_process';
// Setup SmartExit
const smartExit = new SmartExit();
// Spawn and register a child process
const childProcess = spawn('path-to-my-script');
const childProcess = spawn('your_child_process');
smartExit.addProcess(childProcess);
```
If necessary, you can remove a previously added child process:
```typescript
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:
```typescript
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
```typescript
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:
```typescript
const server = app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
// Register cleanup functions
smartExit.addCleanupFunction(async () => {
console.log('Cleaning up resources...');
await releaseResourcesAsync();
console.log("Closing Express server...");
await new Promise((resolve) => server.close(resolve));
});
// Optional: custom signal handling or manual shutdown trigger
process.on('someCustomSignal', async () => {
console.log('Custom signal received, initiating cleanup...');
await smartExit.killAll();
process.exit(0);
});
// 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.
```
---
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.
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

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartexit',
version: '1.0.22',
description: 'A library for performing cleanup operations before exiting a Node.js process, ensuring graceful shutdowns.'
version: '1.0.23',
description: 'A library for managing graceful shutdowns of Node.js processes by handling cleanup operations, including terminating child processes.'
}