Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass forceCloseConnections option from cli #765

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,28 +149,29 @@ If your `package.json` does not have `"type": "module"`, use `.mjs` for the exte
#### Options
You can pass the following options via CLI arguments. You can also use `--config` or `-c` flag to pass a configuration file that exports all the properties listed below in camelCase convention. In case of collision (i.e., An argument existing in both the configuration file and as a command-line argument, the command-line argument is given the priority). Every option has a corresponding environment variable:

| Description | Short command | Full command | Environment variable |
| --------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ------------------ | ------------------------ |
| Path to configuration file that can be used to manage the options listed below | `-c` | `--config` | `FASTIFY_CONFIG or CONFIG` |
| Port to listen on (default to 3000) | `-p` | `--port` | `FASTIFY_PORT or PORT` |
| Address to listen on | `-a` | `--address` | `FASTIFY_ADDRESS` |
| Socket to listen on | `-s` | `--socket` | `FASTIFY_SOCKET` |
| Module to preload | `-r` | `--require` | `FASTIFY_REQUIRE` |
| ES Module to preload | `-i` | `--import` | `FASTIFY_IMPORT` |
| Log level (default to fatal) | `-l` | `--log-level` | `FASTIFY_LOG_LEVEL` |
| Path to logging configuration module to use | `-L` | `--logging-module` | `FASTIFY_LOGGING_MODULE` |
| Start Fastify app in debug mode with nodejs inspector | `-d` | `--debug` | `FASTIFY_DEBUG` |
| Set the inspector port (default: 9320) | `-I` | `--debug-port` | `FASTIFY_DEBUG_PORT` |
| Set the inspector host to listen on (default: loopback address or `0.0.0.0` inside Docker or Kubernetes) | | `--debug-host` | `FASTIFY_DEBUG_HOST` |
| Prints pretty logs | `-P` | `--pretty-logs` | `FASTIFY_PRETTY_LOGS` |
| Watch process.cwd() directory for changes, recursively; when that happens, the process will auto reload | `-w` | `--watch` | `FASTIFY_WATCH` |
| Ignore changes to the specified files or directories when watch is enabled. (e.g. `--ignore-watch='node_modules .git logs/error.log'` ) | | `--ignore-watch` | `FASTIFY_IGNORE_WATCH` |
| Prints events triggered by watch listener (useful to debug unexpected reload when using `--watch` ) | `-V` | `--verbose-watch` | `FASTIFY_VERBOSE_WATCH` |
| Use custom options | `-o` | `--options` | `FASTIFY_OPTIONS` |
| Set the prefix | `-x` | `--prefix` | `FASTIFY_PREFIX` |
| Set the plugin timeout | `-T` | `--plugin-timeout` | `FASTIFY_PLUGIN_TIMEOUT` |
| Defines the maximum payload, in bytes,<br>that the server is allowed to accept | | `--body-limit` | `FASTIFY_BODY_LIMIT` |
| Set the maximum ms delay before forcefully closing pending requests after receiving SIGTERM or SIGINT signals; and uncaughtException or unhandledRejection errors (default: 500) | `-g` | `--close-grace-delay` | `FASTIFY_CLOSE_GRACE_DELAY` |
| Description | Short command | Full command | Environment variable |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|-----------------------------|-----------------------------------|
| Path to configuration file that can be used to manage the options listed below | `-c` | `--config` | `FASTIFY_CONFIG or CONFIG` |
| Port to listen on (default to 3000) | `-p` | `--port` | `FASTIFY_PORT or PORT` |
| Address to listen on | `-a` | `--address` | `FASTIFY_ADDRESS` |
| Socket to listen on | `-s` | `--socket` | `FASTIFY_SOCKET` |
| Module to preload | `-r` | `--require` | `FASTIFY_REQUIRE` |
| ES Module to preload | `-i` | `--import` | `FASTIFY_IMPORT` |
| Log level (default to fatal) | `-l` | `--log-level` | `FASTIFY_LOG_LEVEL` |
| Path to logging configuration module to use | `-L` | `--logging-module` | `FASTIFY_LOGGING_MODULE` |
| Start Fastify app in debug mode with nodejs inspector | `-d` | `--debug` | `FASTIFY_DEBUG` |
| Set the inspector port (default: 9320) | `-I` | `--debug-port` | `FASTIFY_DEBUG_PORT` |
| Set the inspector host to listen on (default: loopback address or `0.0.0.0` inside Docker or Kubernetes) | | `--debug-host` | `FASTIFY_DEBUG_HOST` |
| Prints pretty logs | `-P` | `--pretty-logs` | `FASTIFY_PRETTY_LOGS` |
| Watch process.cwd() directory for changes, recursively; when that happens, the process will auto reload | `-w` | `--watch` | `FASTIFY_WATCH` |
| Ignore changes to the specified files or directories when watch is enabled. (e.g. `--ignore-watch='node_modules .git logs/error.log'` ) | | `--ignore-watch` | `FASTIFY_IGNORE_WATCH` |
| Prints events triggered by watch listener (useful to debug unexpected reload when using `--watch` ) | `-V` | `--verbose-watch` | `FASTIFY_VERBOSE_WATCH` |
| Use custom options | `-o` | `--options` | `FASTIFY_OPTIONS` |
| Set the prefix | `-x` | `--prefix` | `FASTIFY_PREFIX` |
| Set the plugin timeout | `-T` | `--plugin-timeout` | `FASTIFY_PLUGIN_TIMEOUT` |
| Set forceCloseConnections option on fastify instance | `-f` | `--force-close-connections` | `FASTIFY_FORCE_CLOSE_CONNECTIONS` |
| Defines the maximum payload, in bytes,<br>that the server is allowed to accept | | `--body-limit` | `FASTIFY_BODY_LIMIT` |
| Set the maximum ms delay before forcefully closing pending requests after receiving SIGTERM or SIGINT signals; and uncaughtException or unhandledRejection errors (default: 500) | `-g` | `--close-grace-delay` | `FASTIFY_CLOSE_GRACE_DELAY` |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you reformat the whole table? Can you only make the change needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ops, I believed was some linter. Instead was my IDE. Restored the untouched lines


