首页 > 解决方案 > 在 Cloud Function 超时前 5 秒运行一些东西

问题描述

如果我有一个谷歌云功能,并且我以最大超时(比如说540s)部署它,我怎么能在超时之前运行一些东西?例如,在超时前 5 秒,我想运行自定义线路?例如:

def run_something(data, context):
    print('Function running...')
    time.sleep(600)

    if time_left <= 5_seconds: 
       print("Function is about to time out in 5 seconds")

标签: pythongoogle-cloud-platformgoogle-cloud-functions

解决方案


如果您想跟踪超时请求以进行进一步调查,您可以在Stackdriver Logging中过滤您的云函数的超时请求。

请注意,超时不会在 Stackdriver 中报告为错误,但是您可以设置过滤器超时日志条目。在 Google Cloud Platform Console 中,转到Stackdriver Logging并构建一个过滤器,如下所示以获取超时的请求:

resource.type="cloud_function"
resource.labels.function_name="[YOUR_FUNCTION_NAME_HERE]"
"finished with status: 'timeout'"

推荐阅读