-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Cloud Scheduler sample #1968
Merged
+218
−0
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b9a9f1f
scheduler sample
averikitsch 61019cd
scheduler tutorial draft
averikitsch f211a46
create and delete requests completed
averikitsch 708ea5d
Merge branch 'master' into scheduler
averikitsch f939ae5
updated region tag
averikitsch d8733b6
Merge branch 'scheduler' of https://github.com/GoogleCloudPlatform/py…
averikitsch d64afe4
update error
averikitsch ddb373b
fix linting
averikitsch 0f03868
Merge branch 'master' into scheduler
averikitsch a93fc14
Update styling
averikitsch 347abc5
Update license
averikitsch 2665037
Update license
averikitsch e757ed5
Update license
averikitsch 98cd407
Update license
averikitsch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright 2018 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the 'License'); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an 'AS IS' BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# [START cloud_scheduler_python_yaml] | ||
runtime: python37 | ||
service: my-service | ||
# [END cloud_scheduler_python_yaml] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright 2018 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the 'License'); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an 'AS IS' BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
def create_scheduler_job(project_id, location_id, service_id): | ||
"""Create a job with an App Engine target via the Cloud Scheduler API""" | ||
# [START cloud_scheduler_create_job] | ||
from google.cloud import scheduler | ||
|
||
# Create a client. | ||
client = scheduler.CloudSchedulerClient() | ||
|
||
# TODO(developer): Uncomment and set the following variables | ||
# project_id = 'PROJECT_ID' | ||
# location_id = 'LOCATION_ID' | ||
# service_id = 'my-service' | ||
|
||
# Construct the fully qualified location path. | ||
parent = client.location_path(project_id, location_id) | ||
|
||
# Construct the request body. | ||
job = { | ||
'app_engine_http_target': { | ||
'app_engine_routing': { | ||
'service': service_id | ||
}, | ||
'relative_uri': '/log_payload', | ||
'http_method': 'POST', | ||
'body': 'Hello World'.encode() | ||
}, | ||
'schedule': '* * * * *', | ||
'time_zone': 'America/Los_Angeles' | ||
} | ||
|
||
# Use the client to send the job creation request. | ||
response = client.create_job(parent, job) | ||
|
||
print('Created job: {}'.format(response.name)) | ||
# [END cloud_scheduler_create_job] | ||
return response | ||
|
||
|
||
def delete_scheduler_job(project_id, location_id, job_id): | ||
"""Delete a job via the Cloud Scheduler API""" | ||
# [START cloud_scheduler_delete_job] | ||
from google.cloud import scheduler | ||
from google.api_core.exceptions import GoogleAPICallError | ||
|
||
# Create a client. | ||
client = scheduler.CloudSchedulerClient() | ||
|
||
# TODO(developer): Uncomment and set the following variables | ||
# project_id = 'PROJECT_ID' | ||
# location_id = 'LOCATION_ID' | ||
# job_id = 'JOB_ID' | ||
|
||
# Construct the fully qualified job path. | ||
job = client.job_path(project_id, location_id, job_id) | ||
|
||
# Use the client to send the job deletion request. | ||
try: | ||
client.delete_job(job) | ||
print("Job deleted.") | ||
except GoogleAPICallError as e: | ||
print("Error: %s" % e) | ||
# [END cloud_scheduler_delete_job] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright 2018 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
|
||
import create_job | ||
|
||
TEST_PROJECT_ID = os.getenv('GCLOUD_PROJECT') | ||
TEST_LOCATION = os.getenv('LOCATION_ID', 'us-central1') | ||
|
||
|
||
def test_create_job(capsys): | ||
create_result = create_job.create_scheduler_job(TEST_PROJECT_ID, | ||
TEST_LOCATION, | ||
'my-service') | ||
out, _ = capsys.readouterr() | ||
assert 'Created job:' in out | ||
|
||
job_name = create_result.name.split('/')[-1] | ||
create_job.delete_scheduler_job(TEST_PROJECT_ID, TEST_LOCATION, job_name) | ||
|
||
out, _ = capsys.readouterr() | ||
assert 'Job deleted.' in out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2018 Google LLC | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Optional) Consider updating to 2019 as this is first publish |
||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""App Engine app to serve as an endpoint for Cloud Scheduler samples.""" | ||
|
||
# [START cloud_scheduler_app] | ||
from flask import Flask, request | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
# Define relative URI for job endpoint | ||
@app.route('/log_payload', methods=['POST']) | ||
def example_task_handler(): | ||
"""Log the job payload.""" | ||
payload = request.get_data(as_text=True) or '(empty payload)' | ||
print('Received job with payload: {}'.format(payload)) | ||
return 'Printed job payload: {}'.format(payload) | ||
# [END cloud_scheduler_app] | ||
|
||
|
||
@app.route('/') | ||
def hello(): | ||
"""Basic index to verify app is serving.""" | ||
return 'Hello World!' | ||
|
||
|
||
if __name__ == '__main__': | ||
# This is used when running locally. Gunicorn is used to run the | ||
# application on Google App Engine. See entrypoint in app.yaml. | ||
app.run(host='127.0.0.1', port=8080, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2018 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def app(): | ||
import main | ||
main.app.testing = True | ||
return main.app.test_client() | ||
|
||
|
||
def test_index(app): | ||
r = app.get('/') | ||
assert r.status_code == 200 | ||
|
||
|
||
def test_log_payload(capsys, app): | ||
payload = 'test_payload' | ||
|
||
r = app.post('/log_payload', data=payload) | ||
assert r.status_code == 200 | ||
|
||
out, _ = capsys.readouterr() | ||
assert payload in out | ||
|
||
|
||
def test_empty_payload(capsys, app): | ||
r = app.post('/log_payload') | ||
assert r.status_code == 200 | ||
|
||
out, _ = capsys.readouterr() | ||
assert 'empty payload' in out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Flask==1.0.2 | ||
gunicorn==19.9.0 | ||
google-cloud-scheduler==0.1.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider removing the hanging indent: