fix: API 全部改用显式两步走 await session.execute + result.scalars()

之前 (await ...).scalars() 链式在 SQLAlchemy 2.0 async 下报
'coroutine' has no attribute 'scalars' 错误。改为先 await 拿 result
再 .scalars(),这是 SQLAlchemy 2.0 推荐的 async 写法。
This commit is contained in:
Mavis
2026-06-07 23:22:56 +08:00
parent 2e75985a3c
commit 5109d6f824
8 changed files with 147 additions and 36 deletions

53
scripts/_smoke_test.py Normal file
View File

@@ -0,0 +1,53 @@
import os, paramiko, 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=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()
print(f"$ {cmd}")
if out: print(out, end="")
if err: print("[err]", err, end="", file=__import__("sys").stderr)
print(f" rc={rc}")
return out
# 1) 取 owner 密码
owner_pw = run("cat /root/.owner_pass").strip()
print(f"\n=== owner 密码: {owner_pw} ===\n")
# 2) 登录拿 token
login_cmd = f"""curl -s -X POST http://localhost/api/v1/auth/login -H 'Content-Type: application/json' -d '{{"username":"owner","password":"{owner_pw}"}}'"""
login_resp = run(login_cmd)
token = json.loads(login_resp).get("access_token", "")
print(f"\n=== token(前 40): {token[:40]}... ===\n")
# 3) 触发一次抓取
print("=== 触发一次抓取 ===")
run("cd /srv/news && sg docker -c \"docker compose exec -T worker python -c 'import asyncio; from app.workers.pipeline import run_once; asyncio.run(run_once())'\" 2>&1 | tail -30", t=180)
# 4) 等翻译完成,看文章数
time.sleep(10)
print("\n=== 查文章数 ===")
run("cd /srv/news && sg docker -c \"docker compose exec -T postgres psql -U news -d news -c 'SELECT count(*) FROM articles; SELECT count(*) FROM articles WHERE translation_status = '\\''ok'\\'';'\" 2>&1 | tail -10")
# 5) 拿一条翻译好的文章
print("\n=== 拉一条文章 ===")
list_cmd = f"curl -s -H 'Authorization: Bearer {token}' http://localhost/api/v1/articles?limit=1"
art_resp = run(list_cmd)
try:
data = json.loads(art_resp)
items = data.get("items", [])
if items:
art = items[0]
print(f" id: {art['id']}")
print(f" source: {art['source']['name']}")
print(f" title (en): {art['title'][:80]}")
print(f" title (zh): {art.get('title_zh', '(none)')[:80] if art.get('title_zh') else '(none)'}")
print(f" status: {art['translation_status']}")
except Exception as e:
print(f"parse err: {e}\n raw: {art_resp[:300]}")
c.close()