Skip to content

Commit

Permalink
Merge pull request yjs#18 from beorn/feat/graceful-shutdown
Browse files Browse the repository at this point in the history
feat: graceful shutdown
  • Loading branch information
dmonad authored Apr 25, 2024
2 parents 97ae086 + 872f651 commit 69b3fea
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
12 changes: 11 additions & 1 deletion bin/auth-server-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ app.get('/auth/perm/:room/:userid', async (res, req) => {
* Resolves when the server started.
*/
export const authServerStarted = promise.create((resolve, reject) => {
app.listen(port, (token) => {
const server = app.listen(port, (token) => {
if (token) {
logging.print(logging.GREEN, `[${appName}] Listening to port ${port}`)
resolve()
Expand All @@ -86,4 +86,14 @@ export const authServerStarted = promise.create((resolve, reject) => {
throw err
}
})

// Gracefully shut down the server when running in Docker
process.on("SIGTERM", shutDown)
process.on("SIGINT", shutDown)

function shutDown() {
console.log("Received SIGTERM/SIGINT - shutting down")
server.close()
process.exit(0)
}
})
18 changes: 17 additions & 1 deletion demos/auth-express/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ app.get('/auth/perm/:room/:userid', async (req, res) => {
// serve static files
app.use(express.static('./'))

app.listen(port, () => {
const server = app.listen(port, () => {
console.log(`Express Demo Auth server listening on port ${port}`)
})

// Gracefully shut down the server when running in Docker
process.on("SIGTERM", shutDown)
process.on("SIGINT", shutDown)

function shutDown() {
console.log("Received SIGTERM/SIGINT - shutting down gracefully")
server.close(() => {
console.log("Closed out remaining connections - shutting down")
process.exit(0)
})
setTimeout(() => {
console.error("Couldn't close connections - forcefully shutting down")
process.exit(1)
}, 10000)
}
18 changes: 17 additions & 1 deletion demos/blocksuite/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ app.get('*', (req, res) => {
res.sendFile(resolve(__dirname, 'index.html'))
})

app.listen(port, () => {
const server = app.listen(port, () => {
console.log(`Express Demo BlockSuite server listening on port ${port}`)
})

// Gracefully shut down the server when running in Docker
process.on("SIGTERM", shutDown)
process.on("SIGINT", shutDown)

function shutDown() {
console.log("Received SIGTERM/SIGINT - shutting down gracefully")
server.close(() => {
console.log("Closed out remaining connections - shutting down")
process.exit(0)
})
setTimeout(() => {
console.error("Couldn't close connections - forcefully shutting down")
process.exit(1)
}, 10000)
}

0 comments on commit 69b3fea

Please sign in to comment.