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 all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ You can pass the following options via CLI arguments. You can also use `--config
| 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` |

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
}