Google Cloud Scheduler:为什么云功能可以成功运行,但记录器仍然显示错误?

问题描述

我设置了一个Google Cloud Scheduler作业,该作业通过HTTP触发了云功能。我可以确定云功能已触发并成功运行-它产生了预期的结果。 但是,调度程序作业仍然显示“失败”,并且记录器如下:

{
  "insertId": "8ca551232347v49","jsonPayload": {
    "jobName": "projects/john/locations/asia-southeast2/jobs/Get_food","status": "UNKNowN","url": "https://asia-southeast2-john.cloudfunctions.net/Get_food","@type": "type.googleapis.com/google.cloud.scheduler.logging.AttemptFinished","targettype": "HTTP"
  },"httpRequest": {},"resource": {
    "type": "cloud_scheduler_job","labels": {
      "job_id": "Get_food","location": "asia-southeast2","project_id": "john"
    }
  },"timestamp": "2020-10-22T04:08:24.521610728Z","severity": "ERROR","logName": "projects/john/logs/cloudscheduler.googleapis.com%2Fexecutions","receiveTimestamp": "2020-10-22T04:08:24.521610728Z"
}

我在下面粘贴了云功能代码,并进行了必要的修改,以删除敏感信息:

import requests
import pyMysqL 
from pyMysqL.constants import CLIENT
from google.cloud import storage
import os
import time 
from DingBot import DING_BOT 
from decouple import config
import datetime 

BUCKET_NAME = 'john-test-dataset'
FOLDER_IN_BUCKET = 'compressed_data'
LOCAL_PATH = '/tmp/'



TIMEOUT_TIME = 500


def run(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values that can be turned into a
        Response object using
        `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
    """
        
    while True:
        # some code that will be break the loop in about 200 seconds

    DING_BOT.send_text(msg)
    return 'ok'

我可以确定的是,在功能结束前的那一行DING_BOT.send_text(msg)已成功执行。我收到了短信。

这是什么云呢?

解决方法

这是一个常见的问题,因为Google Cloud Console的部分UI。因此,我假设您仅使用控制台来设置调度程序。

因此,您需要创建command line (GCLOUD)API或用"attempt-deadline" parameter.custom option更新它(但GCLOUD更加容易),以添加{{3}}

实际上Cloud Scheduler也有一个超时(默认为60秒),如果URL在此时间段内未应答,则呼叫被视为失败

将此参数增加到250s,应该没问题。

注意:您还可以使用CLI设置重试策略,如果需要的话可能会很有趣!