Skip to content

Commit 61019cd

Browse files
committed
scheduler tutorial draft
1 parent b9a9f1f commit 61019cd

File tree

6 files changed

+125
-47
lines changed

6 files changed

+125
-47
lines changed

scheduler/README.md

Whitespace-only changes.

scheduler/app.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
runtime: python
2+
service: my-service
23
env: flex
34
entrypoint: gunicorn -b :$PORT main:app
45

56
runtime_config:
67
python_version: 3
7-
8-
env_variables:
9-
PYTHONUNBUFFERED: TRUE

scheduler/create_job.py

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,51 @@
1-
# Copyright 2018 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-
# [START cloud_scheduler_create_job]
16-
from google.cloud import scheduler
17-
18-
client = scheduler.CloudSchedulerClient()
19-
20-
# TODO(developer): Uncomment and set the following variables
21-
# project_id = "PROJECT_ID"
22-
# location_id = "LOCATION_ID"
23-
# app_engine_version = "VERSION_ID"
24-
25-
parent = client.location_path(project_id, location_id)
26-
27-
job = {
28-
'app_engine_http_target': {
29-
'app_engine_routing': {
30-
'service':'default',
31-
'version': app_engine_version
1+
# Copyright 2018 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+
16+
def create_scheduler_job(project_id, location_id, service_id):
17+
# [START cloud_scheduler_quickstart]
18+
"""Create a job with an App Engine target via the Cloud Scheduler API"""
19+
from google.cloud import scheduler
20+
21+
# Create a client.
22+
client = scheduler.CloudSchedulerClient()
23+
24+
# TODO(developer): Uncomment and set the following variables
25+
# project_id = 'PROJECT_ID'
26+
# location_id = 'LOCATION_ID'
27+
# service_id = 'my-service'
28+
29+
# Construct the fully qualified location path.
30+
parent = client.location_path(project_id, location_id)
31+
32+
# Construct the request body.
33+
job = {
34+
'app_engine_http_target': {
35+
'app_engine_routing': {
36+
'service': service_id
37+
},
38+
'relative_uri': '/log_payload',
39+
'http_method': 'POST',
40+
'body': 'Hello World'.encode()
3241
},
33-
'relative_uri': '/log_payload',
34-
'http_method': 'POST',
35-
'body': 'Hello World'.encode()
36-
},
37-
'schedule': "* * * * *",
38-
'time_zone': "America/Los_Angeles"
39-
}
40-
41-
response = client.create_job(parent, job)
42-
# [END cloud_scheduler_create_job]
42+
'schedule': '* * * * *',
43+
'time_zone': 'America/Los_Angeles'
44+
}
45+
46+
# Use the client to send job creation request.
47+
response = client.create_job(parent, job)
48+
49+
print('Created job: {}'.format(response.name))
50+
return response
51+
# [END cloud_scheduler_quickstart]

scheduler/create_job_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2018 Google Inc. All Rights Reserved.
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+
17+
import create_job
18+
19+
TEST_PROJECT_ID = os.getenv('GCLOUD_PROJECT')
20+
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
21+
22+
23+
def test_create_job():
24+
result = create_job.create_scheduler_job(
25+
TEST_PROJECT_ID, TEST_LOCATION, 'my-service')
26+
assert TEST_PROJECT_ID in result.name

scheduler/main.py

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

1515
"""App Engine app to serve as an endpoint for Cloud Scheduler samples."""
1616

17-
# [START cloud_scheduler_quickstart]
17+
# [START cloud_scheduler_app]
1818
from flask import Flask, request
1919

2020
app = Flask(__name__)
@@ -24,9 +24,9 @@
2424
def example_task_handler():
2525
"""Log the request payload."""
2626
payload = request.get_data(as_text=True) or '(empty payload)'
27-
print('Received job with payload: {}'.format(payload), flush=True)
27+
print('Received job with payload: {}'.format(payload))
2828
return 'Printed job payload: {}'.format(payload)
29-
# [END cloud_scheduler_quickstart]
29+
# [END cloud_scheduler_app]
3030

3131

3232
@app.route('/')

scheduler/main_test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2018 Google Inc. All Rights Reserved.
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 pytest
16+
17+
18+
@pytest.fixture
19+
def app():
20+
import main
21+
main.app.testing = True
22+
return main.app.test_client()
23+
24+
25+
def test_index(app):
26+
r = app.get('/')
27+
assert r.status_code == 200
28+
29+
30+
def test_log_payload(capsys, app):
31+
payload = 'test_payload'
32+
33+
r = app.post('/log_payload', data=payload)
34+
assert r.status_code == 200
35+
36+
out, _ = capsys.readouterr()
37+
assert payload in out
38+
39+
40+
def test_empty_payload(capsys, app):
41+
r = app.post('/log_payload')
42+
assert r.status_code == 200
43+
44+
out, _ = capsys.readouterr()
45+
assert 'empty payload' in out

0 commit comments

Comments
 (0)