部署在Google App Engine的Discord Bot每14分钟出现500错误如何解决?
问题原因
Google App Engine 平台默认要求托管的服务必须监听指定HTTP端口,响应平台发起的启动检查、健康检查类请求。你部署的Discord Bot为纯长连接服务,没有实现HTTP响应逻辑,平台判定进程启动失败,会定期终止进程触发重启,对应你观察到的每14分钟离线的现象。
可行解决方案
方案一:修改Bot代码适配GAE检查规则(推荐,改动量小)
给Bot代码增加极简HTTP服务,响应平台的检查请求即可,示例如下(以discord.py + aiohttp为例):
import os import asyncio from aiohttp import web import discord from discord.ext import commands # 替换为你原有的Bot初始化逻辑 bot = commands.Bot(command_prefix="!", intents=discord.Intents.default()) # 健康检查响应逻辑,固定返回200状态码即可 async def health_handler(request): return web.Response(status=200, text="alive") async def run_services(): # 启动HTTP服务,监听GAE自动分配的PORT端口 app = web.Application() app.add_routes([ web.get("/", health_handler), web.get("/_ah/warmup", health_handler) ]) runner = web.AppRunner(app) await runner.setup() port = int(os.environ.get("PORT", 8080)) site = web.TCPSite(runner, "0.0.0.0", port) await site.start() # 启动原有Discord Bot await bot.start("替换为你的Bot Token") if __name__ == "__main__": asyncio.run(run_services())
- 确认你的
requirements.txt文件中添加了对应HTTP库依赖,比如示例中用到aiohttp需要新增一行aiohttp>=3.8.0 - 调整
app.yaml配置,关闭不必要的warmup服务调用:
runtime: python38 instance_class: B1 manual_scaling: instances: 1 entrypoint: python3 bot.py # 禁用默认warmup请求 inbound_services: []
方案二:更换托管服务
Discord Bot属于长驻留长连接服务,也可以选择GCP更适配这类服务的产品部署,无需额外修改代码:
- 使用Cloud Run,配置始终分配CPU+最小实例数为1
- 使用Compute Engine e2-micro微型实例,直接后台运行Bot进程
内容的提问来源于stack exchange,提问作者buttermilk
相关产品推荐
相关产品推荐

