同步更新:重构路由、服务模块,更新前端构建

This commit is contained in:
2025-12-14 21:47:08 +08:00
parent e01a7b5235
commit a346509a5f
87 changed files with 9186 additions and 7826 deletions

3
realtime/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

View File

@@ -0,0 +1,38 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations
from flask_login import current_user
from flask_socketio import emit, join_room, leave_room
from services.accounts_service import load_user_accounts
from services.client_log import log_to_client
from services.state import safe_get_user_accounts_snapshot, safe_get_user_logs
def register_socketio_handlers(socketio) -> None:
@socketio.on("connect")
def handle_connect():
"""客户端连接"""
if current_user.is_authenticated:
user_id = current_user.id
join_room(f"user_{user_id}")
log_to_client("客户端已连接", user_id)
accounts = safe_get_user_accounts_snapshot(user_id)
if not accounts:
load_user_accounts(user_id)
accounts = safe_get_user_accounts_snapshot(user_id)
emit("accounts_list", [acc.to_dict() for acc in accounts.values()])
for log_entry in safe_get_user_logs(user_id):
emit("log", log_entry)
@socketio.on("disconnect")
def handle_disconnect():
"""客户端断开"""
if current_user.is_authenticated:
user_id = current_user.id
leave_room(f"user_{user_id}")

56
realtime/status_push.py Normal file
View File

@@ -0,0 +1,56 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations
import os
import time
from services.runtime import get_logger, get_socketio
from services.state import safe_get_account, safe_iter_task_status_items
def status_push_worker() -> None:
"""后台线程:按间隔推送运行中任务的状态更新(可节流)。"""
logger = get_logger()
try:
push_interval = float(os.environ.get("STATUS_PUSH_INTERVAL_SECONDS", "2"))
except Exception:
push_interval = 2.0
push_interval = max(0.5, push_interval)
socketio = get_socketio()
while True:
try:
status_items = safe_iter_task_status_items()
for account_id, status_info in status_items:
if status_info.get("status") != "运行中":
continue
user_id = status_info.get("user_id")
if not user_id:
continue
account = safe_get_account(user_id, account_id)
if not account:
continue
account_data = account.to_dict()
socketio.emit("account_update", account_data, room=f"user_{user_id}")
progress = status_info.get("progress", {}) or {}
progress_data = {
"account_id": account_id,
"stage": status_info.get("detail_status", ""),
"total_items": account.total_items,
"browsed_items": progress.get("items", 0),
"total_attachments": account.total_attachments,
"viewed_attachments": progress.get("attachments", 0),
"start_time": status_info.get("start_time", 0),
"elapsed_seconds": account_data.get("elapsed_seconds", 0),
"elapsed_display": account_data.get("elapsed_display", ""),
}
socketio.emit("task_progress", progress_data, room=f"user_{user_id}")
time.sleep(push_interval)
except Exception as e:
logger.debug(f"状态推送出错: {e}")
time.sleep(push_interval)