Coverage for src/healthz.py: 0%
11 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-08-19 01:39 +0900
« prev ^ index » next coverage.py v7.9.1, created at 2025-08-19 01:39 +0900
1#!/usr/bin/env python3
2"""
3Liveness のチェックを行います
5Usage:
6 healthz.py [-c CONFIG] [-s] [-p SERVER_PORT] [-D]
8Options:
9 -c CONFIG : CONFIG を設定ファイルとして読み込んで実行します。[default: config.yaml]
10 -s : サーバーモード。
11 -p SERVER_PORT : ZeroMQ の Pub サーバーを動作させるポートを指定します。 [default: 4444]
12 -D : デバッグモードで動作します。
13"""
15import logging
16import pathlib
17import sys
19import my_lib.healthz
22def check_liveness(target_list, port=None):
23 for target in target_list:
24 if not my_lib.healthz.check_liveness(target["name"], target["liveness_file"], target["interval"]):
25 return False
27 if port is not None:
28 return my_lib.healthz.check_tcp_port(port)
29 else:
30 return True
33######################################################################
34if __name__ == "__main__":
35 import os
37 import docopt
38 import my_lib.config
39 import my_lib.logger
41 args = docopt.docopt(__doc__)
43 config_file = args["-c"]
44 server_mode = args["-s"]
45 port = args["-p"]
46 server_port = int(os.environ.get("HEMS_SERVER_PORT", args["-p"]))
47 debug_mode = args["-D"]
49 my_lib.logger.init("hems.wattmeter-sharp", level=logging.DEBUG if debug_mode else logging.INFO)
51 config = my_lib.config.load(config_file)
53 target_list = [
54 {
55 "name": name,
56 "liveness_file": pathlib.Path(config["liveness"]["file"][name]),
57 "interval": 6 * 60, # NOTE: 6分間隔でパケットが飛んでくる
58 }
59 for name in ["measure"]
60 ]
62 if check_liveness(target_list, server_port if server_mode else None):
63 logging.info("OK.")
64 sys.exit(0)
65 else:
66 sys.exit(-1)