58 lines
3.0 KiB
Python
58 lines
3.0 KiB
Python
|
|
"""重置 + 直接调 service 测 usage 链路 — 用 docker exec -i 传脚本。"""
|
||
|
|
import os, paramiko
|
||
|
|
PW = os.environ["REMOTE_PASS"]
|
||
|
|
c = paramiko.SSHClient()
|
||
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||
|
|
c.connect("207.57.129.228", port=19717, username="root", password=PW, timeout=15, allow_agent=False, look_for_keys=False)
|
||
|
|
def run(cmd, t=60):
|
||
|
|
si, so, se = c.exec_command(cmd, timeout=t)
|
||
|
|
out = so.read().decode("utf-8", "replace")
|
||
|
|
err = se.read().decode("utf-8", "replace")
|
||
|
|
rc = so.channel.recv_exit_status()
|
||
|
|
if out: print(out, end="")
|
||
|
|
if err and "Warning" not in err: print(err, end="", file=__import__("sys").stderr)
|
||
|
|
return out
|
||
|
|
|
||
|
|
rpw = run("grep ^REDIS_PASSWORD /srv/news/.env | cut -d= -f2").strip()
|
||
|
|
run("docker exec news-aggregator-redis-1 redis-cli -a '" + rpw + "' DEL translation:month:202606 2>/dev/null")
|
||
|
|
print("--- usage 重置 0 ---")
|
||
|
|
|
||
|
|
# 用 stdin 喂脚本
|
||
|
|
script = '''import asyncio
|
||
|
|
from app.services.translation.service import service
|
||
|
|
from app.redis_client import get_redis
|
||
|
|
async def main():
|
||
|
|
r = get_redis(); await r.ping()
|
||
|
|
print("before:", await r.get("translation:month:202606") or 0, flush=True)
|
||
|
|
res1 = await service.translate("Breaking news from Reuters today.", source="en", target="zh")
|
||
|
|
print(" call 1: engine=", res1.engine, "chars=", res1.chars, "text=", res1.text[:40], flush=True)
|
||
|
|
print("after 1:", await r.get("translation:month:202606") or 0, flush=True)
|
||
|
|
res2 = await service.translate("The market fell sharply after the announcement.", source="en", target="zh")
|
||
|
|
print(" call 2: engine=", res2.engine, "chars=", res2.chars, flush=True)
|
||
|
|
print("after 2:", await r.get("translation:month:202606") or 0, flush=True)
|
||
|
|
res3 = await service.translate("Breaking news from Reuters today.", source="en", target="zh")
|
||
|
|
print(" call 3 (cache): cached=", res3.cached, "engine=", res3.engine, flush=True)
|
||
|
|
print("after 3:", await r.get("translation:month:202606") or 0, flush=True)
|
||
|
|
asyncio.run(main())
|
||
|
|
'''
|
||
|
|
# 写到 worker 容器内的 /app 目录
|
||
|
|
# docker exec -i 把脚本从 stdin 写入
|
||
|
|
run("docker exec -i -w /app news-aggregator-worker-1 sh -c 'cat > /app/_t.py' 2>/dev/null", t=5) # 这个会 hang
|
||
|
|
|
||
|
|
# 改:用 docker exec 的 stdin (paramiko 可以发 stdin)
|
||
|
|
si, so, se = c.exec_command("docker exec -i -w /app news-aggregator-worker-1 sh -c 'cat > /app/_t.py && python /app/_t.py'", timeout=30)
|
||
|
|
si.sendall(script.encode("utf-8"))
|
||
|
|
si.channel.shutdown_write() # 关闭 stdin 告诉 docker 没更多输入
|
||
|
|
out = so.read().decode("utf-8", "replace")
|
||
|
|
err = se.read().decode("utf-8", "replace")
|
||
|
|
print(f"--- 跑 ---\n{out}")
|
||
|
|
if err and "Warning" not in err: print("err:", err)
|
||
|
|
|
||
|
|
# /me/usage
|
||
|
|
out = run("curl -s -X POST http://localhost/api/v1/auth/login -H 'Content-Type: application/json' -d '{\"username\":\"owner\",\"password\":\"Owner2026!\"}'")
|
||
|
|
token = json.loads(out)["access_token"]
|
||
|
|
u = json.loads(run("curl -s -H 'Authorization: Bearer " + token + "' 'http://localhost/api/v1/me/usage'"))
|
||
|
|
print("\n--- /me/usage ---")
|
||
|
|
print(" ", u)
|
||
|
|
c.close()
|