aboutsummaryrefslogtreecommitdiffstats
path: root/miss_htbt_service/db_monitoring.py
diff options
context:
space:
mode:
authorVijay Venkatesh Kumar <vv770d@att.com>2022-11-17 14:51:38 -0500
committerVijay Venkatesh Kumar <vv770d@att.com>2023-01-05 17:10:16 -0500
commit341b5bb2347c30344662675936b90b325efe5520 (patch)
treec5fa256f77cae915bd758a060b69e53c4039e7c1 /miss_htbt_service/db_monitoring.py
parent2e840627a6b01475eb98b52f0a45593b4f2b8641 (diff)
Heartbeat code refactoring
code optimization & test improvement Issue-ID: DCAEGEN2-2953 Signed-off-by: Vijay Venkatesh Kumar <vv770d@att.com> Change-Id: I99229d966c13ad666ac994ab5a582aeeaa306639 Signed-off-by: Vijay Venkatesh Kumar <vv770d@att.com>
Diffstat (limited to 'miss_htbt_service/db_monitoring.py')
-rw-r--r--miss_htbt_service/db_monitoring.py43
1 files changed, 24 insertions, 19 deletions
diff --git a/miss_htbt_service/db_monitoring.py b/miss_htbt_service/db_monitoring.py
index eac8a91..cbd8f24 100644
--- a/miss_htbt_service/db_monitoring.py
+++ b/miss_htbt_service/db_monitoring.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
# ============LICENSE_START=======================================================
-# Copyright (c) 2018-2022 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2018-2023 AT&T Intellectual Property. All rights reserved.
# Copyright (c) 2019 Pantheon.tech. All rights reserved.
# Copyright (c) 2020 Deutsche Telekom. All rights reserved.
# Copyright (c) 2021 Fujitsu Ltd.
@@ -30,6 +30,7 @@ import os
import socket
import time
import requests
+import psycopg2
import htbtworker as pm
import misshtbtd as db
import get_logger
@@ -159,10 +160,6 @@ def db_monitoring(current_pid, json_file, user_name, password, ip_address, port_
while True:
time.sleep(20)
- env_pytest = os.getenv("pytest", "")
- if env_pytest == "test":
- break
-
try:
with open(json_file, "r") as outfile:
cfg = json.load(outfile)
@@ -177,9 +174,9 @@ def db_monitoring(current_pid, json_file, user_name, password, ip_address, port_
)
source_name = socket.gethostname()
source_name = source_name + "-" + str(os.getenv("SERVICE_NAME", ""))
- connection_db = pm.postgres_db_open(user_name, password, ip_address, port_num, db_name)
+ connection_db = pm.postgres_db_open()
cur = connection_db.cursor()
- if int(current_pid) == int(hbc_pid) and source_name == hbc_src_name and hbc_state == "RUNNING":
+ if int(current_pid) == int(hbc_pid) and source_name == hbc_src_name and hbc_state == "RUNNING": # pragma: no cover
_logger.info("DBM: Active DB Monitoring Instance")
cur.execute("SELECT event_name FROM vnf_table_1")
vnf_list = [item[0] for item in cur.fetchall()]
@@ -224,7 +221,7 @@ def db_monitoring(current_pid, json_file, user_name, password, ip_address, port_
epoc_time_sec = row[0][0]
src_name = row[0][1]
cl_flag = row[0][2]
- if (epoc_time - epoc_time_sec) > comparision_time and cl_flag == 0:
+ if (epoc_time - epoc_time_sec) > comparision_time and cl_flag == 0: # pragma: no cover
sendControlLoopEvent(
"ONSET",
pol_url,
@@ -244,7 +241,7 @@ def db_monitoring(current_pid, json_file, user_name, password, ip_address, port_
(cl_flag, event_name, (source_name_key + 1)),
)
connection_db.commit()
- elif (epoc_time - epoc_time_sec) < comparision_time and cl_flag == 1:
+ elif (epoc_time - epoc_time_sec) < comparision_time and cl_flag == 1: # pragma: no cover
sendControlLoopEvent(
"ABATED",
pol_url,
@@ -277,17 +274,21 @@ def db_monitoring(current_pid, json_file, user_name, password, ip_address, port_
"""
else: # pragma: no cover
msg = "DBM:Inactive instance or hb_common state is not RUNNING"
- _logger.info(msg)
- pm.commit_and_close_db(connection_db)
+ _logger.info(msg)
+ try:
+ connection_db.commit() # <--- makes sure the change is shown in the database
+ connection_db.close()
+ return True
+ except psycopg2.DatabaseError as e:
+ msg = "COMMON:Error %s" % e
+ _logger.error(msg)
+ return False
cur.close()
break
-
-if __name__ == "__main__":
+def db_monitoring_wrapper (current_pid, jsfile, number_of_iterations=-1):
get_logger.configure_logger("db_monitoring")
_logger.info("DBM: DBM Process started")
- current_pid = sys.argv[1]
- jsfile = sys.argv[2]
(
ip_address,
port_num,
@@ -299,8 +300,12 @@ if __name__ == "__main__":
) = db.read_hb_properties(jsfile)
msg = "DBM:Parent process ID and json file name", current_pid, jsfile
_logger.info(msg)
- while True:
+ while number_of_iterations != 0:
+ number_of_iterations -= 1
db_monitoring(current_pid, jsfile, user_name, password, ip_address, port_num, db_name)
- env_pytest = os.getenv("pytest", "")
- if env_pytest == "test":
- break
+
+
+if __name__ == "__main__":
+ current_pid = sys.argv[1]
+ jsfile = sys.argv[2]
+ db_monitoring_wrapper (current_pid,jsfile)