fix: 账号截图开关持久化与状态推送优化

This commit is contained in:
2025-12-16 18:27:45 +08:00
parent e699f4fb94
commit 2abb9ab494
19 changed files with 127 additions and 48 deletions

View File

@@ -10,21 +10,31 @@ 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"))
push_interval = float(os.environ.get("STATUS_PUSH_INTERVAL_SECONDS", "1"))
except Exception:
push_interval = 2.0
push_interval = 1.0
push_interval = max(0.5, push_interval)
socketio = get_socketio()
from services.tasks import get_task_scheduler
scheduler = get_task_scheduler()
while True:
try:
queue_snapshot = scheduler.get_queue_state_snapshot()
pending_total = int(queue_snapshot.get("pending_total", 0) or 0)
running_total = int(queue_snapshot.get("running_total", 0) or 0)
running_by_user = queue_snapshot.get("running_by_user") or {}
positions = queue_snapshot.get("positions") or {}
status_items = safe_iter_task_status_items()
for account_id, status_info in status_items:
if status_info.get("status") != "运行中":
status = status_info.get("status")
if status not in ("运行中", "排队中"):
continue
user_id = status_info.get("user_id")
if not user_id:
@@ -33,8 +43,22 @@ def status_push_worker() -> None:
if not account:
continue
account_data = account.to_dict()
pos = positions.get(account_id) or {}
account_data.update(
{
"queue_pending_total": pending_total,
"queue_running_total": running_total,
"queue_ahead": pos.get("queue_ahead"),
"queue_position": pos.get("queue_position"),
"queue_is_vip": pos.get("is_vip"),
"queue_running_user": int(running_by_user.get(int(user_id), 0) or 0),
}
)
socketio.emit("account_update", account_data, room=f"user_{user_id}")
if status != "运行中":
continue
progress = status_info.get("progress", {}) or {}
progress_data = {
"account_id": account_id,
@@ -46,6 +70,11 @@ def status_push_worker() -> None:
"start_time": status_info.get("start_time", 0),
"elapsed_seconds": account_data.get("elapsed_seconds", 0),
"elapsed_display": account_data.get("elapsed_display", ""),
"queue_pending_total": pending_total,
"queue_running_total": running_total,
"queue_ahead": pos.get("queue_ahead"),
"queue_position": pos.get("queue_position"),
"queue_running_user": int(running_by_user.get(int(user_id), 0) or 0),
}
socketio.emit("task_progress", progress_data, room=f"user_{user_id}")
@@ -53,4 +82,3 @@ def status_push_worker() -> None:
except Exception as e:
logger.debug(f"状态推送出错: {e}")
time.sleep(push_interval)