Skip to content

feat: graceful shutdown (#58)#107

Open
edehvictor wants to merge 1 commit intoCalloraOrg:mainfrom
edehvictor:feature/graceful-shutdown
Open

feat: graceful shutdown (#58)#107
edehvictor wants to merge 1 commit intoCalloraOrg:mainfrom
edehvictor:feature/graceful-shutdown

Conversation

@edehvictor
Copy link

Summary

This PR implements a robust graceful shutdown mechanism to ensure the backend safely drains in-flight requests and closes database connections on SIGTERM and SIGINT signals, preventing 502 errors during deployments or restarts.

Fixes #58

Changes Made

  • Signal Handlers: Registered listeners for SIGTERM and SIGINT inside src/index.ts.
  • Load Balancer Draining: Calls server.close() immediately to stop accepting new HTTP requests.
  • In-Flight Request Tracking: Implemented raw TCP socket tracking using a Set (activeConnections) to ensure critical billing and settlement requests are fully processed before the server exits.
  • Database Teardown: Extracted closeDb() in src/db/index.ts and integrated it into the shutdown sequence to safely close the SQLite connection.
  • Timeout Safety Net: Added a 30-second maximum shutdown timeout (TIMEOUT_MS) to force-exit the process if connections hang indefinitely.

Testing

  • Verified server stops accepting new connections immediately on CTRL+C.
  • Verified the event loop waits for active connections to drop to 0.
  • Verified the database closes successfully before process.exit(0).

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.

Add Graceful Shutdown

1 participant