@ladjs/graceful
Advanced tools
Comparing version 3.0.2 to 3.1.0
@@ -1,14 +0,14 @@ | ||
interface Logger { | ||
type Logger = { | ||
info(): unknown; | ||
warn(): unknown; | ||
error(): unknown; | ||
} | ||
}; | ||
interface LilHttpTerminator { | ||
type LilHttpTerminator = { | ||
gracefulTerminationTimeout?: number; | ||
maxWaitTimeout?: number; | ||
logger?: Logger; | ||
} | ||
}; | ||
export interface GracefulOptions { | ||
export type GracefulOptions = { | ||
servers?: Array<{ close(): unknown }>; | ||
@@ -22,3 +22,4 @@ brees?: Array<{ stop(): Promise<void> }>; | ||
lilHttpTerminator?: LilHttpTerminator; | ||
} | ||
ignoreHook?: string | boolean; | ||
}; | ||
@@ -25,0 +26,0 @@ export default class Graceful { |
71
index.js
@@ -22,2 +22,3 @@ const http = require('http'); | ||
lilHttpTerminator: {}, | ||
ignoreHook: 'ignore_hook', | ||
...config | ||
@@ -34,8 +35,5 @@ }; | ||
// shortcut logger | ||
this.logger = this.config.logger; | ||
// if lilHttpTerminator does not have a logger set then re-use `this.logger` | ||
// if lilHttpTerminator does not have a logger set then re-use `this.config.logger` | ||
if (!this.config.lilHttpTerminator.logger) | ||
this.config.lilHttpTerminator.logger = this.logger; | ||
this.config.lilHttpTerminator.logger = this.config.logger; | ||
@@ -84,3 +82,3 @@ // prevent multiple SIGTERM/SIGHUP/SIGINT from firing graceful exit | ||
warning.emitter = null; | ||
this.logger.warn(warning); | ||
this.config.logger.warn(warning); | ||
}); | ||
@@ -101,3 +99,3 @@ | ||
process.once('uncaughtException', (err) => { | ||
this.logger.error(err); | ||
this.config.logger.error(err); | ||
process.exit(1); | ||
@@ -110,3 +108,7 @@ }); | ||
if (message === 'shutdown') { | ||
this.logger.info('Received shutdown message'); | ||
if (this.config.ignoreHook) | ||
this.config.logger.info('Received shutdown message', { | ||
[this.config.ignoreHook]: true | ||
}); | ||
else this.config.logger.info('Received shutdown message'); | ||
await this.exit(); | ||
@@ -147,3 +149,6 @@ } | ||
} catch (err) { | ||
this.logger.error(err, { code }); | ||
this.config.logger.error(err, { | ||
code, | ||
...(this.config.ignoreHook ? { [this.config.ignoreHook]: true } : {}) | ||
}); | ||
} | ||
@@ -163,3 +168,6 @@ } | ||
} catch (err) { | ||
this.logger.error(err, { code }); | ||
this.config.logger.error(err, { | ||
code, | ||
...(this.config.ignoreHook ? { [this.config.ignoreHook]: true } : {}) | ||
}); | ||
} | ||
@@ -180,3 +188,6 @@ } | ||
} catch (err) { | ||
this.logger.error(err, { code }); | ||
this.config.logger.error(err, { | ||
code, | ||
...(this.config.ignoreHook ? { [this.config.ignoreHook]: true } : {}) | ||
}); | ||
} | ||
@@ -195,3 +206,6 @@ } | ||
} catch (err) { | ||
this.logger.error(err, { code }); | ||
this.config.logger.error(err, { | ||
code, | ||
...(this.config.ignoreHook ? { [this.config.ignoreHook]: true } : {}) | ||
}); | ||
} | ||
@@ -210,3 +224,6 @@ } | ||
} catch (err) { | ||
this.logger.error(err, { code }); | ||
this.config.logger.error(err, { | ||
code, | ||
...(this.config.ignoreHook ? { [this.config.ignoreHook]: true } : {}) | ||
}); | ||
} | ||
@@ -224,6 +241,13 @@ } | ||
async exit(code) { | ||
if (code) this.logger.info('Gracefully exiting', { code }); | ||
if (code) | ||
this.config.logger.info('Gracefully exiting', { | ||
code, | ||
...(this.config.ignoreHook ? { [this.config.ignoreHook]: true } : {}) | ||
}); | ||
if (this._isExiting) { | ||
this.logger.info('Graceful exit already in progress', { code }); | ||
this.config.logger.info('Graceful exit already in progress', { | ||
code, | ||
...(this.config.ignoreHook ? { [this.config.ignoreHook]: true } : {}) | ||
}); | ||
return; | ||
@@ -236,7 +260,10 @@ } | ||
setTimeout(() => { | ||
this.logger.error( | ||
this.config.logger.error( | ||
new Error( | ||
`Graceful exit failed, timeout of ${this.config.timeoutMs}ms was exceeded` | ||
), | ||
{ code } | ||
{ | ||
code, | ||
...(this.config.ignoreHook ? { [this.config.ignoreHook]: true } : {}) | ||
} | ||
); | ||
@@ -260,7 +287,13 @@ // eslint-disable-next-line unicorn/no-process-exit | ||
]); | ||
this.logger.info('Gracefully exited', { code }); | ||
this.config.logger.info('Gracefully exited', { | ||
code, | ||
...(this.config.ignoreHook ? { [this.config.ignoreHook]: true } : {}) | ||
}); | ||
// eslint-disable-next-line unicorn/no-process-exit | ||
process.exit(0); | ||
} catch (err) { | ||
this.logger.error(err, { code }); | ||
this.config.logger.error(err, { | ||
code, | ||
...(this.config.ignoreHook ? { [this.config.ignoreHook]: true } : {}) | ||
}); | ||
// eslint-disable-next-line unicorn/no-process-exit | ||
@@ -267,0 +300,0 @@ process.exit(1); |
{ | ||
"name": "@ladjs/graceful", | ||
"description": "Gracefully exit HTTP servers (Express/Koa/Fastify/etc), databases (Mongo/Mongoose), Bree job schedulers, and custom handlers.", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"author": "Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)", | ||
@@ -21,24 +21,24 @@ "bugs": { | ||
"devDependencies": { | ||
"@commitlint/cli": "^17.0.2", | ||
"@commitlint/config-conventional": "^17.0.2", | ||
"@ladjs/api": "^10.0.3", | ||
"@ladjs/web": "^14.0.3", | ||
"ava": "^4.3.0", | ||
"@commitlint/cli": "^17.2.0", | ||
"@commitlint/config-conventional": "^17.2.0", | ||
"@ladjs/api": "^12.0.2", | ||
"@ladjs/web": "^18.0.4", | ||
"ava": "^5.1.0", | ||
"cross-env": "^7.0.3", | ||
"eslint": "8.17.0", | ||
"eslint": "8.27.0", | ||
"eslint-config-xo-lass": "^2.0.1", | ||
"express": "^4.18.1", | ||
"fastify": "^3.29.0", | ||
"express": "^4.18.2", | ||
"fastify": "^4.10.0", | ||
"fixpack": "^4.0.0", | ||
"get-port": "5", | ||
"husky": "^8.0.1", | ||
"ioredis": "^5.0.6", | ||
"husky": "^8.0.2", | ||
"ioredis": "^5.2.4", | ||
"ioredis-mock": "^8.2.2", | ||
"koa": "^2.13.4", | ||
"lint-staged": "^13.0.0", | ||
"lint-staged": "^13.0.3", | ||
"nyc": "^15.1.0", | ||
"remark-cli": "^10.0.1", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-github": "^4.0.4", | ||
"smtp-server": "^3.11.0", | ||
"xo": "^0.49.0" | ||
"xo": "^0.52.4" | ||
}, | ||
@@ -45,0 +45,0 @@ "engines": { |
@@ -85,3 +85,13 @@ # [**@ladjs/graceful**](https://github.com/ladjs/graceful) | ||
// options to pass to `lil-http-terminator` to override defaults | ||
lilHttpTerminator: {} | ||
lilHttpTerminator: {}, | ||
// | ||
// appends a `true` boolean value to a property of this name in the logger meta object | ||
// (this is useful for Cabin/Axe as it will prevent a log from being created in MongoDB) | ||
// (and instead of having a DB log created upon graceful exit, it will simply log to console) | ||
// (defer to the Forward Email codebase, specifically the logger helper) | ||
// | ||
// NOTE: if you set this to `false` then this will be ignored and no meta property will be populated | ||
// | ||
ignoreHook: 'ignore_hook' | ||
}); | ||
@@ -161,12 +171,13 @@ | ||
| Property | Type | Default Value | Description | | ||
| ------------------- | ------ | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `servers` | Array | `[]` | An array of HTTP or NET servers to gracefully close on exit | | ||
| `brees` | Array | `[]` | An array of [Bree][] instances to gracefully exit | | ||
| `redisClients` | Array | `[]` | An array of Redis client instances to gracefully exit | | ||
| `mongooses` | Array | `[]` | An array of Mongoose connections to gracefully exit | | ||
| `customHandlers` | Array | `[]` | An array of functions (custom handlers) to invoke upon graceful exit | | ||
| `logger` | Object | `console` | This is the default logger. **We recommend using [Cabin][cabin]** instead of using `console` as your default logger. Set this value to `false` to disable logging entirely (uses noop function) | | ||
| `timeoutMs` | Number | `5000` | A number in milliseconds for how long to wait to gracefully exit | | ||
| `lilHttpTerminator` | Object | `{}` | An object of options to pass to `lil-http-terminator` to override default options provided | | ||
| Property | Type | Default Value | Description | | ||
| ------------------- | ------------------------- | --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `servers` | Array | `[]` | An array of HTTP or NET servers to gracefully close on exit | | ||
| `brees` | Array | `[]` | An array of [Bree][] instances to gracefully exit | | ||
| `redisClients` | Array | `[]` | An array of Redis client instances to gracefully exit | | ||
| `mongooses` | Array | `[]` | An array of Mongoose connections to gracefully exit | | ||
| `customHandlers` | Array | `[]` | An array of functions (custom handlers) to invoke upon graceful exit | | ||
| `logger` | Object | `console` | This is the default logger. **We recommend using [Cabin][cabin]** instead of using `console` as your default logger. Set this value to `false` to disable logging entirely (uses noop function) | | ||
| `timeoutMs` | Number | `5000` | A number in milliseconds for how long to wait to gracefully exit | | ||
| `lilHttpTerminator` | Object | `{}` | An object of options to pass to `lil-http-terminator` to override default options provided | | ||
| `ignoreHook` | String or `false` Boolean | `"ignore_hook"` | Appends a `true` boolean property to a property with this value in logs, e.g. `console.log('graceful exiting', { ignore_hook: true });` which is useful for preventing logs from being written to a database in hooks (this is meant for usage with [Cabin][] and [Axe][] and made for [Forward Email][forward-email]). If you pass a `false` value then this property will not get populated. | | ||
@@ -219,1 +230,5 @@ | ||
[bree]: https://jobscheduler.net | ||
[axe]: https://github.com/cabinjs/axe | ||
[forward-email]: https://github.com/forwardemail |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24589
288
232