Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
shadowban-eu-backend
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
shadowban-eu-backend
Commits
11025e87
Commit
11025e87
authored
Oct 15, 2020
by
Raphael Beer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add: wait for DB +
Makes 7 attempts to connect to the database
parent
208a9d57
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
21 deletions
+31
-21
db.py
db.py
+31
-21
No files found.
db.py
View file @
11025e87
import
copy
import
traceback
import
sys
from
time
import
sleep
from
pymongo
import
MongoClient
,
errors
as
MongoErrors
,
DESCENDING
class
Database
:
...
...
@@ -9,26 +10,19 @@ class Database:
RESULTS_COLLECTION
=
'results'
RATELIMIT_COLLECTION
=
'rate-limits'
try
:
print
(
'[mongoDB] Connecting to '
+
host
+
':'
+
str
(
port
))
print
(
'[mongoDB] Using Database `'
+
db
+
'`'
)
# client and DB
self
.
client
=
MongoClient
(
host
,
port
,
serverSelectionTimeoutMS
=
3
,
username
=
username
,
password
=
password
)
self
.
db
=
self
.
client
[
db
]
# collections
self
.
results
=
self
.
db
[
RESULTS_COLLECTION
]
self
.
rate_limits
=
self
.
db
[
RATELIMIT_COLLECTION
]
# Test connection immediately, instead of
# when trying to write in a request, later.
self
.
client
.
admin
.
command
(
'ismaster'
)
except
MongoErrors
.
ServerSelectionTimeoutError
:
print
(
traceback
.
format_exc
())
sys
.
exit
(
'MongoDB connection timed out.'
)
except
:
print
(
traceback
.
format_exc
())
sys
.
exit
(
'MongoDB connection failed.'
)
print
(
'[mongoDB] Connecting to '
+
host
+
':'
+
str
(
port
))
print
(
'[mongoDB] Using Database `'
+
db
+
'`'
)
# client and DB
self
.
client
=
MongoClient
(
host
,
port
,
serverSelectionTimeoutMS
=
3
,
username
=
username
,
password
=
password
)
self
.
db
=
self
.
client
[
db
]
# collections
self
.
results
=
self
.
db
[
RESULTS_COLLECTION
]
self
.
rate_limits
=
self
.
db
[
RATELIMIT_COLLECTION
]
# Test connection immediately, instead of
# when trying to write in a request, later.
self
.
client
.
admin
.
command
(
'ismaster'
)
def
write_result
(
self
,
result
):
# copy.deepcopy; otherwise mongo ObjectId (_id) would be added,
...
...
@@ -45,4 +39,20 @@ def connect(host=None, port=27017, db='tester', username=None, password=None):
if
host
is
None
:
raise
ValueError
(
'[mongoDB] Database constructor needs a `host`name or ip!'
)
return
Database
(
host
=
host
,
port
=
port
,
db
=
db
,
username
=
username
,
password
=
password
)
attempt
=
0
max_attempts
=
7
mongo_client
=
None
while
(
mongo_client
is
None
):
print
(
'[mongoDB|connect] Connecting, '
,
attempt
,
'/'
,
max_attempts
)
try
:
mongo_client
=
Database
(
host
=
host
,
port
=
port
,
db
=
db
,
username
=
username
,
password
=
password
)
except
Exception
as
e
:
if
attempt
is
max_attempts
:
raise
e
sleep
(
attempt
)
attempt
+=
1
return
mongo_client
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment