This commit is contained in:
ddrwode
2026-03-03 13:02:13 +08:00
parent 5fa922f1d8
commit ed77233f2c

View File

@@ -9,7 +9,7 @@ from asgiref.sync import sync_to_async
from channels.generic.websocket import AsyncWebsocketConsumer
from common.protocol import MsgType, TaskStatus, make_msg
from common.protocol import MsgType, TaskStatus, TaskType, make_msg
from server.core.worker_manager import worker_manager
logger = logging.getLogger("server.ws")
@@ -124,8 +124,13 @@ class WorkerConsumer(AsyncWebsocketConsumer):
if final_status:
await self._update_account_task_status(task_id, final_status)
logger.info("任务 %s 最终状态已更新: %s", task_id, final_status)
# check_login 任务:更新账号登录状态(仅在成功时)
if result and final_status == TaskStatus.SUCCESS.value:
# check_login 成功时更新账号登录状态
task_type = await self._get_task_type(task_id)
if (
result
and final_status == TaskStatus.SUCCESS.value
and task_type == TaskType.CHECK_LOGIN.value
):
await self._upsert_account_status(result)
except Exception as db_err:
logger.error("任务 %s 写入数据库失败: %s", task_id, db_err)
@@ -229,15 +234,27 @@ class WorkerConsumer(AsyncWebsocketConsumer):
from server.models import BossAccount
BossAccount.objects.filter(current_task_id=task_id).update(current_task_status=task_status)
@staticmethod
@sync_to_async
def _get_task_type(task_id):
from server.models import Task
task = Task.objects.filter(task_id=task_id).only("task_type").first()
return task.task_type if task else None
@sync_to_async
def _upsert_account_status(self, result):
from server.models import BossAccount
from django.utils import timezone as tz
browser_id = result.get("browser_id", "")
browser_name = result.get("browser_name", "")
boss_username = result.get("boss_username", "")
boss_id = result.get("boss_id", "")
is_logged_in = result.get("is_logged_in", False)
browser_id = str(result.get("browser_id", "") or "").strip()
browser_name = str(result.get("browser_name", "") or "").strip()
boss_username = str(result.get("boss_username", "") or "").strip()
boss_id = str(result.get("boss_id", "") or "").strip()
is_logged_in = bool(result.get("is_logged_in", False))
if not browser_id and not browser_name:
logger.warning("跳过账号状态更新browser_id 与 browser_name 均为空worker=%s", self.worker_id)
return
# 优先按 worker_id + browser_name 匹配
account = None
@@ -261,6 +278,9 @@ class WorkerConsumer(AsyncWebsocketConsumer):
account.checked_at = now
account.save()
else:
if not browser_id and not browser_name:
logger.warning("跳过创建账号browser_id 与 browser_name 均为空worker=%s", self.worker_id)
return
BossAccount.objects.create(
worker_id=self.worker_id,
browser_id=browser_id or f"name:{browser_name}",