【发布时间】:2021-03-05 03:26:03
【问题描述】:
我正在使用 Gunicorn 的 Google Cloud Run 中部署一个 python 应用程序。我的 gunicorn 和 cloud run 超时都设置为 900 秒,这也是 Cloud Run 的超时。奇怪的是,当我调用该函数时,如果应用程序运行时间超过 60 秒,我会从 Cloud Run 收到 502 错误,而如果运行时间少于 60 秒,则不会。例如,下面部署的函数抛出了这个错误:
def process_file(request=request):
time.sleep(61)
...
return handle_response()
但是,如果我将睡眠时间改为 40 秒:
def process_file(request=request):
time.sleep(40)
...
return handle_response()
没有 502 错误。起初我以为问题是由 nginx 引起的,它有 60 秒的默认超时时间,但似乎 nginx 并没有默认使用 docker 或 cloud run 部署,所以这似乎不是问题的原因。我的 Dockerfile 如下:
FROM continuumio/miniconda3
# Install production dependencies.
RUN conda install numpy==1.17.2
RUN conda install xlsxwriter==1.1.2
RUN conda install pandas==0.25.1
RUN conda install -c conda-forge ciso8601
RUN pip install gunicorn flask gevent flask_mail flask-cors pyjwt firebase_admin networkx datefinder google-cloud-pubsub
# Copy local code to the container image.
COPY app.py .
RUN mkdir backend/
COPY backend/ /backend/
# Service must listen to $PORT environment variable.
# This default value facilitates local development.
ENV PORT 8080
# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind 0.0.0.0:$PORT --workers 1 app:app --timeout 900 --log-level debug
我在前端使用axios 调用云运行,据我了解,它没有超时,所以我不认为这应该是一个问题。任何帮助表示赞赏,谢谢!
编辑:这是 chrome 控制台中错误消息的图像 - 不过似乎不是很有帮助:
【问题讨论】:
-
默认的 Cloud Run 超时为 5 分钟。使用
--log-level debug运行 Gunicorn 以查看 gunicorn 是问题还是其他原因。 Cloud Run 不使用 Nginx 作为前端。 GFE(谷歌前端)位于 Cloud Run 的前面。 Cloud Run 支持--timeout=N。尝试将其设置为特定值,例如 120。查看您的 Cloud Run 服务实例的 Stackdriver 日志。用你的发现编辑你的问题。 -
我将超时设置为 15 分钟(900 秒),这样就不会出现问题;我会用这个更新问题。正如您从 dockerfile 中看到的那样,我已经添加了调试和超时标志 - 日志中没有显示任何有用的信息。
-
Stackdriver 没有显示 502?
-
不,502 只显示在前端。在堆栈驱动程序中没有它的踪迹。我可能应该注意到,云运行函数实际上确实成功完成了执行。
-
这意味着 Cloud Run 本身可能不是罪魁祸首。查看浏览器调试器。有没有出现可能指向问题的东西?您是否在 Cloud Run 前使用负载平衡器? HTTP 错误 502 表示网关错误。以我的经验,这意味着前端和后端之间的故障(后端无法及时响应)。编辑您的问题,详细了解您的架构。
标签: docker google-cloud-platform timeout google-cloud-run http-status-code-502