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 shutdown after receiving SIGTERM signal #29959

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/next/bin/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ if (process.env.NODE_ENV && !standardEnv.includes(process.env.NODE_ENV)) {
;(process.env as any).NODE_ENV = process.env.NODE_ENV || defaultEnv

// Make sure commands gracefully respect termination signals (e.g. from Docker)
process.on('SIGTERM', () => process.exit(0))
process.on('SIGTERM', () =>
Copy link
Member

@ijjk ijjk Feb 10, 2022

Choose a reason for hiding this comment

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

What do you think about these listeners being omitted when an environment variable is present and instead adding custom handling for this in _document?

This would allow more control over calling process.exit() instead of relying on an arbitrary time value. e.g.

if (!process.env.NEXT_MANUAL_SIGTERM) {
  process.on('SIGTERM', () => process.exit(0))
}
// pages/_document.js

if (process.env.NEXT_MANUAL_SIGTERM) {
  // this should be added in your custom _document 
  process.on('SIGTERM', () => {
    console.log('cleaning up')
    process.exit(0)
  })
}

Copy link

Choose a reason for hiding this comment

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

love this, we would like to overwrite this to close database connection ...

setTimeout(
() => process.exit(0),

Choose a reason for hiding this comment

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

Is there anyway that this could be something like:

() => {
  server.close();
  process.exit(0);
}

parseInt(process.env.NEXT_GRACEFUL_SHUTDOWN_TIMEOUT_MS || '0', 10)
)
)
process.on('SIGINT', () => process.exit(0))

commands[command]()
Expand Down