From bdbce71217948c388ac8ba767fc8304ec7312e79 Mon Sep 17 00:00:00 2001 From: shy3130 Date: Sat, 11 Jul 2026 10:44:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(ext-data):=20=E6=A6=82=E5=BF=B5/=E8=A1=8C?= =?UTF-8?q?=E4=B8=9A=E5=AE=9A=E6=97=B6=E6=8B=89=E5=8F=96=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=BC=80=E5=90=AF=20+=20=E4=BF=AE=E5=A4=8D=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 概念/行业预设 enabled 由 False 改为 True, 全新部署首次启动即自动调度 - 修复 main.py 启动顺序: ensure_builtin_presets 移到 pull_scheduler.refresh 之前, 否则全新部署时 scheduler 读不到刚创建的预设, 定时任务不启动 - 已有用户遵循"已存在则跳过"原则, 不受影响 --- backend/app/main.py | 17 +++++++++-------- backend/app/services/ext_presets.py | 19 +++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/backend/app/main.py b/backend/app/main.py index b768b39..0da2f49 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -114,20 +114,21 @@ async def lifespan(app: FastAPI): except Exception as e: # noqa: BLE001 logger.warning("wecom_bot_service init failed: %s", e) - # 扩展数据定时拉取 - from app.services.ext_pull import pull_scheduler - pull_scheduler.start(store.data_dir) - pull_scheduler.refresh(store.data_dir) - app.state.pull_scheduler = pull_scheduler - - # 内置扩展表 (概念/行业): 只创建 config (含拉取配置), 不自动拉数据 - # 数据获取由用户在概念/行业页点「获取数据」手动触发 (POST /api/ext-data/presets/{id}/fetch) + # 内置扩展表 (概念/行业): 先创建 config (含拉取配置), 默认开启定时拉取。 + # 必须在 pull_scheduler.refresh() 之前执行, 否则全新部署时 scheduler 读不到 + # 刚创建的预设, 定时任务不会启动。 try: from app.services.ext_presets import ensure_builtin_presets await ensure_builtin_presets(store.data_dir) except Exception as e: # noqa: BLE001 logger.warning("内置扩展表初始化失败 (不影响启动): %s", e) + # 扩展数据定时拉取: 在预设配置就绪后启动, 自动调度 enabled 的预设。 + from app.services.ext_pull import pull_scheduler + pull_scheduler.start(store.data_dir) + pull_scheduler.refresh(store.data_dir) + app.state.pull_scheduler = pull_scheduler + # 财务数据 (需 Expert 套餐): 仅初始化调度器供 /api/financials/sync/* 手动同步, # 不启动自动调度——用户在「财务分析」页点「同步」手动拉取。 from app.services.financial_sync import financial_scheduler diff --git a/backend/app/services/ext_presets.py b/backend/app/services/ext_presets.py index 6ec0e6a..c8ec7f5 100644 --- a/backend/app/services/ext_presets.py +++ b/backend/app/services/ext_presets.py @@ -6,7 +6,9 @@ - 「已存在则跳过」: 绝不覆盖用户已有数据, 老用户零影响 - 拉取失败只记 warning, 不阻断启动 (保持「没数据也能跑」) -种子数据来源: https://files.688798.xyz/ths/{concepts,industries}.json +种子数据来源 (概念/行业各自独立配置): + - 概念: https://shy313.com/api/plugins/market_flow/exports/ths-concepts + - 行业: https://shy313.com/api/plugins/market_flow/exports/ths-industries 作者更新数据只需改接口上的 JSON, 用户下次拉取自动同步, 无需发版。 接入点: app.main.lifespan → ensure_builtin_presets(store.data_dir) @@ -26,8 +28,9 @@ from app.services.ext_data import ( logger = logging.getLogger(__name__) -# 种子数据源 (作者维护, 改这里即对所有用户生效) -_THS_BASE = "https://files.688798.xyz/ths" +# 种子数据源 (概念/行业各自独立配置, 作者维护) +_CONCEPT_DATA_URL = "https://shy313.com/api/plugins/market_flow/exports/ths-concepts" +_INDUSTRY_DATA_URL = "https://shy313.com/api/plugins/market_flow/exports/ths-industries" # --------------------------------------------------------------------------- @@ -55,10 +58,10 @@ def _concept_preset() -> ExtConfig: symbol_map={"type": "mapped", "col": "股票代码"}, code_map={"type": "computed", "from": "symbol", "method": "strip_exchange"}, pull=PullConfig( - url=f"{_THS_BASE}/concepts.json", + url=_CONCEPT_DATA_URL, method="GET", schedule_minutes=1440, - enabled=False, + enabled=True, ), ) @@ -84,10 +87,10 @@ def _industry_preset() -> ExtConfig: symbol_map={"type": "mapped", "col": "股票代码"}, code_map={"type": "computed", "from": "symbol", "method": "strip_exchange"}, pull=PullConfig( - url=f"{_THS_BASE}/industries.json", + url=_INDUSTRY_DATA_URL, method="GET", schedule_minutes=1440, - enabled=False, + enabled=True, ), ) @@ -209,7 +212,7 @@ async def ensure_builtin_presets(data_dir: Path) -> None: try: store.upsert(config) logger.info("内置扩展表 %s 配置已就绪 (待用户手动获取数据)", config.id) - except Exception as e: # noqa: BLE001 + except Exception as e: logger.warning("内置扩展表 %s 配置写入失败 (不影响启动): %s", config.id, e)