Files
diary-news/scripts/_direct2.py

56 lines
2.9 KiB
Python
Raw Normal View History

"""重置 + 直接调 service 测 usage 链路。
实现: paramiko 写脚本到容器临时文件,然后 docker exec """
import os, paramiko, base64, json
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 ---")
# 在本机写脚本,scp 到容器(不行,容器是 worker 容器,用 docker cp)
script = (
"import asyncio\n"
"from app.services.translation.service import service\n"
"from app.redis_client import get_redis\n"
"async def main():\n"
" r = get_redis(); await r.ping()\n"
" print('before:', await r.get('translation:month:202606') or 0, flush=True)\n"
" res1 = await service.translate('Breaking news from Reuters today.', source='en', target='zh')\n"
" print(' call 1: engine=', res1.engine, 'chars=', res1.chars, 'text=', res1.text[:40], flush=True)\n"
" print('after 1:', await r.get('translation:month:202606') or 0, flush=True)\n"
" res2 = await service.translate('The market fell sharply after the announcement.', source='en', target='zh')\n"
" print(' call 2: engine=', res2.engine, 'chars=', res2.chars, flush=True)\n"
" print('after 2:', await r.get('translation:month:202606') or 0, flush=True)\n"
" res3 = await service.translate('Breaking news from Reuters today.', source='en', target='zh')\n"
" print(' call 3 (cache): cached=', res3.cached, 'engine=', res3.engine, flush=True)\n"
" print('after 3:', await r.get('translation:month:202606') or 0, flush=True)\n"
"asyncio.run(main())\n"
)
local_path = "D:/selftools/diary-news/scripts/_t_direct.py"
with open(local_path, "w", encoding="utf-8") as f:
f.write(script)
# docker cp 进 worker 容器
run("docker cp " + local_path + " news-aggregator-worker-1:/app/_td.py")
print("--- 跑 ---")
run("docker exec -w /app news-aggregator-worker-1 python /app/_td.py 2>&1 | tail -15", t=30)
# /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()