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

Support graceful shutdowns #36909

Merged
merged 8 commits into from May 16, 2022
23 changes: 23 additions & 0 deletions docs/deployment.md
Expand Up @@ -98,6 +98,29 @@ Next.js will automatically load the latest version of your application in the ba

**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.
ijjk marked this conversation as resolved.
Show resolved Hide resolved

## 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

For more information on what to do next, we recommend the following sections:
Expand Down
7 changes: 5 additions & 2 deletions packages/next/bin/next.ts
Expand Up @@ -129,8 +129,11 @@ if (process.versions.pnp === '3') {
}

// Make sure commands gracefully respect termination signals (e.g. from Docker)
process.on('SIGTERM', () => process.exit(0))
process.on('SIGINT', () => process.exit(0))
// Allow the graceful termination to be manually configurable
if (!process.env.NEXT_MANUAL_SIG_HANDLE) {
process.on('SIGTERM', () => process.exit(0))
process.on('SIGINT', () => process.exit(0))
}

commands[command]()
.then((exec) => exec(forwardedArgs))
Expand Down