Files
diary-news/scripts/_check_count.py

57 lines
2.3 KiB
Python
Raw Normal View History

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: print("[err]", err, end="", file=__import__("sys").stderr)
return out
# 找一条最新抓的 article,重译
print("--- 找最新 article + 重译 ---")
out = run("docker exec news-aggregator-postgres-1 psql -U news -d news -tA -c \"SELECT id FROM articles ORDER BY fetched_at DESC LIMIT 1;\"")
aid = out.strip()
print(f" article id: {aid}")
# 改回 pending 然后触发翻译
run(f"docker exec news-aggregator-postgres-1 psql -U news -d news -c \"UPDATE articles SET translation_status = 'pending' WHERE id = {aid};\" 2>&1 | tail -2")
# 直接用 worker 调 translate
print("--- 触发翻译 ---")
script = f'''import asyncio
from app.workers.pipeline import translate_article
from app.services.translation.service import service
async def main():
# 调一次 service
res = await service.translate("Hello world, this is a test of translation.", source="en", target="zh")
print("res:", res.engine, "chars:", res.chars, "text:", res.text[:50])
# 再调一次,会走 cache
res2 = await service.translate("Hello world, this is a test of translation.", source="en", target="zh")
print("res2:", res2.engine, "cached:", res2.cached)
asyncio.run(main())
'''
print("--- 测试 service.translate ---")
import time
# 写脚本文件 + docker cp
with open("/tmp/_test_translate.py", "w", encoding="utf-8") as f:
f.write(script)
# 用 stdin
run(f"docker exec -i news-aggregator-worker-1 python -u", t=30) # 这个不通,要传脚本
# 改成 echo | 跑
quoted = script.replace('"', '\\"').replace('\\n', '\\\\n')
run(f"docker exec news-aggregator-worker-1 python -c \"{quoted}\"", t=30)
# 看 redis
print("\n--- redis usage ---")
rpw = run("grep ^REDIS_PASSWORD /srv/news/.env | cut -d= -f2").strip()
out = run(f"docker exec news-aggregator-redis-1 redis-cli -a '{rpw}' GET 'translation:month:202606' 2>&1 | grep -v Warning")
print(f" GET: {out.strip()}")
c.close()