Coverage for flask/src/healthz.py: 76%

11 statements  

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

1#!/usr/bin/env python3 

2""" 

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

4 

5Usage: 

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

7 

8Options: 

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

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

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

12""" 

13 

14import logging 

15import pathlib 

16import sys 

17 

18import my_lib.healthz 

19 

20 

21def check_liveness(target_list, port=None): 

22 for target in target_list: 

23 if not my_lib.healthz.check_liveness(target["name"], target["liveness_file"], target["interval"]): 23 ↛ 24line 23 didn't jump to line 24 because the condition on line 23 was never true

24 return False 

25 

26 if port is not None: 26 ↛ 27line 26 didn't jump to line 27 because the condition on line 26 was never true

27 return my_lib.healthz.check_port(port) 

28 else: 

29 return True 

30 

31 

32if __name__ == "__main__": 

33 import docopt 

34 import my_lib.config 

35 import my_lib.logger 

36 

37 args = docopt.docopt(__doc__) 

38 

39 config_file = args["-c"] 

40 port = args["-p"] 

41 debug_mode = args["-D"] 

42 

43 my_lib.logger.init("hems.rasp-water", level=logging.DEBUG if debug_mode else logging.INFO) 

44 

45 config = my_lib.config.load(config_file) 

46 

47 target_list = [ 

48 { 

49 "name": name, 

50 "liveness_file": pathlib.Path(config["liveness"]["file"][name]), 

51 "interval": 10, 

52 } 

53 for name in ["scheduler"] 

54 ] 

55 

56 if check_liveness(target_list, port): 

57 logging.info("OK.") 

58 sys.exit(0) 

59 else: 

60 sys.exit(-1)