Skip to content

Commit 89d1071

Browse files
andrewsgJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Add comments and region tags to Cloud Tasks samples (GoogleCloudPlatform#1271)
1 parent 278afe6 commit 89d1071

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

appengine/flexible/tasks/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ App Engine queues push tasks to an App Engine HTTP target. This directory
1212
contains both the App Engine app to deploy, as well as the snippets to run
1313
locally to push tasks to it, which could also be called on App Engine.
1414

15-
`app_engine_queue_snippets.py` is a simple command-line program to create tasks
16-
to be pushed to the App Engine app.
15+
`create_app_engine_queue_task.py` is a simple command-line program to create
16+
tasks to be pushed to the App Engine app.
1717

1818
`main.py` is the main App Engine app. This app serves as an endpoint to receive
1919
App Engine task attempts.

appengine/flexible/tasks/create_app_engine_queue_task.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@
2020
import json
2121

2222

23-
def seconds_from_now_to_rfc3339_datetime(seconds):
24-
"""Return an RFC 3339 datetime string for a number of seconds from now."""
25-
d = datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds)
26-
return d.isoformat('T') + 'Z'
27-
28-
23+
# [START cloud_tasks_appengine_create_task]
2924
def create_task(project, queue, location, payload=None, in_seconds=None):
3025
"""Create a task for a given queue with an arbitrary payload."""
3126

@@ -34,36 +29,50 @@ def create_task(project, queue, location, payload=None, in_seconds=None):
3429
# Create a client.
3530
client = googleapiclient.discovery.build('cloudtasks', 'v2beta2')
3631

32+
# Construct the request body.
3733
url = '/log_payload'
3834
body = {
3935
'task': {
40-
'app_engine_http_request': {
36+
'app_engine_http_request': { # Specify the type of request.
4137
'http_method': 'POST',
4238
'relative_url': url
4339
}
4440
}
4541
}
4642

4743
if payload is not None:
48-
# Payload is a string (unicode), and must be encoded for base64.
49-
# The finished request body is JSON, which requires unicode.
50-
body['task']['app_engine_http_request']['payload'] = base64.b64encode(
51-
payload.encode()).decode()
44+
# The API expects base64 encoding of the payload, so encode the unicode
45+
# `payload` object into a byte string and base64 encode it.
46+
base64_encoded_payload = base64.b64encode(payload.encode())
47+
48+
# The request body object will be emitted in JSON, which requires
49+
# unicode objects, so convert the byte string to unicode, still base64.
50+
converted_payload = base64_encoded_payload.decode()
51+
52+
# Add the payload to the request.
53+
body['task']['app_engine_http_request']['payload'] = converted_payload
5254

5355
if in_seconds is not None:
54-
scheduled_time = seconds_from_now_to_rfc3339_datetime(in_seconds)
56+
# Convert "seconds from now" into an rfc3339 datetime string.
57+
d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)
58+
scheduled_time = d.isoformat('T') + 'Z'
59+
60+
# Add the rfc3339 datetime string to the request.
5561
body['task']['schedule_time'] = scheduled_time
5662

63+
# Construct the fully qualified queue name.
5764
queue_name = 'projects/{}/locations/{}/queues/{}'.format(
5865
project, location, queue)
5966

6067
print('Sending task {}'.format(json.dumps(body)))
6168

69+
# Use the client to build and send the task.
6270
response = client.projects().locations().queues().tasks().create(
6371
parent=queue_name, body=body).execute()
6472

6573
print('Created task {}'.format(response['name']))
6674
return response
75+
# [END cloud_tasks_appengine_create_task]
6776

6877

6978
if __name__ == '__main__':

appengine/flexible/tasks/main.py

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

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

17+
# [START cloud_tasks_appengine_quickstart]
1718
from flask import Flask, request
1819

1920
app = Flask(__name__)
@@ -25,6 +26,7 @@ def log_payload():
2526
payload = request.get_data(as_text=True) or '(empty payload)'
2627
print('Received task with payload: {}'.format(payload))
2728
return 'Printed task payload: {}'.format(payload)
29+
# [END cloud_tasks_appengine_quickstart]
2830

2931

3032
@app.route('/')

tasks/pull_queue_snippets.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import base64
2525

2626

27+
# [START cloud_tasks_create_task]
2728
def create_task(project, queue, location):
2829
"""Create a task for a given queue with an arbitrary payload."""
2930

@@ -32,25 +33,40 @@ def create_task(project, queue, location):
3233
# Create a client.
3334
client = googleapiclient.discovery.build('cloudtasks', 'v2beta2')
3435

36+
# Prepare the payload.
3537
payload = 'a message for the recipient'
38+
39+
# The API expects base64 encoding of the payload, so encode the unicode
40+
# `payload` object into a byte string and base64 encode it.
41+
base64_encoded_payload = base64.b64encode(payload.encode())
42+
43+
# The request body object will be emitted in JSON, which requires
44+
# unicode objects, so convert the byte string to unicode (still base64).
45+
converted_payload = base64_encoded_payload.decode()
46+
47+
# Construct the request body.
3648
task = {
3749
'task': {
3850
'pull_message': {
39-
'payload': base64.b64encode(payload.encode()).decode()
51+
'payload': converted_payload
4052
}
4153
}
4254
}
4355

56+
# Construct the fully qualified queue name.
4457
queue_name = 'projects/{}/locations/{}/queues/{}'.format(
4558
project, location, queue)
4659

60+
# Use the client to build and send the task.
4761
response = client.projects().locations().queues().tasks().create(
4862
parent=queue_name, body=task).execute()
4963

5064
print('Created task {}'.format(response['name']))
5165
return response
66+
# [END cloud_tasks_create_task]
5267

5368

69+
# [START cloud_tasks_pull_task]
5470
def pull_task(project, queue, location):
5571
"""Pull a single task from a given queue and lease it for 10 minutes."""
5672

@@ -74,6 +90,7 @@ def pull_task(project, queue, location):
7490

7591
print('Pulled task {}'.format(response))
7692
return response['tasks'][0]
93+
# [END cloud_tasks_pull_task]
7794

7895

7996
def acknowledge_task(task):

0 commit comments

Comments
 (0)