본문 바로가기
Q&A2025년 1월 26일1분 읽기

Node.js에서 graceful shutdown 구현

홍
홍재원
조회 573댓글 2

Node.js 서버를 종료할 때 진행 중인 요청을 안전하게 처리하는 방법을 알려주세요.

댓글 2

김김영삼1년 전

Graceful Shutdown:

const server = app.listen(3000)

function shutdown(signal) {
console.log(`${signal} received. Shutting down...`)
server.close(() => {
console.log("HTTP server closed")
// DB 연결 종료
prisma.$disconnect()
process.exit(0)
})
// 10초 후 강제 종료
setTimeout(() => {
console.error("Forced shutdown")
process.exit(1)
}, 10000)
}

process.on("SIGTERM", () => shutdown("SIGTERM"))
process.on("SIGINT", () => shutdown("SIGINT"))

server.close()는 새 연결을 받지 않고 기존 연결이 완료될 때까지 기다립니다.

전전민규(guest)1년 전
자세한 설명 감사합니다. 바로 적용해봐야겠네요.
Ctrl+Enter로 등록