Luiz
Luiz

Reputation: 361

How to update GCP Scheduler Jobs with Python

I'm working in this project to automate updates in Cloud Scheduler Jobs with Python.

I already wrote the logic in Python but I'm facing one problem, it looks like that to update a Cloud Scheduler job with Python is similar to create a job, you have to past most of the properties of the job in the code, that is the problem, I only want to update the retry_config, nothing else. I want to leave the schedule and the target as it is, so I don't have to past those again every time.

Of course I can get the current schedule and target of the job using another class as GetJobRequest for example, that wouldn't be a problem, but I wish I didn't have to, since I don't want to update those fields.

Help?

from google.cloud import scheduler_v1
from google.protobuf import duration_pb2

client = scheduler_v1.CloudSchedulerClient()

retry_config = scheduler_v1.RetryConfig()
retry_config.retry_count = 4
retry_config.max_doublings = 4
retry_config.min_backoff_duration = duration_pb2.Duration(seconds=5)
retry_config.max_backoff_duration = duration_pb2.Duration(seconds=60)

job = scheduler_v1.Job()
job.name = f"projects/{PROJECT_ID}/locations/{DATAFLOW_REGION}/jobs/test"
job.retry_config = retry_config
job.schedule = "* * * * 1"

method = scheduler_v1.HttpMethod(2)

target = scheduler_v1.HttpTarget()
target.uri = "https://xxxx"
target.http_method = method

job.http_target = target

request = scheduler_v1.UpdateJobRequest(
    job=job
)

response = client.update_job(request=request)

print(response)

Upvotes: 1

Views: 1264

Answers (1)

Luiz
Luiz

Reputation: 361

It is possible to specify the properties that need to be changed using the update_mask parameter.

The final code will be as follows:

from google.cloud import scheduler_v1
from google.protobuf import duration_pb2, field_mask_pb2

client = scheduler_v1.CloudSchedulerClient()

retry_config = scheduler_v1.RetryConfig()
retry_config.retry_count = 4
retry_config.max_doublings = 4
retry_config.min_backoff_duration = duration_pb2.Duration(seconds=5)
retry_config.max_backoff_duration = duration_pb2.Duration(seconds=60)

job = scheduler_v1.Job()
job.name = f"projects/{PROJECT_ID}/locations/{DATAFLOW_REGION}/jobs/test"
job.retry_config = retry_config

update_mask = field_mask_pb2.FieldMask(paths=['retry_config'])

request = scheduler_v1.UpdateJobRequest(
    job=job,
    update_mask=update_mask
)

response = client.update_job(request=request)

print(response)

Upvotes: 3

Related Questions