fix: articles.py get_article 链式 await coroutine 报错(.first())
This commit is contained in:
67
scripts/_final5.py
Normal file
67
scripts/_final5.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import os, paramiko, base64, json, time
|
||||
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=120):
|
||||
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("docker exec news-aggregator-postgres-1 psql -U news -d news -c \"UPDATE articles SET translation_status='pending' WHERE id IN (SELECT id FROM articles WHERE translation_status='ok' ORDER BY id LIMIT 3);\" 2>&1 | tail -2")
|
||||
|
||||
# 等
|
||||
time.sleep(3)
|
||||
|
||||
# 查 pending
|
||||
out = run("docker exec news-aggregator-postgres-1 psql -U news -d news -tA -c \"SELECT count(*) FROM articles WHERE translation_status='pending';\"")
|
||||
print(f"pending articles: {out.strip()}")
|
||||
|
||||
# 重置 usage
|
||||
run(f"docker exec news-aggregator-redis-1 redis-cli -a '{rpw}' DEL 'translation:month:202606' 2>&1 | grep -v Warning")
|
||||
|
||||
# 跑 worker 重译
|
||||
script_b64 = base64.b64encode(b'''
|
||||
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).where(Article.translation_status=='pending').limit(5))).all()
|
||||
for r in rows: r[0]
|
||||
ids = [r[0].id for r in rows]
|
||||
print(f"translating {len(ids)}")
|
||||
for aid in ids:
|
||||
try:
|
||||
await translate_article(aid)
|
||||
except Exception as e:
|
||||
print(f" err on {aid}: {e}")
|
||||
print("done")
|
||||
asyncio.run(main())
|
||||
''').decode()
|
||||
run(f"docker exec news-aggregator-worker-1 sh -c 'echo {script_b64} | base64 -d > /app/_tt.py'")
|
||||
run("docker exec -w /app news-aggregator-worker-1 python /app/_tt.py 2>&1 | tail -20", t=180)
|
||||
|
||||
# 看 usage
|
||||
out = run(f"docker exec news-aggregator-redis-1 redis-cli -a '{rpw}' GET 'translation:month:202606' 2>&1 | grep -v Warning")
|
||||
print(f"\n--- redis usage: {out.strip()}")
|
||||
|
||||
# /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"--- /me/usage: {u}")
|
||||
|
||||
# 翻译后统计
|
||||
run("docker exec news-aggregator-postgres-1 psql -U news -d news -c \"SELECT translation_status, count(*) FROM articles GROUP BY 1 ORDER BY 1;\"")
|
||||
c.close()
|
||||
Reference in New Issue
Block a user