Skip to content

Commit f93f861

Browse files
committed
Disconnect Handling to SQL
1 parent af2c05b commit f93f861

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#ignore IDE settings
55
*.idea*
6+
*.vscode*
67

78
#setup
89
build/*

database/db.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525

2626
def db_inputs(connection_type):
2727
"""
28-
a function to determine the type of database the user wants to work with and
29-
selects the corresponding connection to the db
28+
a function to determine the type of database the user wants to work with and
29+
selects the corresponding connection to the db
3030
31-
Args:
32-
connection_type: type of db we are working with
31+
Args:
32+
connection_type: type of db we are working with
3333
34-
Returns:
35-
corresponding command to connect to the db
36-
"""
34+
Returns:
35+
corresponding command to connect to the db
36+
"""
3737
return {
3838
"postgres": 'postgres+psycopg2://{0}:{1}@{2}:{3}/{4}'.format(USER, PASSWORD, HOST, PORT, DATABASE),
3939
"mysql": 'mysql://{0}:{1}@{2}:{3}/{4}'.format(USER, PASSWORD, HOST, PORT, DATABASE),
@@ -43,25 +43,22 @@ def db_inputs(connection_type):
4343

4444
def create_connection():
4545
"""
46-
a function to create connections to db, it retries 100 times if connection returned an error
46+
a function to create connections to db with pessimistic approach
4747
4848
Returns:
4949
connection if success otherwise False
5050
"""
5151
try:
52-
for _ in range(0, 100):
53-
try:
54-
db_engine = create_engine(
55-
db_inputs(DB),
56-
connect_args={
57-
'check_same_thread': False
58-
}
59-
)
60-
Session = sessionmaker(bind=db_engine)
61-
session = Session()
62-
return session
63-
except Exception:
64-
time.sleep(0.1)
52+
db_engine = create_engine(
53+
db_inputs(DB),
54+
connect_args={
55+
'check_same_thread': False
56+
},
57+
pool_pre_ping=True
58+
)
59+
Session = sessionmaker(bind=db_engine)
60+
session = Session()
61+
return session
6562
except Exception:
6663
warn(messages("database_connect_fail"))
6764
return False
@@ -196,17 +193,17 @@ def submit_temp_logs_to_db(log):
196193

197194
def find_temp_events(target, module_name, scan_unique_id, event_name):
198195
"""
199-
select all events by scan_unique id, target, module_name
196+
select all events by scan_unique id, target, module_name
200197
201-
Args:
202-
target: target
203-
module_name: module name
204-
scan_unique_id: unique scan identifier
205-
event_name: event_name
198+
Args:
199+
target: target
200+
module_name: module name
201+
scan_unique_id: unique scan identifier
202+
event_name: event_name
206203
207-
Returns:
208-
an array with JSON events or an empty array
209-
"""
204+
Returns:
205+
an array with JSON events or an empty array
206+
"""
210207
session = create_connection()
211208
try:
212209
for _ in range(1, 100):

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ texttable==1.6.7
99
PySocks==1.7.1 # library_name=socks # module name is not equal to socks name; this is required to be checked on startup
1010
pyOpenSSL==23.2.0 # library_name=OpenSSL
1111
flask==2.3.2
12-
SQLAlchemy>=1.3.0 # library_name=sqlalchemy
12+
SQLAlchemy>=1.4.43 # library_name=sqlalchemy
1313
py3DNS==3.2.1 # library_name=DNS
1414
numpy==1.24.3
1515
terminable_thread==0.7.1

0 commit comments

Comments
 (0)