Compare commits

..

No commits in common. "master" and "v1.0.22" have entirely different histories.

4 changed files with 95 additions and 92 deletions

View File

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

View File

@ -1,8 +1,8 @@
{ {
"name": "@push.rocks/smartexit", "name": "@push.rocks/smartexit",
"version": "1.0.23", "version": "1.0.22",
"private": false, "private": false,
"description": "A library for managing graceful shutdowns of Node.js processes by handling cleanup operations, including terminating child processes.", "description": "A library for performing cleanup operations before exiting a Node.js process, ensuring graceful shutdowns.",
"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,23 +43,12 @@
"type": "module", "type": "module",
"keywords": [ "keywords": [
"Node.js", "Node.js",
"TypeScript", "cleanup",
"process management",
"graceful shutdown", "graceful shutdown",
"cleanup operations", "process management",
"child process termination",
"signal handling", "signal handling",
"library", "child process termination",
"npm package", "TypeScript",
"async cleanup", "npm library"
"module", ]
"SIGINT handling",
"uncaught exception management",
"process exit management"
],
"homepage": "https://code.foss.global/push.rocks/smartexit",
"repository": {
"type": "git",
"url": "https://code.foss.global/push.rocks/smartexit.git"
}
} }

136
readme.md
View File

@ -1,16 +1,14 @@
# @push.rocks/smartexit 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:
A library for performing cleanup operations before exiting a Node.js process, ensuring graceful shutdowns.
## Install ## Install
To install `@push.rocks/smartexit`, use npm or Yarn as follows: To include `@push.rocks/smartexit` in your project, install it via npm:
```bash ```bash
npm install @push.rocks/smartexit --save npm install @push.rocks/smartexit --save
``` ```
or or using yarn:
```bash ```bash
yarn add @push.rocks/smartexit yarn add @push.rocks/smartexit
@ -18,93 +16,115 @@ yarn add @push.rocks/smartexit
## Usage ## Usage
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. `@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.
### Basic Setup ### Setting Up `SmartExit` in a TypeScript Project
First, import `SmartExit` from the package: 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`:
```typescript ```typescript
import { SmartExit } from '@push.rocks/smartexit'; import { SmartExit } from '@push.rocks/smartexit';
``` ```
Create an instance of `SmartExit`: #### Initialize `SmartExit`
Create a new `SmartExit` instance to manage your cleanup tasks and child processes:
```typescript ```typescript
const smartExit = new SmartExit(); const smartExit = new SmartExit();
``` ```
### Registering Cleanup Functions
`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...");
// Your cleanup operations here
});
```
### Managing Child Processes ### Managing Child Processes
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. If your application spawns child processes, `SmartExit` can ensure they are cleanly terminated during application shutdown.
To add a child process to the management list: #### Adding a Child Process
```typescript ```typescript
import { spawn } from 'child_process'; import { spawn } from 'child_process';
const childProcess = spawn('your_child_process'); // Example child process
smartExit.addProcess(childProcess); const exampleProcess = spawn('my-long-running-process');
// Add the process to SmartExit
smartExit.addProcess(exampleProcess);
``` ```
If necessary, you can remove a previously added child process: #### 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 ```typescript
smartExit.removeProcess(childProcess); smartExit.removeProcess(exampleProcess);
``` ```
### Triggering Cleanup ### Registering Cleanup Functions
`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: 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.
```typescript ```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}`);
});
smartExit.addCleanupFunction(async () => { smartExit.addCleanupFunction(async () => {
console.log("Closing Express server..."); console.log("Performing custom cleanup...");
await new Promise((resolve) => server.close(resolve)); await performCleanupAsync();
}); });
``` ```
### Handling Process Signals
`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:
```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');
smartExit.addProcess(childProcess);
// Register cleanup functions
smartExit.addCleanupFunction(async () => {
console.log('Cleaning up resources...');
await releaseResourcesAsync();
});
// 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 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. 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.
## 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.23', version: '1.0.22',
description: 'A library for managing graceful shutdowns of Node.js processes by handling cleanup operations, including terminating child processes.' description: 'A library for performing cleanup operations before exiting a Node.js process, ensuring graceful shutdowns.'
} }