refactor: refactor of backend
This commit is contained in:
1 parent
d4cb8c5def
commit
14e5af0c9c
55 files changed
+2251
-1805
No files matched your search
@@ -3,7 +3,8 @@ backend/app/services/fixed_assets_snapshot.py
|
||||
|
||||
用途:提供固定资产快照同步、本地结构化落地、Excel 导出与本地查询能力。
|
||||
依赖:Flask current_app、openpyxl、sqlite3、Text2SQLService(延迟导入)、ERP Oracle。
|
||||
逻辑关系:该服务负责将 ERP 固资实时查询能力沉淀为本地快照,供 text2sql 在固资场景下优先查询。
|
||||
逻辑:同步主循环由独立进程 workers/worker_fixed_assets.py 运行;Web 进程不再起线程。
|
||||
text2sql 固资场景优先查本地快照。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -24,8 +25,6 @@ from openpyxl import Workbook
|
||||
|
||||
from app.path_utils import backend_dir
|
||||
|
||||
_worker_started = False
|
||||
_worker_lock = threading.Lock()
|
||||
_sync_lock = threading.Lock()
|
||||
|
||||
|
||||
@@ -970,13 +969,32 @@ SELECT COUNT(*) FROM {result_source_name} WHERE {where_sql}
|
||||
scheduled = scheduled + timedelta(days=1)
|
||||
return scheduled
|
||||
|
||||
@classmethod
|
||||
def run_worker_forever(cls, app) -> None:
|
||||
"""
|
||||
在独立进程中运行固资快照同步主循环(阻塞)。
|
||||
|
||||
核心逻辑:若本地无快照且开启 boot 补齐则先 bootstrap;之后按配置时区每日定点 sync_with_retry。
|
||||
参数:
|
||||
app: Flask 应用(须已 create_app);本方法内部进入 app_context。
|
||||
"""
|
||||
cls._worker_loop(app)
|
||||
|
||||
@classmethod
|
||||
def _worker_loop(cls, app) -> None:
|
||||
with app.app_context():
|
||||
if not bool(app.config.get("FIXED_ASSETS_SYNC_ENABLED", True)):
|
||||
cls._log("info", "Fixed assets snapshot worker disabled by config; exiting")
|
||||
return
|
||||
timezone_name = str(app.config.get("FIXED_ASSETS_SYNC_TIMEZONE") or "Asia/Shanghai").strip()
|
||||
schedule_hour = int(app.config.get("FIXED_ASSETS_SYNC_HOUR", 3) or 3)
|
||||
schedule_minute = int(app.config.get("FIXED_ASSETS_SYNC_MINUTE", 0) or 0)
|
||||
poll_seconds = max(30, int(app.config.get("FIXED_ASSETS_SYNC_POLL_SECONDS", 60) or 60))
|
||||
cls._log(
|
||||
"info",
|
||||
f"Fixed assets snapshot worker loop started tz={timezone_name} "
|
||||
f"daily={schedule_hour:02d}:{schedule_minute:02d} poll={poll_seconds}s",
|
||||
)
|
||||
if cls._should_run_bootstrap():
|
||||
try:
|
||||
cls.sync_with_retry(reason="bootstrap")
|
||||
@@ -996,23 +1014,15 @@ SELECT COUNT(*) FROM {result_source_name} WHERE {where_sql}
|
||||
|
||||
@classmethod
|
||||
def start_worker(cls, app) -> None:
|
||||
"""启动固定资产快照后台同步线程。"""
|
||||
global _worker_started
|
||||
if not bool(app.config.get("FIXED_ASSETS_SYNC_ENABLED", True)):
|
||||
app.logger.info("Fixed assets snapshot worker disabled by config")
|
||||
return
|
||||
with _worker_lock:
|
||||
if _worker_started:
|
||||
return
|
||||
thread = threading.Thread(
|
||||
target=cls._worker_loop,
|
||||
args=(app,),
|
||||
name="fixed-assets-snapshot-worker",
|
||||
daemon=True,
|
||||
)
|
||||
thread.start()
|
||||
_worker_started = True
|
||||
app.logger.info("Fixed assets snapshot worker started")
|
||||
"""
|
||||
已废弃:请使用独立进程 workers/worker_fixed_assets.py。
|
||||
|
||||
保留方法签名以免外部脚本误调崩溃;仅打日志,不再在 Web 进程起线程。
|
||||
"""
|
||||
app.logger.warning(
|
||||
"FixedAssetsSnapshotService.start_worker is deprecated; "
|
||||
"run workers/worker_fixed_assets.py instead (no in-process thread)"
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _log(level: str, message: str) -> None:
|
||||
|
||||
Reference in new issue
Block a user