Google Cloud Scheduler: Why cloud function runs successfully but logger still shows error?

873 views Asked by At

I set up a google cloud scheduler job that triggers a cloud function through HTTP. I can be sure that the cloud function is triggered and runs successfully - it has produced the expected outcome. However, the scheduler job still shows "failed" and the logger is like:

{
  "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"
}

I have pasted the cloud function code below with edits necessary to remove sensitive information:

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'

what I can be sure of is that the line right before the end of the fucntion, DING_BOT.send_text(msg) executed successfully. I have received the text message.

What cloud be wrong here?

1

There are 1 answers

1
guillaume blaquiere On BEST ANSWER

It's a common problem because of partial UI of Google Cloud Console. So, I took the hypothesis that you set up your scheduler only with the console.

So, you need to create, or to update it with command line (GCLOUD) or API (but GCLOUD is easier), to add the "attempt-deadline" parameter.

In fact Cloud Scheduler also have a timeout (60s by default)and if the URL don't answer in this timeframe, the call is considered as fail

Increase this param to 250s, and it should be OK.

Note: you can also set retry policies with the CLI, it could be interesting if you need it!