Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add graceful shutdown for node18 functions #306

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
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;

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

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

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