Coverage for src/healthz.py: 0%

12 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-28 08:08 +0000

1#!/usr/bin/env python3 

2""" 

3Liveness のチェックを行います 

4 

5Usage: 

6 healthz.py [-c CONFIG] [-m MODE] [-p PORT] [-D] 

7 

8Options: 

9 -c CONFIG : CONFIG を設定ファイルとして読み込んで実行します。[default: config.yaml] 

10 -m (CRTL|ACT|WEB) : 動作モード [default: CTRL] 

11 -p PORT : WEB サーバのポートを指定します。[default: 5000] 

12 -D : デバッグモードで動作します。 

13""" 

14 

15import logging 

16import pathlib 

17import sys 

18 

19import my_lib.healthz 

20 

21SCHEMA_CONFIG = "config.schema" 

22 

23 

24def check_liveness(target_list, port=None): 

25 for target in target_list: 

26 if not my_lib.healthz.check_liveness(target["name"], target["liveness_file"], target["interval"]): 

27 return False 

28 

29 if port is not None: 

30 return my_lib.healthz.check_port(port) 

31 else: 

32 return True 

33 

34 

35if __name__ == "__main__": 

36 import docopt 

37 import my_lib.config 

38 import my_lib.logger 

39 import my_lib.pretty 

40 

41 args = docopt.docopt(__doc__) 

42 

43 config_file = args["-c"] 

44 mode = args["-m"] 

45 port = args["-p"] 

46 debug_mode = args["-D"] 

47 

48 my_lib.logger.init("hems.unit_cooler", level=logging.DEBUG if debug_mode else logging.INFO) 

49 

50 config = my_lib.config.load(config_file, pathlib.Path(SCHEMA_CONFIG)) 

51 

52 logging.info("Mode: %s", mode) 

53 if mode == "CTRL": 

54 conf_list = [["controller"]] 

55 port = None 

56 elif mode == "WEB": 

57 conf_list = [["webui", "subscribe"]] 

58 else: 

59 conf_list = [["actuator", "subscribe"], ["actuator", "control"], ["actuator", "monitor"]] 

60 port = None 

61 

62 target_list = [ 

63 { 

64 "name": " - ".join(conf_path), 

65 "liveness_file": my_lib.config.get_path(config, conf_path, ["liveness", "file"]), 

66 "interval": ( # noqa: PLC3002 

67 lambda x: x 

68 if x is not None 

69 else my_lib.config.get_data(config, ["controller"], ["interval_sec"]) 

70 )(my_lib.config.get_data(config, conf_path, ["interval_sec"])), 

71 } 

72 for conf_path in conf_list 

73 ] 

74 

75 logging.debug(my_lib.pretty.format(target_list)) 

76 

77 if check_liveness(target_list, port): 

78 logging.info("OK.") 

79 sys.exit(0) 

80 else: 

81 sys.exit(-1)