X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_lcm%2Flcm_hc.py;h=25be92c33bde5e10cffb4ccc59c620bf16164287;hb=56b86c280d4c67ea0844b2049b4e505abba8429e;hp=701e5b6f073413ac4d63b42d329682d5617e0fe8;hpb=94f0611a61f3f115bed849887b7c538f40f11c6d;p=osm%2FLCM.git diff --git a/osm_lcm/lcm_hc.py b/osm_lcm/lcm_hc.py index 701e5b6..25be92c 100644 --- a/osm_lcm/lcm_hc.py +++ b/osm_lcm/lcm_hc.py @@ -17,17 +17,43 @@ # under the License. ## -from os import path from time import time, sleep from sys import stderr -""" This module is used for helth check. A file called time_last_ping is used +from osm_lcm.lcm_utils import LcmException +import yaml + +""" This module is used for health check. A file called time_last_ping is used This contains the last time where something is received from kafka """ -def health_check(health_check_file=None, ping_interval_pace=120): - health_check_file = health_check_file or path.expanduser("~") + "/time_last_ping" +def get_health_check_file(config_file=None): + try: + health_check_file = "/app/storage/time_last_ping" + if not config_file: + return health_check_file + # If config_input is dictionary + if isinstance(config_file, dict) and config_file.get("storage"): + health_check_file = config_file["storage"]["path"] + "/time_last_ping" + # If config_input is file + elif isinstance(config_file, str): + with open(config_file) as f: + # read file as yaml format + conf = yaml.safe_load(f) + # Ensure all sections are not empty + if conf.get("storage"): + health_check_file = conf["storage"]["path"] + "/time_last_ping" + + return health_check_file + except (IOError, FileNotFoundError, TypeError, AttributeError, KeyError) as error: + raise LcmException( + f"Error occured while getting the health check file location: {error}" + ) + + +def health_check(config_file=None, ping_interval_pace=120): + health_check_file = get_health_check_file(config_file) retry = 2 while retry: retry -= 1 @@ -35,7 +61,9 @@ def health_check(health_check_file=None, ping_interval_pace=120): with open(health_check_file, "r") as f: last_received_ping = f.read() - if time() - float(last_received_ping) < 2 * ping_interval_pace: # allow one ping not received every two + if ( + time() - float(last_received_ping) < 2 * ping_interval_pace + ): # allow one ping not received every two exit(0) except Exception as e: print(e, file=stderr) @@ -44,5 +72,5 @@ def health_check(health_check_file=None, ping_interval_pace=120): exit(1) -if __name__ == '__main__': +if __name__ == "__main__": health_check()