Skip to content

feat: enable graceful shutdown #75

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

Merged
merged 1 commit into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"debug": "^4.3.4",
"express": "^4.18.1",
"express-interceptor": "^1.2.0",
"http-terminator": "^3.2.0",
"lodash": "^4.17.21",
"minimist": "^1.2.6",
"on-finished": "^2.4.1",
Expand Down
42 changes: 36 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@

// Functions framework entry point that configures and starts Node.js server
// that runs user's code on HTTP request.
import {getUserFunction} from './loader';
import {ErrorHandler} from './invoker';
import {getServer} from './server';
import {parseOptions, helpText, OptionsError} from './options';
import {OpenFunction} from './functions';
import * as process from 'process';

import {createHttpTerminator} from 'http-terminator';

import getAysncServer from './openfunction/async_server';
import {
OpenFunctionContext,
ContextUtils,
} from './openfunction/function_context';

import {getUserFunction} from './loader';
import {ErrorHandler} from './invoker';
import {getServer} from './server';
import {parseOptions, helpText, OptionsError} from './options';
import {OpenFunction} from './functions';

/**
* Main entrypoint for the functions framework that loads the user's function
* and starts the HTTP server.
Expand All @@ -51,6 +56,8 @@ export const main = async () => {
}
const {userFunction, signatureType} = loadedFunction;

// Try to determine the server runtime
// Considering the async runtime in the first place
if (ContextUtils.IsAsyncRuntime(options.context as OpenFunctionContext)) {
options.context!.port = options.port;

Expand All @@ -59,7 +66,12 @@ export const main = async () => {
options.context!
);
await server.start();
} else {

// DaprServer uses httpTerminator in server.stop()
handleShutdown(async () => await server.stop());
}
// Then taking sync runtime as the fallback
else {
const server = getServer(userFunction!, signatureType, options.context);
const errorHandler = new ErrorHandler(server);
server
Expand All @@ -73,6 +85,12 @@ export const main = async () => {
}
})
.setTimeout(0); // Disable automatic timeout on incoming connections.

// Create and use httpTerminator for Express
const terminator = createHttpTerminator({
server,
});
handleShutdown(async () => await terminator.terminate());
}
} catch (e) {
if (e instanceof OptionsError) {
Expand All @@ -86,3 +104,15 @@ export const main = async () => {

// Call the main method to load the user code and start the http server.
main();

function handleShutdown(handler: () => Promise<void>): void {
if (!handler) return;

const shutdown = async (code: string) => {
console.log(`🛑 Terminating OpenFunction server on code ${code}...`);
await handler();
};

process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
}
5 changes: 3 additions & 2 deletions test/conformance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
},
"scripts": {
"start": "functions-framework --target=writeHttp",
"knative:async": "concurrently npm:knative:async:run:* npm:knative:async:test",
"compile": "cd .. && npm run compile",
"knative:async": "npm run compile && concurrently npm:knative:async:run:* npm:knative:async:test",
"knative:async:run:func": "cross-env DEBUG=test:*,common:*,ofn:* env-cmd -e knative functions-framework --signature-type=openfunction --target=tryKnativeAsync",
"knative:async:run:dapr": "dapr run -H 3500 -d ../data/components/http --log-level warn",
"knative:async:test": "wait-on tcp:3500 tcp:8080 && curl -s -d '{\"data\": \"hello\"}' -H 'Content-Type: application/json' localhost:8080",
"async": "concurrently npm:async:run:*",
"async": "npm run compile && concurrently npm:async:run:*",
"async:run:func": "cross-env DEBUG=test:*,common:*,ofn:* env-cmd -e async functions-framework --target=tryAsync",
"async:run:dapr": "dapr run -H 3500 -p 8080 -d ../data/components/cron --log-level info"
}
Expand Down