import time import flask import config import biz.task from services import DefaultTemplateService from telemetry import init_opentelemetry from util import api # 使用新的服务容器架构 from services.service_container import get_template_service, register_default_services # 确保服务已注册 register_default_services() template_service = get_template_service() import logging LOGGER = logging.getLogger(__name__) init_opentelemetry(batch=False) app = flask.Flask(__name__) @app.get('/health/check') def health_check(): return api.sync_center() @app.post('/') def do_nothing(): return "NOOP" @app.post('/') def do_task(task_id): try: task_info = api.get_task_info(task_id) if not task_info: LOGGER.error("Failed to get task info for task: %s", task_id) return "Failed to get task info", 400 template_id = task_info.get("templateId") if not template_id: LOGGER.error("Task %s missing templateId", task_id) return "Missing templateId", 400 local_template_info = template_service.get_template(template_id) template_info = api.get_template_info(template_id) if not template_info: LOGGER.error("Failed to get template info for template: %s", template_id) return "Failed to get template info", 400 if local_template_info: if local_template_info.get("updateTime") != template_info.get("updateTime"): LOGGER.info("Template %s needs update, downloading...", template_id) if not template_service.download_template(template_id): LOGGER.error("Failed to download template: %s", template_id) return "Failed to download template", 500 biz.task.start_task(task_info) return "OK" except Exception as e: LOGGER.error("Error processing task %s: %s", task_id, e, exc_info=True) return "Internal server error", 500 if __name__ == '__main__': app.run(host="0.0.0.0", port=9998)