Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
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
Support graceful shutdown after receiving SIGTERM signal
  • Loading branch information
banksalad-seongwoo committed Oct 16, 2021
commit e9d5b8df7059221d9b66a162ecd0bfaccba6880f
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