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 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() print("--- 写文件 ---") run(f"docker exec news-aggregator-worker-1 sh -c 'echo {script_b64} | base64 -d > /tmp/_t.py'") print("--- 跑 ---") run("docker exec news-aggregator-worker-1 python /tmp/_t.py", t=30) print("\n--- redis ---") 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" usage: {out.strip()}") c.close()