MCPcopy Create free account
hub / github.com/ZyphrZero/z.ai2api_python / sync_token_pool

Function sync_token_pool

app/admin/api.py:1069–1111  ·  view source on GitHub ↗

手动同步 Token 池(从数据库重新加载)

()

Source from the content-addressed store, hash-verified

1067
1068@router.post("/tokens/sync-pool")
1069async def sync_token_pool():
1070 """手动同步 Token 池(从数据库重新加载)"""
1071 from app.utils.token_pool import get_token_pool
1072
1073 pool = get_token_pool()
1074
1075 if not pool:
1076 return HTMLResponse("""
1077 <div class="bg-yellow-100 border border-yellow-400 text-yellow-700 px-4 py-3 rounded relative" role="alert">
1078 <strong class="font-bold">提示!</strong>
1079 <span class="block sm:inline">Token 池未初始化,请重启服务。</span>
1080 </div>
1081 """)
1082
1083 # 从数据库同步
1084 await pool.sync_from_database(DEFAULT_TOKEN_NAMESPACE)
1085
1086 # 获取同步后的状态
1087 status = pool.get_pool_status()
1088 total_count = status.get("total_tokens", 0)
1089 available_count = status.get("available_tokens", 0)
1090 user_count = status.get("user_tokens", 0)
1091
1092 logger.info(
1093 f"✅ Token 池手动同步完成,总计 {total_count} 个 Token, 可用 {available_count} 个, 认证用户 {user_count} 个"
1094 )
1095
1096 if total_count == 0:
1097 message_class = "bg-yellow-100 border-yellow-400 text-yellow-700"
1098 message = "同步完成:当前没有可用 Token,请在数据库中启用 Token。"
1099 elif available_count == 0:
1100 message_class = "bg-orange-100 border-orange-400 text-orange-700"
1101 message = f"同步完成:共 {total_count} 个 Token,但无可用 Token(可能都已禁用)。"
1102 else:
1103 message_class = "bg-green-100 border-green-400 text-green-700"
1104 message = f"同步完成:共 {total_count} 个 Token,{available_count} 个可用,{user_count} 个认证用户。"
1105
1106 return HTMLResponse(f"""
1107 <div class="{message_class} border px-4 py-3 rounded relative" role="alert">
1108 <strong class="font-bold">Token 池同步完成!</strong>
1109 <span class="block sm:inline">{message}</span>
1110 </div>
1111 """)

Callers

nothing calls this directly

Calls 3

get_token_poolFunction · 0.90
sync_from_databaseMethod · 0.80
get_pool_statusMethod · 0.45

Tested by

no test coverage detected