fix: 账号截图开关持久化与状态推送优化
This commit is contained in:
@@ -4,16 +4,27 @@ from __future__ import annotations
|
||||
|
||||
import database
|
||||
from services.models import Account
|
||||
from services.state import safe_set_user_accounts
|
||||
from services.state import safe_get_user_accounts_snapshot, safe_set_user_accounts
|
||||
|
||||
|
||||
def load_user_accounts(user_id: int) -> None:
|
||||
"""从数据库加载用户的账号到内存(保持原逻辑不变)。"""
|
||||
existing_accounts = safe_get_user_accounts_snapshot(user_id)
|
||||
accounts_by_id = {}
|
||||
accounts_data = database.get_user_accounts(user_id)
|
||||
for acc_data in accounts_data:
|
||||
account_id = acc_data["id"]
|
||||
existing = existing_accounts.get(account_id)
|
||||
if isinstance(existing, Account):
|
||||
existing.username = acc_data["username"]
|
||||
existing._password = acc_data["password"]
|
||||
existing.remember = bool(acc_data["remember"])
|
||||
existing.remark = acc_data["remark"] or ""
|
||||
accounts_by_id[existing.id] = existing
|
||||
continue
|
||||
|
||||
account = Account(
|
||||
account_id=acc_data["id"],
|
||||
account_id=account_id,
|
||||
user_id=user_id,
|
||||
username=acc_data["username"],
|
||||
password=acc_data["password"],
|
||||
@@ -23,4 +34,3 @@ def load_user_accounts(user_id: int) -> None:
|
||||
accounts_by_id[account.id] = account
|
||||
|
||||
safe_set_user_accounts(user_id, accounts_by_id)
|
||||
|
||||
|
||||
@@ -180,6 +180,23 @@ class TaskScheduler:
|
||||
|
||||
self._cond.notify_all()
|
||||
|
||||
def get_queue_state_snapshot(self) -> dict:
|
||||
"""获取调度器队列/运行状态快照(用于前端展示/监控)。"""
|
||||
with self._cond:
|
||||
pending_tasks = [t for t in self._pending_by_account.values() if t and not t.canceled]
|
||||
pending_tasks.sort(key=lambda t: (0 if t.is_vip else 1, t.submitted_at, t.seq))
|
||||
|
||||
positions = {}
|
||||
for idx, t in enumerate(pending_tasks):
|
||||
positions[t.account_id] = {"queue_position": idx + 1, "queue_ahead": idx, "is_vip": bool(t.is_vip)}
|
||||
|
||||
return {
|
||||
"pending_total": len(pending_tasks),
|
||||
"running_total": int(self._running_global),
|
||||
"running_by_user": dict(self._running_by_user),
|
||||
"positions": positions,
|
||||
}
|
||||
|
||||
def submit_task(
|
||||
self,
|
||||
user_id: int,
|
||||
|
||||
Reference in New Issue
Block a user