55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
"""threshold-event-detector 入口(python -m detector)。
|
||
|
||
用法:
|
||
python -m detector [--addr 0.0.0.0:8080] [--no-seed]
|
||
|
||
默认以进程内存储启动(零依赖),写入示例规则并监听 REST API;可通过环境变量
|
||
HMS_* 切换 MySQL / Redis / Kafka 驱动(需安装对应可选依赖)。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import signal
|
||
|
||
from .api import APIServer
|
||
from .app import DetectorApp
|
||
from .config import Config
|
||
|
||
|
||
def main(argv=None) -> int:
|
||
parser = argparse.ArgumentParser(description="threshold-event-detector")
|
||
parser.add_argument("--addr", default=None, help="HTTP listen address, e.g. 0.0.0.0:8080")
|
||
parser.add_argument("--no-seed", action="store_true", help="do not seed demo rules")
|
||
args = parser.parse_args(argv)
|
||
|
||
config = Config.from_env()
|
||
if args.addr:
|
||
config.http_addr = args.addr
|
||
|
||
app = DetectorApp(config)
|
||
if not args.no_seed and config.seed_demo_rules:
|
||
app.seed_demo_rules()
|
||
app.start()
|
||
|
||
api = APIServer(app, config.http_addr)
|
||
print(f"[threshold-event-detector] listening on http://{config.http_addr}", flush=True)
|
||
|
||
def _stop(signum, frame): # noqa: ARG001
|
||
print("\n[threshold-event-detector] shutting down...", flush=True)
|
||
api.shutdown()
|
||
app.stop()
|
||
|
||
signal.signal(signal.SIGINT, _stop)
|
||
signal.signal(signal.SIGTERM, _stop)
|
||
|
||
try:
|
||
api.serve_forever()
|
||
finally:
|
||
app.stop()
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|