init
This commit is contained in:
41
utils/designer_roster.py
Normal file
41
utils/designer_roster.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
设计师在线状态 - 请求外部查询服务(转人工时按需调用)
|
||||
|
||||
接口: GET /online 返回 {online_users: ["lz", "ZuoWei"], ...}
|
||||
本端用 online_users 同步本地 designer_online 表后派单。
|
||||
"""
|
||||
import os
|
||||
import logging
|
||||
from typing import List, Set
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 设计师在线查询服务,.env 配置 DESIGNER_ROSTER_API,如 http://huichang.online:8001/online
|
||||
_API_URL = os.getenv("DESIGNER_ROSTER_API", "")
|
||||
|
||||
|
||||
async def _fetch_online_users() -> Set[str]:
|
||||
"""请求 GET /online,返回当前在线用户的 user_id 集合"""
|
||||
if not _API_URL:
|
||||
return set()
|
||||
try:
|
||||
import httpx
|
||||
async with httpx.AsyncClient(timeout=10) as client:
|
||||
r = await client.get(_API_URL)
|
||||
r.raise_for_status()
|
||||
data = r.json()
|
||||
users = data.get("online_users", [])
|
||||
return set(users) if isinstance(users, list) else set()
|
||||
except Exception as e:
|
||||
logger.debug(f"设计师在线查询失败: {e}")
|
||||
return set()
|
||||
|
||||
|
||||
async def poll_and_update_roster() -> None:
|
||||
"""请求查询服务,同步在线状态到本地数据库"""
|
||||
from db.designer_roster_db import update_online, get_all_wechat_user_ids
|
||||
online_users = await _fetch_online_users()
|
||||
all_ids = get_all_wechat_user_ids()
|
||||
for uid in all_ids:
|
||||
update_online(uid, uid in online_users)
|
||||
Reference in New Issue
Block a user