Skip to content

feat(cloud_scheduler): modernize samples and cover documented job tasks - #14767

Merged
iennae merged 4 commits into
mainfrom
feat/cloud-scheduler-modernize
Nov 7, 2026
Merged

iennae merged 4 commits into
mainfrom
feat/cloud-scheduler-modernize

Conversation

@iennae

@iennae iennae commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

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-scheduler 2.21.0, with one task and one region tag per file.

Job samples (cloud_scheduler/snippets/)

Region tag Status
cloudscheduler_create_job Updated: now shows the recommended path, an HTTP target that calls a Cloud Run service with an OIDC token
cloudscheduler_create_appengine_job Moved from create_job.py (App Engine target)
cloudscheduler_create_http_job_oauth New
cloudscheduler_create_pubsub_job New
cloudscheduler_create_job_with_retry New
cloudscheduler_update_job New
cloudscheduler_pause_job New
cloudscheduler_resume_job New
cloudscheduler_run_job New
cloudscheduler_get_job New
cloudscheduler_list_jobs New
cloudscheduler_delete_job Updated

Receiver (cloud_scheduler/receiver/)

cloudscheduler_receiver is 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 the cloudscheduler_app and cloudscheduler_python_yaml region tags are retired. Neither tag is used in published documentation.

Tests (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're skipped unless GOOGLE_CLOUD_PROJECT and SERVICE_URL are set.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended. The unit and integration tests pass on Python 3.13.

Compliance & Style

  • I have followed the Sample Guidelines.
  • The README is updated with all relevant information (if applicable). Not applicable: cloud_scheduler has no README.
  • If this PR adds a new sample directory: I updated the CODEOWNERS file. Not applicable: receiver/ is inside the existing cloud_scheduler product directory, which the default owners cover.

Post-Approval Actions

  • Please merge this PR for me once it is approved

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.
@iennae
iennae requested review from a team as code owners November 6, 2026 21:25
@product-auto-label product-auto-label Bot added the samples Issues that are directly related to samples. label Nov 6, 2026
@snippet-bot

snippet-bot Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

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.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cloud_scheduler/receiver/main.py Resolved
Comment thread cloud_scheduler/receiver/main.py Resolved
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.
@iennae
iennae requested a review from camiekim November 6, 2026 23:23

@amcolin amcolin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code runs locally as expected. LGTM.

@iennae
iennae merged commit 4603aec into main Nov 7, 2026
14 of 22 checks passed
@iennae
iennae deleted the feat/cloud-scheduler-modernize branch November 7, 2026 02:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants