Skip to content

Commit

Permalink
Fix SIGTERM signal handling for clean docker container shutdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-chambers committed Jan 5, 2024
1 parent 0eafce6 commit ec541a7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM node:18-alpine

RUN apk add jq

COPY /docker /scripts

COPY /functions /functions
Expand Down
8 changes: 7 additions & 1 deletion docker/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ set -eu -o pipefail
/scripts/package-restore.sh

cd /functions
exec npm start

# Read the npm start script from package.json then exec it to ensure that
# it is the root process in the container, so that signals (ie SIGTERM)
# are propagated properly. "npm start" does not propagate SIGTERM to the
# actual started process
START_CMD=$(jq -r ".scripts.start" "package.json")
PATH=$PATH:/functions/node_modules/.bin exec $START_CMD
7 changes: 4 additions & 3 deletions ndc-lambda-sdk/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const [command, args] =
? ["ts-node-dev", ["--respawn", ...tsNodeArgs]]
: ["ts-node", tsNodeArgs]

const result = spawn.sync(command, args, { stdio: "inherit" })
if (result.error) console.error(result.error);
process.exit(result.status ?? undefined);
const childProcess = spawn(command, args, { stdio: "inherit" })
process.on("SIGTERM", () => childProcess.kill("SIGTERM"));
childProcess.on("error", err => console.error(err));
childProcess.on("close", code => process.exit(code));

0 comments on commit ec541a7

Please sign in to comment.