Skip to content

Commit b9a9f1f

Browse files
committed
scheduler sample
1 parent b4c2b18 commit b9a9f1f

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

scheduler/.gcloudignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file specifies files that are *not* uploaded to Google Cloud Platform
2+
# using gcloud. It follows the same syntax as .gitignore, with the addition of
3+
# "#!include" directives (which insert the entries of the given .gitignore-style
4+
# file at that point).
5+
#
6+
# For more information, run:
7+
# $ gcloud topic gcloudignore
8+
#
9+
.gcloudignore
10+
# If you would like to upload your .git directory, .gitignore file or files
11+
# from your .gitignore file, remove the corresponding line
12+
# below:
13+
.git
14+
.gitignore
15+
16+
# Node.js dependencies:
17+
node_modules/

scheduler/app.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
runtime: python
2+
env: flex
3+
entrypoint: gunicorn -b :$PORT main:app
4+
5+
runtime_config:
6+
python_version: 3
7+
8+
env_variables:
9+
PYTHONUNBUFFERED: TRUE

scheduler/create_job.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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
32+
},
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]

scheduler/main.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
"""App Engine app to serve as an endpoint for Cloud Scheduler samples."""
16+
17+
# [START cloud_scheduler_quickstart]
18+
from flask import Flask, request
19+
20+
app = Flask(__name__)
21+
22+
23+
@app.route('/log_payload', methods=['POST'])
24+
def example_task_handler():
25+
"""Log the request payload."""
26+
payload = request.get_data(as_text=True) or '(empty payload)'
27+
print('Received job with payload: {}'.format(payload), flush=True)
28+
return 'Printed job payload: {}'.format(payload)
29+
# [END cloud_scheduler_quickstart]
30+
31+
32+
@app.route('/')
33+
def hello():
34+
"""Basic index to verify app is serving."""
35+
return 'Hello World!'
36+
37+
38+
if __name__ == '__main__':
39+
# This is used when running locally. Gunicorn is used to run the
40+
# application on Google App Engine. See entrypoint in app.yaml.
41+
app.run(host='127.0.0.1', port=8080, debug=True)

scheduler/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Flask==1.0.2
2+
gunicorn==19.9.0
3+
google-cloud-scheduler==0.1.0

0 commit comments

Comments
 (0)