Files
diary-news/scripts/_check_count3.py

47 lines
2.2 KiB
Python
Raw Normal View History

import os, paramiko, base64
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
# 重置 usage 到 0(我之前测试加了 100)
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")
# 写脚本到 /app/_t.py
script = (
"import asyncio, logging\n"
"from app.services.translation.service import service\n"
"from app.redis_client import get_redis\n"
"logging.basicConfig(level=logging.INFO)\n"
"async def main():\n"
" r = get_redis(); await r.ping()\n"
" print('before:', await r.get('translation:month:202606') or 0)\n"
" res = await service.translate('Hello, world. This is a short test message.', source='en', target='zh')\n"
" print(' result engine=', res.engine, 'chars=', res.chars, 'text=', res.text[:60])\n"
" print('after:', await r.get('translation:month:202606') or 0)\n"
" res2 = await service.translate('Hello, world. This is a short test message.', source='en', target='zh')\n"
" print(' cached:', res2.cached, 'engine=', res2.engine)\n"
" print('after cache:', await r.get('translation:month:202606') or 0)\n"
"asyncio.run(main())\n"
)
script_b64 = base64.b64encode(script.encode()).decode()
run(f"docker exec news-aggregator-worker-1 sh -c 'echo {script_b64} | base64 -d > /app/_t.py && cat /app/_t.py | head -3'")
print("\n--- 跑(在 /app 下)---")
run("docker exec -w /app news-aggregator-worker-1 python /app/_t.py", t=30)
print("\n--- redis ---")
out = run(f"docker exec news-aggregator-redis-1 redis-cli -a '{rpw}' GET 'translation:month:202606' 2>&1 | grep -v Warning")
print(f" usage: {out.strip()}")
c.close()