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

feat: implement graceful shutdown on sigterm #728

Merged
merged 2 commits into from
Feb 21, 2024
Merged

Conversation

sweatybridge
Copy link
Contributor

@sweatybridge sweatybridge commented Feb 21, 2024

What kind of change does this PR introduce?

Bug fix #663

What is the new behavior?

Uses closes-with-grace package from fastify authors. Shuts down the server on the following events

  • process.once('SIGTERM')
  • process.once('SIGINT')
  • process.once('uncaughtException')
  • process.once('unhandledRejection')

Additional context

Tested with docker-compose.yml

$ docker compose up --build
...
[+] Running 1/1
 ✔ Container postgres-meta-meta-1  Recreated                                           0.1s 
Attaching to postgres-meta-meta-1
postgres-meta-meta-1  | (node:1) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
postgres-meta-meta-1  | (Use `node --trace-warnings ...` to show where the warning was created)
postgres-meta-meta-1  | {"level":"info","time":"2024-02-21T10:03:46.750Z","pid":1,"hostname":"dce592e0de3a","msg":"Server listening at http://0.0.0.0:8080"}
postgres-meta-meta-1  | {"level":"info","time":"2024-02-21T10:03:46.757Z","pid":1,"hostname":"dce592e0de3a","msg":"Server listening at http://0.0.0.0:8081"}
^CGracefully stopping... (press Ctrl+C again to force)
Aborting on container exit...
[+] Running 1/1
 ✔ Container postgres-meta-meta-1  Stopped                                             0.2s 
canceled

@sweatybridge sweatybridge requested review from a team as code owners February 21, 2024 10:07
@@ -16,7 +16,7 @@ COPY --from=build /usr/src/app/node_modules node_modules
COPY --from=build /usr/src/app/dist dist
COPY package.json ./
ENV PG_META_PORT=8080
CMD ["npm", "run", "start"]
CMD ["node", "dist/server/server.js"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was wrong with npm start (run can be omitted here)?
It's one more place you've to remember to update dist/server/server.js in case it's moved 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm run start wasn't passing down signals from the parent process. I tried it initially with a barebone docker-compose.yml.

version: '3'

services:
  pgmeta:
    build: .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be related to this issue npm/cli#6684

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫠

@soedirgo
Copy link
Member

Sweet! Would've thought this is built-in

src/server/server.ts Outdated Show resolved Hide resolved
src/server/server.ts Outdated Show resolved Hide resolved
@sweatybridge sweatybridge merged commit b2b9546 into master Feb 21, 2024
4 checks passed
@sweatybridge sweatybridge deleted the graceful-shutdown branch February 21, 2024 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stopping the container is slow, resorting to SIGKILL
3 participants