From b7bab8a35c51c11a8f12efbb5690202c42f78076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E4=B8=BA=E4=B9=8B?= <560397@gree.com.cn> Date: Tue, 21 Nov 2023 14:34:47 +0800 Subject: [PATCH] Update project --- application/libs/flask_consul/consul.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/application/libs/flask_consul/consul.py b/application/libs/flask_consul/consul.py index a265c8b..f94827d 100644 --- a/application/libs/flask_consul/consul.py +++ b/application/libs/flask_consul/consul.py @@ -7,7 +7,7 @@ # @Email : StudentCWZ@outlook.com # @Date : 2023/11/21 09:10 # @File : consul.py -# @Description : +# @Description : This module provides an interface for registering and deregistering Flask services with Consul. """ @@ -22,21 +22,25 @@ from application.libs.helper import EnvVarHelper class FlaskConsulService: + """Flask service class for Consul registration and deregistration.""" def __init__(self, app=None): + """Initialize with optional Flask application.""" self.app = app self.client = None self.service_id = None if app is not None: self.init_app(app) - def init_app(self, app: Flask): + def init_app(self, app: Flask) -> None: + """Initialize service with Flask app, register it with Consul and setup deregistration at exit.""" env_vars, server_cfg = self.set_server_cfg(app) self.register_service(env_vars, server_cfg) atexit.register(self.deregister_service) @staticmethod def set_server_cfg(app: Flask) -> tuple: + """Return environment variables and server configuration.""" # 实例化对象 eh = EnvVarHelper() # 从环境变量获取 consul 参数 @@ -52,6 +56,7 @@ class FlaskConsulService: return env_vars, cfg def register_service(self, env_vars: dict, cfg: ConfigHelper): + """Register the service with Consul using the given environment variables and server configuration.""" service_name = cfg.get('Name', 'elasticsearch-log-parse') service_address = cfg.get('IP', 'localhost') service_port = cfg.get('Port', '5000') @@ -77,4 +82,5 @@ class FlaskConsulService: ServerError('Failed to register service') def deregister_service(self): + """Deregister the service from Consul.""" self.client.agent.service.deregister(self.service_id) -- GitLab