feat(smartexit): Add silent logging option, structured shutdown logs, and killAll return stats
This commit is contained in:
@@ -1,5 +1,14 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2025-12-15 - 1.1.0 - feat(smartexit)
|
||||||
|
Add silent logging option, structured shutdown logs, and killAll return stats
|
||||||
|
|
||||||
|
- Introduce ISmartExitOptions with a silent flag to disable console logging
|
||||||
|
- Add internal log() helper and use a [smartexit] prefix for shutdown/error messages
|
||||||
|
- killAll() now returns Promise<{ processesKilled, cleanupFunctionsRan }> and tallies processes and cleanup functions run
|
||||||
|
- Constructor accepts options (backwards compatible) to configure behavior
|
||||||
|
- Documentation (readme.hints.md) updated with usage and example output
|
||||||
|
|
||||||
## 2025-12-15 - 1.0.24 - fix(deps)
|
## 2025-12-15 - 1.0.24 - fix(deps)
|
||||||
bump dependencies, update tests and docs, adjust tsconfig and npm release config
|
bump dependencies, update tests and docs, adjust tsconfig and npm release config
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1,29 @@
|
|||||||
|
# SmartExit - Development Hints
|
||||||
|
|
||||||
|
## Logging System
|
||||||
|
|
||||||
|
The module uses consolidated logging with a `[smartexit]` prefix:
|
||||||
|
|
||||||
|
- **Default behavior**: Logs a single summary line on shutdown
|
||||||
|
- **Silent mode**: Pass `{ silent: true }` to constructor to disable all logging
|
||||||
|
|
||||||
|
### Example output
|
||||||
|
```
|
||||||
|
[smartexit] Shutdown complete: killed 3 child processes, ran 2 cleanup functions
|
||||||
|
```
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
```typescript
|
||||||
|
// Default - logs summary
|
||||||
|
const smartExit = new SmartExit();
|
||||||
|
|
||||||
|
// Silent - no logging
|
||||||
|
const smartExit = new SmartExit({ silent: true });
|
||||||
|
```
|
||||||
|
|
||||||
|
## killAll() Return Value
|
||||||
|
|
||||||
|
The `killAll()` method returns stats about the cleanup:
|
||||||
|
```typescript
|
||||||
|
const { processesKilled, cleanupFunctionsRan } = await smartExit.killAll();
|
||||||
|
```
|
||||||
|
|||||||
@@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartexit',
|
name: '@push.rocks/smartexit',
|
||||||
version: '1.0.24',
|
version: '1.1.0',
|
||||||
description: 'A library for managing graceful shutdowns of Node.js processes by handling cleanup operations, including terminating child processes.'
|
description: 'A library for managing graceful shutdowns of Node.js processes by handling cleanup operations, including terminating child processes.'
|
||||||
}
|
}
|
||||||
|
|||||||
95
ts/index.ts
95
ts/index.ts
@@ -1,5 +1,9 @@
|
|||||||
import * as plugins from './smartexit.plugins.js';
|
import * as plugins from './smartexit.plugins.js';
|
||||||
|
|
||||||
|
export interface ISmartExitOptions {
|
||||||
|
silent?: boolean; // Completely disable logging
|
||||||
|
}
|
||||||
|
|
||||||
export type TProcessSignal =
|
export type TProcessSignal =
|
||||||
| 'SIGHUP' // Hangup detected on controlling terminal or death of controlling process
|
| 'SIGHUP' // Hangup detected on controlling terminal or death of controlling process
|
||||||
| 'SIGINT' // Interrupt from keyboard
|
| 'SIGINT' // Interrupt from keyboard
|
||||||
@@ -53,6 +57,22 @@ export class SmartExit {
|
|||||||
// Instance
|
// Instance
|
||||||
public processesToEnd = new plugins.lik.ObjectMap<plugins.childProcess.ChildProcess>();
|
public processesToEnd = new plugins.lik.ObjectMap<plugins.childProcess.ChildProcess>();
|
||||||
public cleanupFunctions = new plugins.lik.ObjectMap<() => Promise<any>>();
|
public cleanupFunctions = new plugins.lik.ObjectMap<() => Promise<any>>();
|
||||||
|
private options: ISmartExitOptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal logging helper that respects silent option
|
||||||
|
*/
|
||||||
|
private log(message: string, isError = false): void {
|
||||||
|
if (this.options.silent) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const prefix = '[smartexit]';
|
||||||
|
if (isError) {
|
||||||
|
console.error(`${prefix} ${message}`);
|
||||||
|
} else {
|
||||||
|
console.log(`${prefix} ${message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* adds a process to be exited
|
* adds a process to be exited
|
||||||
@@ -73,59 +93,66 @@ export class SmartExit {
|
|||||||
this.processesToEnd.remove(childProcessArg);
|
this.processesToEnd.remove(childProcessArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async killAll() {
|
public async killAll(): Promise<{ processesKilled: number; cleanupFunctionsRan: number }> {
|
||||||
console.log('Checking for remaining child processes before exit...');
|
const processes = this.processesToEnd.getArray();
|
||||||
if (this.processesToEnd.getArray().length > 0) {
|
const cleanupFuncs = this.cleanupFunctions.getArray();
|
||||||
console.log('found remaining child processes');
|
let processesKilled = 0;
|
||||||
let counter = 1;
|
let cleanupFunctionsRan = 0;
|
||||||
this.processesToEnd.forEach(async (childProcessArg) => {
|
|
||||||
const pid = childProcessArg.pid;
|
|
||||||
console.log(`killing process #${counter} with pid ${pid}`);
|
|
||||||
plugins.smartdelay.delayFor(10000).then(() => {
|
|
||||||
if (childProcessArg.killed) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
process.kill(pid, 'SIGKILL');
|
|
||||||
});
|
|
||||||
process.kill(pid, 'SIGINT');
|
|
||||||
|
|
||||||
counter++;
|
// Kill child processes
|
||||||
});
|
if (processes.length > 0) {
|
||||||
} else {
|
for (const childProcessArg of processes) {
|
||||||
console.log(`ChildProcesses look clean.`);
|
const pid = childProcessArg.pid;
|
||||||
|
if (pid) {
|
||||||
|
plugins.smartdelay.delayFor(10000).then(() => {
|
||||||
|
if (childProcessArg.killed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
process.kill(pid, 'SIGKILL');
|
||||||
|
});
|
||||||
|
process.kill(pid, 'SIGINT');
|
||||||
|
processesKilled++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (this.cleanupFunctions.getArray.length > 0) {
|
|
||||||
this.cleanupFunctions.forEach(async (cleanupFunction) => {
|
// Run cleanup functions
|
||||||
|
if (cleanupFuncs.length > 0) {
|
||||||
|
for (const cleanupFunction of cleanupFuncs) {
|
||||||
await cleanupFunction();
|
await cleanupFunction();
|
||||||
});
|
cleanupFunctionsRan++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
console.log(`Ready to exit!`);
|
|
||||||
|
return { processesKilled, cleanupFunctionsRan };
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor(optionsArg: ISmartExitOptions = {}) {
|
||||||
|
this.options = optionsArg;
|
||||||
|
|
||||||
// do app specific cleaning before exiting
|
// do app specific cleaning before exiting
|
||||||
process.on('exit', async (code) => {
|
process.on('exit', async (code) => {
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
console.log('Process wants to exit');
|
const { processesKilled, cleanupFunctionsRan } = await this.killAll();
|
||||||
await this.killAll();
|
this.log(`Shutdown complete: killed ${processesKilled} child processes, ran ${cleanupFunctionsRan} cleanup functions`);
|
||||||
console.log('Exited ok!');
|
|
||||||
} else {
|
} else {
|
||||||
console.error('Exited NOT OK!');
|
const { processesKilled, cleanupFunctionsRan } = await this.killAll();
|
||||||
|
this.log(`Shutdown complete: killed ${processesKilled} child processes, ran ${cleanupFunctionsRan} cleanup functions (exit code: ${code})`, true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// catch ctrl+c event and exit normally
|
// catch ctrl+c event and exit normally
|
||||||
process.on('SIGINT', async () => {
|
process.on('SIGINT', async () => {
|
||||||
console.log('Ctrl-C... or SIGINT signal received!');
|
const { processesKilled, cleanupFunctionsRan } = await this.killAll();
|
||||||
await this.killAll();
|
this.log(`Shutdown complete: killed ${processesKilled} child processes, ran ${cleanupFunctionsRan} cleanup functions`);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
//catch uncaught exceptions, trace, then exit normally
|
// catch uncaught exceptions, trace, then exit normally
|
||||||
process.on('uncaughtException', async (err) => {
|
process.on('uncaughtException', async (err) => {
|
||||||
console.log('SMARTEXIT: uncaught exception...');
|
this.log(`Uncaught exception: ${err.message}`, true);
|
||||||
console.log(err);
|
const { processesKilled, cleanupFunctionsRan } = await this.killAll();
|
||||||
await this.killAll();
|
this.log(`Shutdown complete: killed ${processesKilled} child processes, ran ${cleanupFunctionsRan} cleanup functions (exit code: 1)`, true);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user