By default `fastify-cli` runs [`dotenv`](https://www.npmjs.com/package/dotenv), so it will load all the env variables stored in `.env` in your current working directory.

Expand Down
4 changes: 3 additions & 1 deletion args.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = function parseArgs (args) {
},
number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout', 'close-grace-delay'],
string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module', 'debug-host', 'lang', 'require', 'import', 'config', 'method'],
boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug', 'standardlint', 'common-prefix', 'include-hooks'],
boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug', 'standardlint', 'common-prefix', 'include-hooks', 'force-close-connections'],
envPrefix: 'FASTIFY_',
alias: {
port: ['p'],
Expand All @@ -47,6 +47,7 @@ module.exports = function parseArgs (args) {
'log-level': ['l'],
'pretty-logs': ['P'],
'plugin-timeout': ['T'],
'force-close-connections': ['f'],
'close-grace-delay': ['g'],
'logging-module': ['L'],
'verbose-watch': ['V']
Expand All @@ -73,6 +74,7 @@ module.exports = function parseArgs (args) {
port: parsedArgs.port,
bodyLimit: parsedArgs.bodyLimit,
pluginTimeout: parsedArgs.pluginTimeout,
forceCloseConnections: parsedArgs.forceCloseConnections,
closeGraceDelay: parsedArgs.closeGraceDelay,
pluginOptions,
prettyLogs: parsedArgs.prettyLogs,
Expand Down
4 changes: 4 additions & 0 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ async function runFastify (args, additionalOptions, serverOptions) {
options.bodyLimit = opts.bodyLimit
}

if (opts.forceCloseConnections) {
options.forceCloseConnections = opts.forceCloseConnections
}

if (opts.prettyLogs) {
options.logger.transport = {
target: 'pino-pretty'
Expand Down
15 changes: 14 additions & 1 deletion test/args.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test('should parse args correctly', t => {
'--options', 'true',
'--prefix', 'FASTIFY_',
'--plugin-timeout', '500',
'--force-close-connections', 'true',
'--close-grace-delay', '30000',
'--body-limit', '5242880',
'--debug', 'true',
Expand All @@ -47,6 +48,7 @@ test('should parse args correctly', t => {
logLevel: 'info',
prefix: 'FASTIFY_',
pluginTimeout: 500,
forceCloseConnections: true,
closeGraceDelay: 30000,
pluginOptions: {},
bodyLimit: 5242880,
Expand Down Expand Up @@ -78,6 +80,7 @@ test('should parse args with = assignment correctly', t => {
'--options=true',
'--prefix=FASTIFY_',
'--plugin-timeout=500',
'--force-close-connections=true',
'--close-grace-delay=30000',
'--body-limit=5242880',
'--debug=true',
Expand All @@ -104,6 +107,7 @@ test('should parse args with = assignment correctly', t => {
logLevel: 'info',
prefix: 'FASTIFY_',
pluginTimeout: 500,
forceCloseConnections: true,
closeGraceDelay: 30000,
pluginOptions: {},
bodyLimit: 5242880,
Expand Down Expand Up @@ -135,6 +139,7 @@ test('should parse env vars correctly', t => {
process.env.FASTIFY_PREFIX = 'FASTIFY_'
process.env.FASTIFY_BODY_LIMIT = '5242880'
process.env.FASTIFY_PLUGIN_TIMEOUT = '500'
process.env.FASTIFY_FORCE_CLOSE_CONNECTIONS = 'true'
process.env.FASTIFY_CLOSE_GRACE_DELAY = '30000'
process.env.FASTIFY_DEBUG = 'true'
process.env.FASTIFY_DEBUG_PORT = '1111'
Expand All @@ -156,6 +161,7 @@ test('should parse env vars correctly', t => {
delete process.env.FASTIFY_PREFIX
delete process.env.FASTIFY_BODY_LIMIT
delete process.env.FASTIFY_PLUGIN_TIMEOUT
delete process.env.FASTIFY_FORCE_CLOSE_CONNECTIONS
delete process.env.FASTIFY_CLOSE_GRACE_DELAY
delete process.env.FASTIFY_DEBUG
delete process.env.FASTIFY_DEBUG_PORT
Expand All @@ -181,6 +187,7 @@ test('should parse env vars correctly', t => {
require: './require-module.js',
import: './import-module.js',
pluginTimeout: 500,
forceCloseConnections: true,
closeGraceDelay: 30000,
pluginOptions: {},
debug: true,
Expand All @@ -195,7 +202,7 @@ test('should parse env vars correctly', t => {
})

test('should respect default values', t => {
t.plan(14)
t.plan(15)

const argv = [
'app.js'
Expand All @@ -211,6 +218,7 @@ test('should respect default values', t => {
t.equal(parsedArgs.verboseWatch, false)
t.equal(parsedArgs.logLevel, 'fatal')
t.equal(parsedArgs.pluginTimeout, 10000)
t.equal(parsedArgs.forceCloseConnections, undefined)
t.equal(parsedArgs.closeGraceDelay, 500)
t.equal(parsedArgs.debug, false)
t.equal(parsedArgs.debugPort, 9320)
Expand All @@ -236,6 +244,7 @@ test('should parse custom plugin options', t => {
'--options', 'true',
'--prefix', 'FASTIFY_',
'--plugin-timeout', '500',
'--force-close-connections', 'true',
'--close-grace-delay', '30000',
'--body-limit', '5242880',
'--debug', 'true',
Expand Down Expand Up @@ -269,6 +278,7 @@ test('should parse custom plugin options', t => {
logLevel: 'info',
prefix: 'FASTIFY_',
pluginTimeout: 500,
forceCloseConnections: true,
closeGraceDelay: 30000,
pluginOptions: {
a: true,
Expand Down Expand Up @@ -303,6 +313,7 @@ test('should parse config file correctly and prefer config values over default o
port: 5000,
bodyLimit: undefined,
pluginTimeout: 9000,
forceCloseConnections: true,
closeGraceDelay: 1000,
pluginOptions: {},
prettyLogs: true,
Expand Down Expand Up @@ -335,6 +346,7 @@ test('should prefer command line args over config file options', t => {
'--port', '4000',
'--debugPort', '9320',
'--plugin-timeout', '10000',
'--force-close-connections', 'false',
'--close-grace-delay', '30000',
'app.js'
]
Expand All @@ -346,6 +358,7 @@ test('should prefer command line args over config file options', t => {
port: 4000,
bodyLimit: undefined,
pluginTimeout: 10000,
forceCloseConnections: false,
closeGraceDelay: 30000,
pluginOptions: {},
prettyLogs: true,
Expand Down
1 change: 1 addition & 0 deletions test/data/custom-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ module.exports = {
prettyLogs: true,
debugPort: 4000,
pluginTimeout: 9 * 1000,
forceCloseConnections: true,
closeGraceDelay: 1000
}