Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
I
install.api
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Script
install.api
Commits
2219f038
Commit
2219f038
authored
Apr 08, 2020
by
金凯强
🎨
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
数据库连接测试超时问题修改
parent
f02fc195
Pipeline
#22244
failed with stage
in 11 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
45 deletions
+45
-45
common_tools.py
app/utils/common_tools.py
+45
-45
No files found.
app/utils/common_tools.py
View file @
2219f038
...
...
@@ -7,6 +7,49 @@ from huansi_utils.app.apploader import logger
from
huansi_utils.exception.exception
import
HSException
# 超过四秒,基本上没救了,告辞~~~~
@
timeout_decorator
.
timeout
(
2
)
def
test_sql_connection
(
ip
,
port
,
user_name
,
password
,
db_name
):
from
sqlalchemy
import
create_engine
from
sqlalchemy.orm
import
sessionmaker
con_str
=
f
'mssql+pymssql://{user_name}:{password}@{ip}:{port}/{db_name}'
engine
=
create_engine
(
con_str
)
DBSession
=
sessionmaker
(
bind
=
engine
)
try
:
session
=
DBSession
()
result
=
session
.
execute
(
"select test='huansi'"
)
data
=
result
.
fetchone
()
if
data
.
test
==
'huansi'
:
return
True
else
:
return
False
except
Exception
as
e
:
if
isinstance
(
e
,
timeout_decorator
.
TimeoutError
):
logger
.
error
(
"数据库连接失败,失败原因:{}"
.
format
(
e
))
return
False
orig
=
getattr
(
e
,
'orig'
)
args
=
getattr
(
orig
,
'args'
,
((),))[
0
]
error_message
=
''
for
e_item
in
args
:
if
isinstance
(
e_item
,
bytes
):
try
:
e_item
=
e_item
.
decode
()
except
:
e_item
=
str
(
e_item
)
error_message
+=
str
(
e_item
)
if
not
error_message
:
error_message
=
str
(
e
)
logger
.
error
(
"数据库连接失败,失败原因:{}"
.
format
(
error_message
))
return
False
class
CommonTools
:
@
classmethod
def
validate_dict_value_all_not_none
(
cls
,
dict_data
:
dict
,
mapping
=
None
):
...
...
@@ -21,9 +64,7 @@ class CommonTools:
if
value
is
None
:
raise
HSException
(
f
"{key}不能为空"
)
# 超过四秒,基本上没救了,告辞~~~~
@
timeout_decorator
.
timeout
(
2
)
def
test_db_connection
(
cls
,
ip
:
int
,
port
:
int
,
user_name
:
str
,
password
:
str
,
db_name
:
str
):
def
test_db_connection
(
self
,
ip
:
int
,
port
:
int
,
user_name
:
str
,
password
:
str
,
db_name
:
str
):
'''
测试数据库连接
:param ip:
...
...
@@ -34,48 +75,7 @@ class CommonTools:
:param sql:
:return:
'''
from
sqlalchemy
import
create_engine
from
sqlalchemy.orm
import
sessionmaker
con_str
=
f
'mssql+pymssql://{user_name}:{password}@{ip}:{port}/{db_name}'
engine
=
create_engine
(
con_str
)
DBSession
=
sessionmaker
(
bind
=
engine
)
try
:
session
=
DBSession
()
result
=
session
.
execute
(
"select test='huansi'"
)
data
=
result
.
fetchone
()
if
data
.
test
==
'huansi'
:
return
True
else
:
return
False
except
Exception
as
e
:
if
isinstance
(
e
,
TimeoutError
):
logger
.
error
(
"数据库连接失败,失败原因:{}"
.
format
(
e
))
return
False
orig
=
getattr
(
e
,
'orig'
)
args
=
getattr
(
orig
,
'args'
,
((),))[
0
]
error_message
=
''
for
e_item
in
args
:
if
isinstance
(
e_item
,
bytes
):
try
:
e_item
=
e_item
.
decode
()
except
:
e_item
=
str
(
e_item
)
error_message
+=
str
(
e_item
)
if
not
error_message
:
error_message
=
str
(
e
)
logger
.
error
(
"数据库连接失败,失败原因:{}"
.
format
(
error_message
))
return
False
return
test_sql_connection
(
ip
,
port
,
user_name
,
password
,
db_name
)
@
classmethod
def
validate_runner_is_install
(
self
):
...
...
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