Files
diary-news/scripts/_direct_test.py

55 lines
2.6 KiB
Python
Raw Normal View History

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: print("[err]", err, end="", file=__import__("sys").stderr")
return out
rpw = run("grep ^REDIS_PASSWORD /srv/news/.env | cut -d= -f2").strip()
# 重置
run(f"docker exec news-aggregator-redis-1 redis-cli -a '{rpw}' DEL 'translation:month:202606' 2>&1 | grep -v Warning")
print("--- usage 重置 0 ---")
# 在 worker 进程内直接调 service.translate 两次(确认链路)
script_b64 = base64.b64encode(b'''
import asyncio, sys
from app.services.translation.service import service
from app.redis_client import get_redis
async def main():
r = get_redis()
await r.ping()
print(f"before: {await r.get('translation:month:202606') or 0}", flush=True)
# 1) 全新字符串 -> 走 tencent
res1 = await service.translate("Breaking news from Reuters today.", source="en", target="zh")
print(f" call 1: engine={res1.engine} chars={res1.chars} text={res1.text[:40]!r}", flush=True)
print(f"after 1: {await r.get('translation:month:202606') or 0}", flush=True)
# 2) 另一段
res2 = await service.translate("The market fell sharply after the announcement.", source="en", target="zh")
print(f" call 2: engine={res2.engine} chars={res2.chars}", flush=True)
print(f"after 2: {await r.get('translation:month:202606') or 0}", flush=True)
# 3) 重复 1 的文本 -> 走 cache
res3 = await service.translate("Breaking news from Reuters today.", source="en", target="zh")
print(f" call 3 (cache): cached={res3.cached} engine={res3.engine}", flush=True)
print(f"after 3: {await r.get('translation:month:202606') or 0}", flush=True)
asyncio.run(main())
''').decode()
run(f"docker exec news-aggregator-worker-1 sh -c 'echo {script_b64} | base64 -d > /app/_tt2.py'")
print("--- worker 跑 ---")
run("docker exec -w /app news-aggregator-worker-1 python /app/_tt2.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(f"curl -s -H 'Authorization: Bearer {token}' 'http://localhost/api/v1/me/usage'"))
print(f"\n--- /me/usage ---\n {u}")
c.close()