Repository navigation
feat(cloud_scheduler): modernize samples and cover documented job tasks - #14767
Conversation
The Cloud Scheduler Python samples covered only creating and deleting a job with an App Engine target. They returned values instead of printing results, had no error handling, and pinned an old library version. The App Engine handler app used a retired runtime. This change adds a sample for each documented job task, using google-cloud-scheduler 2.21.0, with one task and one region tag per file: - cloudscheduler_create_job: now shows the recommended path, an HTTP target that calls a Cloud Run service with an OIDC token. - cloudscheduler_create_appengine_job: the App Engine target sample, moved from create_job.py. - New samples: - cloudscheduler_create_http_job_oauth - cloudscheduler_create_pubsub_job - cloudscheduler_create_job_with_retry - cloudscheduler_update_job - cloudscheduler_pause_job - cloudscheduler_resume_job - cloudscheduler_run_job - cloudscheduler_get_job - cloudscheduler_list_jobs - cloudscheduler_delete_job: modernized. It also adds cloudscheduler_receiver, a Cloud Run function that handles scheduled requests and skips duplicate deliveries. It replaces the App Engine handler app, retiring the cloudscheduler_app and cloudscheduler_python_yaml region tags. Tests live in tests/ subdirectories: - Unit tests for every sample and for the receiver, using a mocked client. These run without Google Cloud credentials. - Integration tests that run every sample live, including a job that calls the receiver end to end. They are skipped unless GOOGLE_CLOUD_PROJECT and SERVICE_URL are set. Unit and integration tests pass on Python 3.13.
Replace the double negatives about unauthenticated access in the create_job and receiver docstrings with positive phrasing: the Cloud Run service can require authentication, and the receiver function is deployed to require authentication.
|
Here is the summary of changes. You are about to add 11 region tags.
You are about to delete 2 region tags.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request restructures and updates the Cloud Scheduler samples, introducing a new receiver Cloud Run function sample with deduplication logic and updating various job management snippets along with comprehensive unit and integration tests. Feedback suggests addressing potential race conditions in the receiver function by introducing a threading.Lock and a processing_runs set to track active runs safely, as well as adding an autouse fixture in the tests to reset these global in-memory sets to prevent test interference.
Replace the custom argument helper in each sample's validation entrypoint with argparse named flags (for example, --project-id and --job-id). Each flag defaults to its environment variable, such as GOOGLE_CLOUD_PROJECT, and then to an example value, so the samples run without flags when the environment variables are set. Flags override environment variables, and --help lists every option with its environment variable. Add unit tests for the entrypoint that cover environment variable defaults, flag overrides, and example defaults.
Address PR review feedback: - The receiver now checks and claims each run in one step under a lock, before doing the work, so two deliveries of the same run that arrive together can't both do the work. If the work fails, the claim is released so that Cloud Scheduler's retry can process the run. - Receiver tests reset the claimed runs before each test and cover releasing the claim after a failure. - Tests compare the full Dataproc target URI instead of checking for a hostname substring.
amcolin
left a comment
There was a problem hiding this comment.
Code runs locally as expected. LGTM.
Description
Modernizes the Cloud Scheduler Python samples and adds a sample for each documented job task. Before this change, the samples covered only creating and deleting a job with an App Engine target. They returned values instead of printing results, had no error handling, and pinned an old library version. The App Engine handler app used a retired runtime.
All samples use
google-cloud-scheduler2.21.0, with one task and one region tag per file.Job samples (
cloud_scheduler/snippets/)cloudscheduler_create_jobcloudscheduler_create_appengine_jobcreate_job.py(App Engine target)cloudscheduler_create_http_job_oauthcloudscheduler_create_pubsub_jobcloudscheduler_create_job_with_retrycloudscheduler_update_jobcloudscheduler_pause_jobcloudscheduler_resume_jobcloudscheduler_run_jobcloudscheduler_get_jobcloudscheduler_list_jobscloudscheduler_delete_jobReceiver (
cloud_scheduler/receiver/)cloudscheduler_receiveris a Cloud Run function that handles scheduled requests and skips duplicate deliveries of the same scheduled run. It replaces the App Engine handler app, so thecloudscheduler_appandcloudscheduler_python_yamlregion tags are retired. Neither tag is used in published documentation.Tests (in
tests/subdirectories)GOOGLE_CLOUD_PROJECTandSERVICE_URLare set.Checklist
Testing
Compliance & Style
cloud_schedulerhas no README.receiver/is inside the existingcloud_schedulerproduct directory, which the default owners cover.Post-Approval Actions