Skip to content

Commit 62d5599

Browse files
authored
Add .env conf to graceful termination strategy suggested by templates (#537)
* Add closeWithGrace env config * Add closeWithGrace env config * feat: add support for close-grace-delay conf * feat: add support for close-grace-delay parameter
1 parent 917cff2 commit 62d5599

File tree

8 files changed

+28
-7
lines changed

8 files changed

+28
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ You can pass the following options via CLI arguments. You can also use `--config
165165
| Set the prefix | `-x` | `--prefix` | `FASTIFY_PREFIX` |
166166
| Set the plugin timeout | `-T` | `--plugin-timeout` | `FASTIFY_PLUGIN_TIMEOUT` |
167167
| Defines the maximum payload, in bytes,<br>that the server is allowed to accept | | `--body-limit` | `FASTIFY_BODY_LIMIT` |
168+
| 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` |
168169

169170
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.
170171

@@ -212,7 +213,7 @@ const appService = require('./app.js')
212213
app.register(appService)
213214

214215
// delay is the number of milliseconds for the graceful close to finish
215-
const closeListeners = closeWithGrace({ delay: 500 }, async function ({ signal, err, manual }) {
216+
const closeListeners = closeWithGrace({ delay: process.env.FASTIFY_CLOSE_GRACE_DELAY || 500 }, async function ({ signal, err, manual }) {
216217
if (err) {
217218
app.log.error(err)
218219
}

args.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const DEFAULT_ARGUMENTS = {
1515
debugPort: 9320,
1616
options: false,
1717
pluginTimeout: 10 * 1000, // everything should load in 10 seconds
18+
closeGraceDelay: 500,
1819
lang: 'js',
1920
standardlint: false
2021
}
@@ -25,7 +26,7 @@ module.exports = function parseArgs (args) {
2526
configuration: {
2627
'populate--': true
2728
},
28-
number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout'],
29+
number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout', 'close-grace-delay'],
2930
string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module', 'debug-host', 'lang', 'require', 'config'],
3031
boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug', 'standardlint'],
3132
envPrefix: 'FASTIFY_',
@@ -44,6 +45,7 @@ module.exports = function parseArgs (args) {
4445
'log-level': ['l'],
4546
'pretty-logs': ['P'],
4647
'plugin-timeout': ['T'],
48+
'close-grace-delay': ['g'],
4749
'logging-module': ['L'],
4850
'verbose-watch': ['V']
4951
}
@@ -69,6 +71,7 @@ module.exports = function parseArgs (args) {
6971
port: parsedArgs.port,
7072
bodyLimit: parsedArgs.bodyLimit,
7173
pluginTimeout: parsedArgs.pluginTimeout,
74+
closeGraceDelay: parsedArgs.closeGraceDelay,
7275
pluginOptions,
7376
prettyLogs: parsedArgs.prettyLogs,
7477
options: parsedArgs.options,

help/start.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ OPTS
4949
-T, --plugin-timeout
5050
The maximum amount of time that a plugin can take to load (default to 10 seconds).
5151

52+
-g, --close-grace-delay
53+
The maximum amount of time before forcefully closing pending requests (default to 500 ms)
54+
5255
-h, --help
5356
Show this help message
5457

start.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ async function runFastify (args, additionalOptions) {
137137
const appConfig = Object.assign({}, opts.pluginOptions, additionalOptions)
138138
await fastify.register(file.default || file, appConfig)
139139

140-
const closeListeners = closeWithGrace({ delay: 500 }, async function ({ signal, err, manual }) {
140+
const closeListeners = closeWithGrace({ delay: opts.closeGraceDelay }, async function ({ signal, err, manual }) {
141141
if (err) {
142142
fastify.log.error(err)
143143
}

templates/eject-ts/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const app = Fastify({
1717
app.register(import("./app"));
1818

1919
// delay is the number of milliseconds for the graceful close to finish
20-
const closeListeners = closeWithGrace({ delay: 500 }, async function ({ signal, err, manual }) {
20+
const closeListeners = closeWithGrace({ delay: parseInt(process.env.FASTIFY_CLOSE_GRACE_DELAY) || 500 }, async function ({ signal, err, manual }) {
2121
if (err) {
2222
app.log.error(err)
2323
}

templates/eject/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const appService = require('./app.js')
1919
app.register(appService)
2020

2121
// delay is the number of milliseconds for the graceful close to finish
22-
const closeListeners = closeWithGrace({ delay: 500 }, async function ({ signal, err, manual }) {
22+
const closeListeners = closeWithGrace({ delay: process.env.FASTIFY_CLOSE_GRACE_DELAY || 500 }, async function ({ signal, err, manual }) {
2323
if (err) {
2424
app.log.error(err)
2525
}

test/args.test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ test('should parse args correctly', t => {
1818
'--options', 'true',
1919
'--prefix', 'FASTIFY_',
2020
'--plugin-timeout', '500',
21+
'--close-grace-delay', '30000',
2122
'--body-limit', '5242880',
2223
'--debug', 'true',
2324
'--debug-port', 1111,
@@ -42,6 +43,7 @@ test('should parse args correctly', t => {
4243
logLevel: 'info',
4344
prefix: 'FASTIFY_',
4445
pluginTimeout: 500,
46+
closeGraceDelay: 30000,
4547
pluginOptions: {},
4648
bodyLimit: 5242880,
4749
debug: true,
@@ -68,6 +70,7 @@ test('should parse args with = assignment correctly', t => {
6870
'--options=true',
6971
'--prefix=FASTIFY_',
7072
'--plugin-timeout=500',
73+
'--close-grace-delay=30000',
7174
'--body-limit=5242880',
7275
'--debug=true',
7376
'--debug-port', 1111,
@@ -92,6 +95,7 @@ test('should parse args with = assignment correctly', t => {
9295
logLevel: 'info',
9396
prefix: 'FASTIFY_',
9497
pluginTimeout: 500,
98+
closeGraceDelay: 30000,
9599
pluginOptions: {},
96100
bodyLimit: 5242880,
97101
debug: true,
@@ -118,6 +122,7 @@ test('should parse env vars correctly', t => {
118122
process.env.FASTIFY_PREFIX = 'FASTIFY_'
119123
process.env.FASTIFY_BODY_LIMIT = '5242880'
120124
process.env.FASTIFY_PLUGIN_TIMEOUT = '500'
125+
process.env.FASTIFY_CLOSE_GRACE_DELAY = '30000'
121126
process.env.FASTIFY_DEBUG = 'true'
122127
process.env.FASTIFY_DEBUG_PORT = '1111'
123128
process.env.FASTIFY_DEBUG_HOST = '1.1.1.1'
@@ -137,6 +142,7 @@ test('should parse env vars correctly', t => {
137142
delete process.env.FASTIFY_PREFIX
138143
delete process.env.FASTIFY_BODY_LIMIT
139144
delete process.env.FASTIFY_PLUGIN_TIMEOUT
145+
delete process.env.FASTIFY_CLOSE_GRACE_DELAY
140146
delete process.env.FASTIFY_DEBUG
141147
delete process.env.FASTIFY_DEBUG_PORT
142148
delete process.env.FASTIFY_LOGGING_MODULE
@@ -160,6 +166,7 @@ test('should parse env vars correctly', t => {
160166
socket: 'fastify.io.socket:9999',
161167
require: './require-module.js',
162168
pluginTimeout: 500,
169+
closeGraceDelay: 30000,
163170
pluginOptions: {},
164171
debug: true,
165172
debugPort: 1111,
@@ -170,7 +177,7 @@ test('should parse env vars correctly', t => {
170177
})
171178

172179
test('should respect default values', t => {
173-
t.plan(12)
180+
t.plan(13)
174181

175182
const argv = [
176183
'app.js'
@@ -186,6 +193,7 @@ test('should respect default values', t => {
186193
t.equal(parsedArgs.verboseWatch, false)
187194
t.equal(parsedArgs.logLevel, 'fatal')
188195
t.equal(parsedArgs.pluginTimeout, 10000)
196+
t.equal(parsedArgs.closeGraceDelay, 500)
189197
t.equal(parsedArgs.debug, false)
190198
t.equal(parsedArgs.debugPort, 9320)
191199
t.equal(parsedArgs.loggingModule, undefined)
@@ -208,6 +216,7 @@ test('should parse custom plugin options', t => {
208216
'--options', 'true',
209217
'--prefix', 'FASTIFY_',
210218
'--plugin-timeout', '500',
219+
'--close-grace-delay', '30000',
211220
'--body-limit', '5242880',
212221
'--debug', 'true',
213222
'--debug-port', 1111,
@@ -239,6 +248,7 @@ test('should parse custom plugin options', t => {
239248
logLevel: 'info',
240249
prefix: 'FASTIFY_',
241250
pluginTimeout: 500,
251+
closeGraceDelay: 30000,
242252
pluginOptions: {
243253
a: true,
244254
b: true,
@@ -269,6 +279,7 @@ test('should parse config file correctly and prefer config values over default o
269279
port: 5000,
270280
bodyLimit: undefined,
271281
pluginTimeout: 9000,
282+
closeGraceDelay: 1000,
272283
pluginOptions: {},
273284
prettyLogs: true,
274285
options: false,
@@ -296,6 +307,7 @@ test('should prefer command line args over config file options', t => {
296307
'--port', '4000',
297308
'--debugPort', '9320',
298309
'--plugin-timeout', '10000',
310+
'--close-grace-delay', '30000',
299311
'app.js'
300312
]
301313
const parsedArgs = parseArgs(argv)
@@ -306,6 +318,7 @@ test('should prefer command line args over config file options', t => {
306318
port: 4000,
307319
bodyLimit: undefined,
308320
pluginTimeout: 10000,
321+
closeGraceDelay: 30000,
309322
pluginOptions: {},
310323
prettyLogs: true,
311324
options: false,

test/data/custom-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module.exports = {
55
watch: true,
66
prettyLogs: true,
77
debugPort: 4000,
8-
pluginTimeout: 9 * 1000
8+
pluginTimeout: 9 * 1000,
9+
closeGraceDelay: 1000
910
}

0 commit comments

Comments
 (0)