-
Notifications
You must be signed in to change notification settings - Fork 22
fix: make API resilient to DB/pooler connection loss #2330
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| import 'reflect-metadata'; | ||
| import { registerGlobalErrorHandlers } from './utils/globalErrorHandlers'; | ||
| import { bootstrap } from './server/bootstrap'; | ||
|
|
||
| // Register process-level error handlers before anything async runs so a | ||
| // transient DB/pooler error rejecting a promise cannot crash the whole API. | ||
| registerGlobalErrorHandlers(); | ||
|
|
||
| bootstrap(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { logger } from './logger'; | ||
| import SentryLogger from '../sentryLogger'; | ||
|
|
||
| /** | ||
| * Registers process-level handlers for errors that escape application code. | ||
| * | ||
| * Why this exists: the API runs behind a Postgres connection pooler | ||
| * (DigitalOcean managed Postgres / PgBouncer). When the pooler or database has | ||
| * a transient problem, in-flight DB queries reject. Without an | ||
| * `unhandledRejection` listener, Node (>= 15) terminates the whole process on | ||
| * the first such rejection, taking the entire API down for a momentary DB blip. | ||
| * | ||
| * Behaviour: | ||
| * - unhandledRejection: log + report to Sentry, then KEEP the process alive. | ||
| * A single rejected promise (often a transient DB error) must not tear down | ||
| * the server; it should keep serving and recover once the DB is reachable. | ||
| * - uncaughtException: log + report to Sentry, then EXIT. The process state is | ||
| * undefined after an uncaught exception, so we let the container restart | ||
| * policy (`restart: always`) recreate a clean process. | ||
| */ | ||
| let handlersRegistered = false; | ||
|
|
||
| export function registerGlobalErrorHandlers(): void { | ||
| // Idempotent: never attach the listeners more than once. | ||
| if (handlersRegistered) { | ||
| return; | ||
| } | ||
| handlersRegistered = true; | ||
|
|
||
| process.on('unhandledRejection', (reason: unknown) => { | ||
| logger.error('unhandledRejection - process kept alive', reason); | ||
| captureToSentry(reason); | ||
| }); | ||
|
|
||
| process.on('uncaughtException', (error: Error) => { | ||
| logger.fatal('uncaughtException - exiting for a clean restart', error); | ||
| captureToSentry(error); | ||
| flushSentryAndExit(); | ||
| }); | ||
| } | ||
|
|
||
| function captureToSentry(error: unknown): void { | ||
| try { | ||
| SentryLogger.captureException( | ||
| error instanceof Error ? error : new Error(String(error)), | ||
| ); | ||
| } catch { | ||
| // Never let error reporting throw from inside an error handler. | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Flushes pending Sentry events (best-effort, max 2s) and then exits with a | ||
| * non-zero code so the container restart policy (`restart: always`) recreates a | ||
| * clean process. Used by the uncaughtException handler above and by the | ||
| * bootstrap() startup-failure path. | ||
| */ | ||
| export function flushSentryAndExit(): void { | ||
| // A hard fallback guarantees the process exits even if flushing stalls. | ||
| const forceExit = setTimeout(() => process.exit(1), 3000); | ||
| void SentryLogger.close(2000) | ||
| .catch(() => undefined) | ||
| .then(() => { | ||
| clearTimeout(forceExit); | ||
| process.exit(1); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.