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

fix: handle failed db connection on application startup #877

Merged
merged 1 commit into from
Jul 27, 2023
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 server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"node-schedule": "^1.3.2",
"nodemailer": "^6.6.0",
"nodemailer-mailgun-transport": "^2.1.3",
"p-retry": "4",
Copy link
Member

Choose a reason for hiding this comment

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

is the latest version 5 problematic or have other dependency issues?

https://www.npmjs.com/package/p-retry?activeTab=versions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We'd need to switch to ESM in node. From the author: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

Seems like something we should do but I still need to look into it more

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha, thanks! Transitioning to ESM seems like something worth doing at some point but no need to hold this up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've done some looking into it and it still seems like a bit of a dumpster fire that I wouldn't want to touch just yet. The previous versions of these packages that have gone pure ESM seem stable enough to still make up 98% of npm downloads so we have some time

"passport": "^0.6.0",
"passport-facebook": "^3.0.0",
"passport-google-oauth20": "^2.0.0",
Expand Down
19 changes: 15 additions & 4 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import cookieParser from "cookie-parser";
import cors from "cors";
import session from "express-session";
import connectRedis from "connect-redis";
import pRetry from "p-retry";
import * as Sentry from "@sentry/node";
import { Server } from "colyseus";
import schedule from "node-schedule";
Expand Down Expand Up @@ -226,8 +227,18 @@ async function createApp() {
});
}

createConnection(CONNECTION_NAME)
.then(async connection => {
// connect to the database and start the server, retrying if the connection fails
pRetry(
async () => {
await createConnection(CONNECTION_NAME);
await createApp();
})
.catch(error => logger.fatal(error));
},
{
onFailedAttempt: error => {
logger.warn(`Connection to db failed on attempt number ${error.attemptNumber}, retrying...`);
},
retries: 10,
minTimeout: 1 * 1000,
maxTimeout: 60 * 1000,
}
).catch(error => logger.fatal(error));
18 changes: 18 additions & 0 deletions server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,11 @@
dependencies:
"@types/node" "*"

"@types/retry@0.12.0":
version "0.12.0"
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d"
integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==

"@types/semver@^7.3.12":
version "7.5.0"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.0.tgz#591c1ce3a702c45ee15f47a42ade72c2fd78978a"
Expand Down Expand Up @@ -4093,6 +4098,14 @@ p-locate@^4.1.0:
dependencies:
p-limit "^2.2.0"

p-retry@4:
version "4.6.2"
resolved "https://registry.yarnpkg.com/p-retry/-/p-retry-4.6.2.tgz#9baae7184057edd4e17231cee04264106e092a16"
integrity sha512-312Id396EbJdvRONlngUx0NydfrIQ5lsYu0znKVUzVvArzEIt08V1qhtyESbGVd1FGX7UKtiFp5uwKZdM8wIuQ==
dependencies:
"@types/retry" "0.12.0"
retry "^0.13.1"

p-try@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
Expand Down Expand Up @@ -4676,6 +4689,11 @@ resolve@^1.0.0, resolve@^1.1.6, resolve@^1.20.0:
path-parse "^1.0.7"
supports-preserve-symlinks-flag "^1.0.0"

retry@^0.13.1:
version "0.13.1"
resolved "https://registry.yarnpkg.com/retry/-/retry-0.13.1.tgz#185b1587acf67919d63b357349e03537b2484658"
integrity sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg==

reusify@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
Expand Down
Loading