feat(medusa,medusa-core-utils): graceful shutdown server (#3408)

* feat: graceful shutdown
This commit is contained in:
Carlos R. L. Rodrigues
2023-03-10 09:11:45 -03:00
committed by GitHub
parent 9ba09ba4d7
commit 54dcc1871c
8 changed files with 339 additions and 927 deletions
+26 -7
View File
@@ -2,6 +2,7 @@ import "core-js/stable"
import "regenerator-runtime/runtime"
import express from "express"
import { GracefulShutdownServer } from "medusa-core-utils"
import { track } from "medusa-telemetry"
import { scheduleJob } from "node-schedule"
@@ -19,13 +20,31 @@ export default async function ({ port, directory }) {
const { dbConnection } = await loaders({ directory, expressApp: app })
const serverActivity = Logger.activity(`Creating server`)
const server = app.listen(port, (err) => {
if (err) {
return
}
Logger.success(serverActivity, `Server is ready on port: ${port}`)
track("CLI_START_COMPLETED")
})
const server = GracefulShutdownServer.create(
app.listen(port, (err) => {
if (err) {
return
}
Logger.success(serverActivity, `Server is ready on port: ${port}`)
track("CLI_START_COMPLETED")
})
)
// Handle graceful shutdown
const gracefulShutDown = () => {
server
.shutdown()
.then(() => {
Logger.info("Gracefully stopping the server.")
process.exit(0)
})
.catch((e) => {
Logger.error("Error received when shutting down the server.", e)
process.exit(1)
})
}
process.on("SIGTERM", gracefulShutDown)
process.on("SIGINT", gracefulShutDown)
scheduleJob(CRON_SCHEDULE, () => {
track("PING")