You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Web App部署Telegram Bot启动超时问题求助

问题排查与解决方案

1. 端口配置不一致问题

日志显示Azure在对8000端口发送健康探测,但你设置的WEBSITES_PORT=8080与Gunicorn启动命令gunicorn -b 0.0.0.0:8000 app:app的监听端口不匹配,这是启动超时的核心原因之一。

  • 修正方式(二选一):
    • 修改启动命令,统一端口为8080:gunicorn -b 0.0.0.0:8080 app:app
    • 修改环境变量WEBSITES_PORT=8000,匹配Gunicorn监听端口

2. Gunicorn进程超时与阻塞问题

日志提到Gunicorn启动10分钟后终止工作进程,大概率是默认超时设置过短,或Flask应用存在阻塞操作导致健康检查无响应。

  • 优化Gunicorn启动参数,增强稳定性:
    gunicorn -b 0.0.0.0:8080 --timeout 120 --workers 2 --threads 4 app:app
    
    • --timeout 120:将工作进程超时时间延长至120秒,避免无响应被强制终止
    • --workers 2 --threads 4:根据Web App资源配置调整进程/线程数,保证有空闲进程处理健康探测
  • 检查Telegram Bot运行模式:如果使用updater.start_polling()的轮询模式,该操作会阻塞Gunicorn工作进程,导致无法响应Azure健康检查。必须切换为Webhook模式:
    from flask import Flask, request
    import telegram
    
    app = Flask(__name__)
    bot = telegram.Bot(token="YOUR_TELEGRAM_TOKEN")
    
    @app.route('/webhook', methods=['POST'])
    def handle_webhook():
        update = telegram.Update.de_json(request.get_json(force=True), bot)
        # 编写你的消息处理逻辑
        return 'ok'
    
    if __name__ == '__main__':
        # 设置Webhook,替换为你的Web App域名
        bot.set_webhook(url='https://your-app-name.azurewebsites.net/webhook')
        app.run(host='0.0.0.0', port=8080)
    

3. 健康检查路由配置

Azure默认对根路径/发送健康探测,如果你的Flask应用未配置该路由,会返回404导致探测失败。

  • 添加简单的健康检查路由:
    @app.route('/')
    def health_check():
        return 'OK', 200
    

4. 深度日志排查

添加Gunicorn日志参数,获取更详细的运行信息,定位内部错误:

gunicorn -b 0.0.0.0:8080 --timeout 120 --workers 2 --threads 4 --access-logfile - --error-logfile - app:app

通过Azure Web App日志面板查看Gunicorn的请求日志和错误信息,确认是否有请求到达、应用是否存在内部报错。

5. 资源限制排查

Azure Web App免费/基础层资源有限,若Bot启动时加载大量数据或处理高并发请求,可能导致超时。可临时升级至Standard层测试,排除资源不足问题。

内容的提问来源于stack exchange,提问作者Yeshpal Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 14:26:05