Skip to content Skip to sidebar Skip to footer

How To Create A Job With Google Cloud Scheduler Python Api

I would like to create a cron job that will run every day at 10am to trigger a cloud function. However, I am having a problem with the Python api. When I create a job it pops out

Solution 1:

Assuming utc+8 is Australia/Perth and job that will run every day at 10am is 0 10 * * * then the function should be:

from google.cloud import scheduler_v1

project_id = XXXX
client = scheduler_v1.CloudSchedulerClient.from_service_account_json(
    r"./xxxx.json")

parent= client.location_path(project_id,'us-central1')

job={"name":"projects/your-project/locations/app-engine-location/jobs/traing_for_model",
     "description":"this is for testing training model",
     "http_target": {"uri":"https://us-central1-gerald-automl-test.cloudfunctions.net/automl-trainmodel-1-test-for-cron-job"},
     "schedule":"0 10 * * *",
     "time_zone":"Australia/Perth",
     }

training_job= client.create_job(parent,job)

Solution 2:

Have you tried doing:

from google.cloud.scheduler_v1.types import HttpTarget as Target


from google.cloud import scheduler_v1

project_id = XXXX
client = scheduler_v1.CloudSchedulerClient.from_service_account_json(
    r"./xxxx.json")

parent= client.location_path(project_id,'us-central1')
job={"name":"traing_for_model",
     "description":"this is for testing training model",
     "http_target": Target(uri: "https://us-central1-gerald-automl-test.cloudfunctions.net/automl-trainmodel-1-test-for-cron-job"),
     "schedule":"1 0 * * *",
     "time_zone":"utc+8",
     }
training_job= client.create_job(parent,job)

I haven't tested this code, but it's clear that you're sending the string to the http_target, and the instance of it needs to be an actual HttpTarget object, and not just a string.

Post a Comment for "How To Create A Job With Google Cloud Scheduler Python Api"