Skip to content

Commit 3e59bf5

Browse files
Adding forced connection on start up
1 parent 72d14ff commit 3e59bf5

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

.github/CONTRIBUTING.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1-
# Contributing to Ithaca Transit Backend
1+
# Contributing to Score Backend
2+
23
👍🎉 First off, congrats on getting put on this pod 😂🎉👍
34

45
The following is a set of guidelines for contributing to our backend. These are mostly guidelines, not rules. Use your best judgment, and feel free to propose changes to this document in a pull request.
56

67
# Versioning
8+
79
This is _extremely_ important as handling our backend can get quite messy.
810

911
# Making PRs
12+
1013
We want our PRs to be concise but informative. Some pointers as per our PR template:
14+
1115
### Title
16+
1217
Summarize what changes you've made in one sentence. For example: "Exclude staff from the check for follows". For stacked PRs, please indicate clearly in the title where in the stack you are. For example: "[Eatery Refactor][4/5] Converted all files to MVP model."
18+
1319
### Overview
20+
1421
Summarize generally what the purpose of this PR is.
22+
1523
### Changes
24+
1625
Include details of what your changes actually are and how it is intended to work.
26+
1727
### Test Coverage
28+
1829
Describe how you tested this feature: manual testing and/or unit testing. Please include repro steps and/or how to turn the feature on if applicable. In the context of this repo, add a plan for how you intend to test on [integration](https://github.com/cuappdev/integration), with your newly created issue linked.
30+
1931
### Next Steps
32+
2033
If this applies, describe how you plan on addressing future PRs if this is a part of a multi-PR change.
34+
2135
### Related PRs or Issues
22-
If this applies, list related PRs against other branches or repositories.
2336

37+
If this applies, list related PRs against other branches or repositories.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ pillow
88
Flask-APScheduler
99
python-dotenv
1010
pytz
11-
gunicorn
11+
gunicorn

src/database.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,46 @@
11
from pymongo import MongoClient
22
import os
33
from dotenv import load_dotenv
4+
import threading
5+
import time
46

57
load_dotenv()
68

9+
# Determine certificate path and TLS usage
710
if os.getenv("STAGE") == "local":
8-
file_name = "ca-certificate.crt"
9-
use_tls = os.getenv("MONGO_URI") != "mongodb://localhost:27017/"
11+
file_name = "ca-certificate.crt"
12+
use_tls = os.getenv("MONGO_URI") != "mongodb://localhost:27017/"
1013
else:
11-
file_name = "/etc/ssl/ca-certificate.crt"
12-
use_tls = True
14+
file_name = "/etc/ssl/ca-certificate.crt"
15+
use_tls = True
1316

17+
# Initialize MongoDB client
1418
if use_tls:
1519
client = MongoClient(os.getenv("MONGO_URI"), tls=True, tlsCAFile=file_name)
1620
else:
1721
client = MongoClient(os.getenv("MONGO_URI"))
1822

23+
# Force connection on startup
24+
try:
25+
client.admin.command("ping")
26+
print("✅ MongoDB connection successful on startup")
27+
except Exception as e:
28+
print(f"❌ MongoDB connection failed on startup: {e}")
29+
raise e
30+
31+
32+
# Start a keep-alive thread
33+
def keep_connection_alive():
34+
while True:
35+
try:
36+
client.admin.command("ping")
37+
print("🔁 MongoDB keep-alive ping successful")
38+
except Exception as e:
39+
print(f"⚠️ MongoDB keep-alive ping failed: {e}")
40+
time.sleep(300) # ping every 5 minutes
41+
42+
43+
threading.Thread(target=keep_connection_alive, daemon=True).start()
44+
45+
# Access the database
1946
db = client[os.getenv("MONGO_DB", "score_db")]

0 commit comments

Comments
 (0)