diff options
author | Frederick Muriuki Muriithi | 2022-07-18 11:32:44 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2022-07-19 05:08:17 +0300 |
commit | 044184ef28a091519b7632d582387c26bf1543ea (patch) | |
tree | 5c3a08605148d5a718cbb0ecd8fc6d9965242250 /qc_app/check_connections.py | |
parent | 52024b1f6cde74b51181ce4108cf01611a7ea636 (diff) | |
download | gn-uploader-044184ef28a091519b7632d582387c26bf1543ea.tar.gz |
Check connections before launching
* qc_app/__init__.py (refactor): Check connection before launching the
application
* qc_app/check_connections.py (new file): Add code to check
connections
* qc_app/db_utils.py (refactor): enable passing the database uri as an
argument to the connection creation function.
* scripts/worker.py (refactor): Use new code to check for redis
connection.
Diffstat (limited to 'qc_app/check_connections.py')
-rw-r--r-- | qc_app/check_connections.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/qc_app/check_connections.py b/qc_app/check_connections.py new file mode 100644 index 0000000..ceccc32 --- /dev/null +++ b/qc_app/check_connections.py @@ -0,0 +1,28 @@ +"""Check the various connection used in the application""" +import sys +import traceback + +import redis +import MySQLdb + +from qc_app.db_utils import database_connection + +def check_redis(uri: str): + "Check the redis connection" + try: + with redis.Redis.from_url(uri) as rconn: + rconn.ping() + except redis.exceptions.ConnectionError as conn_err: + print(conn_err, file=sys.stderr) + print(traceback.format_exc(), file=sys.stderr) + sys.exit(1) + +def check_db(uri: str): + "Check the mysql connection" + try: + with database_connection(uri) as dbconn: # pylint: disable=[unused-variable] + pass + except MySQLdb.OperationalError as op_err: + print(op_err, file=sys.stderr) + print(traceback.format_exc(), file=sys.stderr) + sys.exit(1) |