Skip to content

Commit

Permalink
add graceful shutdown for node18 functions
Browse files Browse the repository at this point in the history
Signed-off-by: Nikhil Sharma <nikhilsharma230303@gmail.com>
  • Loading branch information
NikhilSharmaWe committed May 26, 2023
1 parent f1bbf94 commit 13eba1c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions template/node18/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const express = require('express')
const app = express()
const handler = require('./function/handler');
const bodyParser = require('body-parser')
const bodyParser = require('body-parser');

const defaultMaxSize = '100kb' // body-parser default

Expand Down Expand Up @@ -137,8 +137,25 @@ app.options('/*', middleware);

const port = process.env.http_port || 3000;

app.listen(port, () => {
const server = app.listen(port, () => {
console.log(`node18 listening on port: ${port}`)
});

const writeTimeout = process.env.write_timeout || 10000;

process.on('SIGTERM', async () => {
console.log(`Function got SIGTERM event, draining up to: ${writeTimeout}ms`);
await gracefulShutdown();
})

async function gracefulShutdown() {
await new Promise((resolve) => {
setTimeout(resolve, writeTimeout);
})

server.close(() => {
console.log('Server gracefully shut down');
process.exit(0);
});
}

0 comments on commit 13eba1c

Please sign in to comment.