54 lines
2.3 KiB
Python
54 lines
2.3 KiB
Python
|
|
import os, paramiko, 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=180):
|
||
|
|
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
|
||
|
|
|
||
|
|
# 写一个 python 脚本到 worker 容器(用 stdin pipe),直接翻译所有 pending
|
||
|
|
script = '''import asyncio
|
||
|
|
from app.workers.pipeline import translate_article
|
||
|
|
from app.database import AsyncSessionLocal
|
||
|
|
from app.models.article import Article
|
||
|
|
from sqlalchemy import select
|
||
|
|
|
||
|
|
async def main():
|
||
|
|
async with AsyncSessionLocal() as s:
|
||
|
|
rows = (await s.execute(select(Article.id).where(Article.translation_status == 'pending').order_by(Article.id))).all()
|
||
|
|
ids = [r[0] for r in rows]
|
||
|
|
print(f"translating {len(ids)} articles...")
|
||
|
|
for i, aid in enumerate(ids, 1):
|
||
|
|
try:
|
||
|
|
await translate_article(aid)
|
||
|
|
except Exception as e:
|
||
|
|
print(f" err on {aid}: {e}")
|
||
|
|
if i % 10 == 0:
|
||
|
|
print(f" {i}/{len(ids)} done")
|
||
|
|
asyncio.run(main())
|
||
|
|
'''
|
||
|
|
# 用 docker exec -i 把脚本传进去
|
||
|
|
print("--- 翻译所有 pending ---")
|
||
|
|
run(f"docker exec -i news-aggregator-worker-1 python -u -c \"{script.replace(chr(34), chr(92)+chr(34))}\"", t=600)
|
||
|
|
|
||
|
|
print("\n--- 翻译后统计 ---")
|
||
|
|
run("docker exec news-aggregator-postgres-1 psql -U news -d news -c \"SELECT translation_status, translation_engine, count(*), sum(translation_chars) FROM articles GROUP BY 1, 2 ORDER BY 1, 2;\"")
|
||
|
|
|
||
|
|
# 看 usage
|
||
|
|
import urllib.request
|
||
|
|
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}")
|
||
|
|
|
||
|
|
# 看 redis
|
||
|
|
print("\n--- redis 计数 ---")
|
||
|
|
run("docker exec news-aggregator-redis-1 redis-cli -a '$(grep ^REDIS_PASSWORD /srv/news/.env | cut -d= -f2)' GET translation:month:202606 2>&1 | tail -3")
|
||
|
|
c.close()
|