Skip to content

Commit c469391

Browse files
refactor: clean up cloud_scheduler samples (GoogleCloudPlatform#10095)
Co-authored-by: gcf-merge-on-green[bot] <60162190+gcf-merge-on-green[bot]@users.noreply.github.com>
1 parent 9685dce commit c469391

File tree

10 files changed

+104
-63
lines changed

10 files changed

+104
-63
lines changed
File renamed without changes.

scheduler/snippets/create_job.py renamed to cloud_scheduler/snippets/create_job.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414

1515
# [START cloudscheduler_create_job]
1616

17-
from google.cloud import scheduler
18-
from google.cloud.scheduler_v1 import Job
17+
from google.cloud import scheduler_v1
1918

2019

21-
def create_scheduler_job(project_id: str, location_id: str, service_id: str) -> Job:
20+
def create_scheduler_job(
21+
project_id: str, location_id: str, service_id: str
22+
) -> scheduler_v1.Job:
2223
"""Create a job with an App Engine target via the Cloud Scheduler API.
2324
2425
Args:
@@ -31,27 +32,29 @@ def create_scheduler_job(project_id: str, location_id: str, service_id: str) ->
3132
"""
3233

3334
# Create a client.
34-
client = scheduler.CloudSchedulerClient()
35-
36-
# Construct the fully qualified location path.
37-
parent = f"projects/{project_id}/locations/{location_id}"
38-
39-
# Construct the request body.
40-
job = {
41-
"app_engine_http_target": {
42-
"app_engine_routing": {"service": service_id},
43-
"relative_uri": "/log_payload",
44-
"http_method": 1,
45-
"body": b"Hello World",
46-
},
47-
"schedule": "* * * * *",
48-
"time_zone": "America/Los_Angeles",
49-
}
35+
client = scheduler_v1.CloudSchedulerClient()
36+
37+
# Construct the job.
38+
job = scheduler_v1.Job(
39+
app_engine_http_target=scheduler_v1.AppEngineHttpTarget(
40+
app_engine_routing=scheduler_v1.AppEngineRouting(service=service_id),
41+
relative_uri="/log_payload",
42+
http_method=scheduler_v1.HttpMethod.POST,
43+
body=b"Hello World",
44+
),
45+
schedule="* * * * *",
46+
time_zone="America/Los_Angeles",
47+
)
5048

5149
# Use the client to send the job creation request.
52-
response = client.create_job(request={"parent": parent, "job": job})
50+
response = client.create_job(
51+
scheduler_v1.CreateJobRequest(
52+
parent=client.common_location_path(project_id, location_id),
53+
job=job,
54+
)
55+
)
5356

54-
print(f"Created job: {response.name}")
5557
return response
5658

59+
5760
# [END cloudscheduler_create_job]

scheduler/snippets/delete_job.py renamed to cloud_scheduler/snippets/delete_job.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
# [START cloudscheduler_delete_job]
1616

17-
from google.cloud import scheduler
17+
from google.cloud import scheduler_v1
1818

1919

20-
def delete_scheduler_job(project_id: str, location_id: str, job_id: str) -> bool:
20+
def delete_scheduler_job(project_id: str, location_id: str, job_id: str) -> None:
2121
"""Delete a job via the Cloud Scheduler API.
2222
2323
Args:
@@ -27,14 +27,14 @@ def delete_scheduler_job(project_id: str, location_id: str, job_id: str) -> bool
2727
"""
2828

2929
# Create a client.
30-
client = scheduler.CloudSchedulerClient()
31-
32-
# Construct the fully qualified job path.
33-
job = f"projects/{project_id}/locations/{location_id}/jobs/{job_id}"
30+
client = scheduler_v1.CloudSchedulerClient()
3431

3532
# Use the client to send the job deletion request.
36-
client.delete_job(name=job)
37-
print("Job deleted.")
38-
return True
33+
client.delete_job(
34+
scheduler_v1.DeleteJobRequest(
35+
name=client.job_path(project_id, location_id, job_id)
36+
)
37+
)
38+
3939

4040
# [END cloudscheduler_delete_job]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import uuid
17+
18+
from google.api_core import exceptions
19+
from google.cloud import scheduler_v1
20+
21+
import pytest
22+
23+
import create_job
24+
import delete_job
25+
26+
TEST_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
27+
TEST_LOCATION = os.getenv("LOCATION_ID", "us-central1")
28+
TEST_JOB_ID = f"my-job-{uuid.uuid4().hex}"
29+
30+
31+
def test_create_job() -> None:
32+
client = scheduler_v1.CloudSchedulerClient()
33+
34+
job = create_job.create_scheduler_job(TEST_PROJECT_ID, TEST_LOCATION, "my-service")
35+
assert job.name.startswith(
36+
client.common_location_path(TEST_PROJECT_ID, TEST_LOCATION)
37+
)
38+
39+
client.delete_job(scheduler_v1.DeleteJobRequest(name=job.name))
40+
41+
42+
def test_delete_job() -> None:
43+
client = scheduler_v1.CloudSchedulerClient()
44+
45+
client.create_job(
46+
scheduler_v1.CreateJobRequest(
47+
parent=client.common_location_path(TEST_PROJECT_ID, TEST_LOCATION),
48+
job=scheduler_v1.Job(
49+
name=client.job_path(TEST_PROJECT_ID, TEST_LOCATION, TEST_JOB_ID),
50+
app_engine_http_target=scheduler_v1.AppEngineHttpTarget(
51+
app_engine_routing=scheduler_v1.AppEngineRouting(
52+
service="service-id"
53+
),
54+
relative_uri="/log_payload",
55+
http_method=scheduler_v1.HttpMethod.POST,
56+
body=b"Hello World",
57+
),
58+
schedule="* * * * *",
59+
time_zone="America/Los_Angeles",
60+
),
61+
)
62+
)
63+
64+
# Deleting an existant job succeeds
65+
delete_job.delete_scheduler_job(TEST_PROJECT_ID, TEST_LOCATION, TEST_JOB_ID)
66+
67+
# Deleting a non-existant job fails with a not found error
68+
with pytest.raises(exceptions.NotFound):
69+
delete_job.delete_scheduler_job(TEST_PROJECT_ID, TEST_LOCATION, TEST_JOB_ID)

scheduler/snippets/main.py renamed to cloud_scheduler/snippets/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def example_task_handler() -> str:
2828
print(f"Received job with payload: {payload}")
2929
return f"Printed job payload: {payload}"
3030

31+
3132
# [END cloudscheduler_app]
3233

3334

File renamed without changes.

scheduler/snippets/noxfile_config.py renamed to cloud_scheduler/snippets/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"ignored_versions": ["2.7", "3.6"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
28-
"enforce_type_hints": False,
28+
"enforce_type_hints": True,
2929
# An envvar key for determining the project id to use. Change it
3030
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
3131
# build specific Cloud project. You can also use your own string
File renamed without changes.
File renamed without changes.

scheduler/snippets/job_test.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)