From f8de511d5803f44a21582bec929e4aa0e03b0a07 Mon Sep 17 00:00:00 2001 From: Sidharth Rathi Date: Sat, 14 May 2022 17:09:08 +0530 Subject: [PATCH 1/5] feat(next): Support graceful shutdowns --- packages/next/bin/next.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/next/bin/next.ts b/packages/next/bin/next.ts index ad8597d91797..f88665fe8d1b 100755 --- a/packages/next/bin/next.ts +++ b/packages/next/bin/next.ts @@ -129,7 +129,16 @@ if (process.versions.pnp === '3') { } // Make sure commands gracefully respect termination signals (e.g. from Docker) -process.on('SIGTERM', () => process.exit(0)) +// Allow the graceful termination to be manually configurable +if (!process.env.NEXT_MANUAL_SIGTERM) { + process.on('SIGTERM', () => + setTimeout( + () => process.exit(0), + parseInt(process.env.NEXT_GRACEFUL_SHUTDOWN_TIMEOUT_MS || '0', 10) + ) + ) +} + process.on('SIGINT', () => process.exit(0)) commands[command]() From 02ae1739af2050df76dcd58a3cda3f249ba68f72 Mon Sep 17 00:00:00 2001 From: Sidharth Rathi Date: Sun, 15 May 2022 21:19:13 +0530 Subject: [PATCH 2/5] fix(next): remove graceful shutdown timeout --- packages/next/bin/next.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/next/bin/next.ts b/packages/next/bin/next.ts index f88665fe8d1b..f72cf295f8d1 100755 --- a/packages/next/bin/next.ts +++ b/packages/next/bin/next.ts @@ -131,12 +131,7 @@ if (process.versions.pnp === '3') { // Make sure commands gracefully respect termination signals (e.g. from Docker) // Allow the graceful termination to be manually configurable if (!process.env.NEXT_MANUAL_SIGTERM) { - process.on('SIGTERM', () => - setTimeout( - () => process.exit(0), - parseInt(process.env.NEXT_GRACEFUL_SHUTDOWN_TIMEOUT_MS || '0', 10) - ) - ) + process.on('SIGTERM', () => process.exit(0)) } process.on('SIGINT', () => process.exit(0)) From e3b7b23e0c33088c7ee868eabbbb0694a8fb4d18 Mon Sep 17 00:00:00 2001 From: Sidharth Rathi Date: Sun, 15 May 2022 22:41:21 +0530 Subject: [PATCH 3/5] fix(next): register SIGINT handler conditionally --- packages/next/bin/next.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/next/bin/next.ts b/packages/next/bin/next.ts index f72cf295f8d1..6c4575ff4c5f 100755 --- a/packages/next/bin/next.ts +++ b/packages/next/bin/next.ts @@ -130,12 +130,11 @@ if (process.versions.pnp === '3') { // Make sure commands gracefully respect termination signals (e.g. from Docker) // Allow the graceful termination to be manually configurable -if (!process.env.NEXT_MANUAL_SIGTERM) { +if (!process.env.NEXT_MANUAL_SIG_HANDLE) { process.on('SIGTERM', () => process.exit(0)) + process.on('SIGINT', () => process.exit(0)) } -process.on('SIGINT', () => process.exit(0)) - commands[command]() .then((exec) => exec(forwardedArgs)) .then(() => { From 8f0540d6f81c5b59d3f79bd8c68cf61d5b72cbc2 Mon Sep 17 00:00:00 2001 From: Sidharth Rathi Date: Sun, 15 May 2022 22:42:17 +0530 Subject: [PATCH 4/5] docs(next): Add section for graceful shutdowns --- docs/deployment.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/deployment.md b/docs/deployment.md index 0049dff3e642..e5c9a246130e 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -96,7 +96,28 @@ When you deploy your Next.js application, you want to see the latest version wit Next.js will automatically load the latest version of your application in the background when routing. For client-side navigations, `next/link` will temporarily function as a normal `` tag. -**Note:** If a new page (with an old version) has already been prefetched by `next/link`, Next.js will use the old version. Navigating to a page that has _not_ been prefetched (and is not cached at the CDN level) will load the latest version. +## Manual Graceful shutdowns + +Sometimes you might want to run some cleanup code on process signals like `SIGTERM` or `SIGINT`. + +You can do that by setting the env variable `NEXT_MANUAL_SIG_HANDLE` to `true` and then register a handler for that signal inside your `_document.js` file. + +```js +// pages/_document.js + +if (process.env.NEXT_MANUAL_SIG_HANDLE) { + // this should be added in your custom _document + process.on('SIGTERM', () => { + console.log('Received SIGTERM: ', 'cleaning up') + process.exit(0) + }) + + process.on('SIGINT', () => { + console.log('Received SIGINT: ', 'cleaning up') + process.exit(0) + }) +} +``` ## Related From 66ba59fc3b4aa12f35586e7c059a3f0ec2cfeb4f Mon Sep 17 00:00:00 2001 From: Sidharth Rathi Date: Mon, 16 May 2022 03:52:25 +0530 Subject: [PATCH 5/5] fix(docs): undo note section removal --- docs/deployment.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/deployment.md b/docs/deployment.md index e5c9a246130e..e8df934f30ff 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -96,6 +96,8 @@ When you deploy your Next.js application, you want to see the latest version wit Next.js will automatically load the latest version of your application in the background when routing. For client-side navigations, `next/link` will temporarily function as a normal `` tag. +**Note:** If a new page (with an old version) has already been prefetched by `next/link`, Next.js will use the old version. Navigating to a page that has _not_ been prefetched (and is not cached at the CDN level) will load the latest version. + ## Manual Graceful shutdowns Sometimes you might want to run some cleanup code on process signals like `SIGTERM` or `SIGINT`.