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", "githost": "code.foss.global",
"gitscope": "push.rocks", "gitscope": "push.rocks",
"gitrepo": "smartexit", "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", "npmPackagename": "@push.rocks/smartexit",
"license": "MIT", "license": "MIT",
"projectDomain": "push.rocks", "projectDomain": "push.rocks",
"keywords": [ "keywords": [
"Node.js", "Node.js",
"cleanup",
"graceful shutdown",
"process management",
"signal handling",
"child process termination",
"TypeScript", "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", "name": "@push.rocks/smartexit",
"version": "1.0.22", "version": "1.0.22",
"private": false, "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", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"author": "Lossless GmbH", "author": "Lossless GmbH",
@ -43,12 +43,18 @@
"type": "module", "type": "module",
"keywords": [ "keywords": [
"Node.js", "Node.js",
"cleanup",
"graceful shutdown",
"process management",
"signal handling",
"child process termination",
"TypeScript", "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"
] ]
} }

130
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 ## 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 ```bash
npm install @push.rocks/smartexit --save npm install @push.rocks/smartexit --save
``` ```
or using yarn: or
```bash ```bash
yarn add @push.rocks/smartexit yarn add @push.rocks/smartexit
@ -16,115 +18,93 @@ yarn add @push.rocks/smartexit
## Usage ## 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). First, import `SmartExit` from the package:
#### Importing `SmartExit`
Start by importing `SmartExit`:
```typescript ```typescript
import { SmartExit } from '@push.rocks/smartexit'; import { SmartExit } from '@push.rocks/smartexit';
``` ```
#### Initialize `SmartExit` Create an instance of `SmartExit`:
Create a new `SmartExit` instance to manage your cleanup tasks and child processes:
```typescript ```typescript
const smartExit = new SmartExit(); 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 ### 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 ```typescript
smartExit.addCleanupFunction(async () => { smartExit.addCleanupFunction(async () => {
console.log("Performing custom cleanup..."); 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 To add a child process to the management list:
In some scenarios, you may wish to initiate the cleanup process manually:
```typescript ```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'; import { spawn } from 'child_process';
// Setup SmartExit const childProcess = spawn('your_child_process');
const smartExit = new SmartExit();
// Spawn and register a child process
const childProcess = spawn('path-to-my-script');
smartExit.addProcess(childProcess); smartExit.addProcess(childProcess);
```
// Register cleanup functions If necessary, you can remove a previously added child process:
smartExit.addCleanupFunction(async () => {
console.log('Cleaning up resources...');
await releaseResourcesAsync();
});
// Optional: custom signal handling or manual shutdown trigger ```typescript
process.on('someCustomSignal', async () => { smartExit.removeProcess(childProcess);
console.log('Custom signal received, initiating cleanup...'); ```
### 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(); await smartExit.killAll();
process.exit(0); 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}`);
}); });
// Assuming this script is running in a long-lived process (e.g., a web server), smartExit.addCleanupFunction(async () => {
// it will now handle shutdowns gracefully, cleaning up and stopping child processes as needed. console.log("Closing Express server...");
await new Promise((resolve) => server.close(resolve));
});
``` ```
--- ---
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 ## License and Legal Information

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartexit', name: '@push.rocks/smartexit',
version: '1.0.22', version: '1.0.23',
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.'
} }