Skip to content

Commit 84c4317

Browse files
averikitschandrewsg
authored andcommitted
Update Cloud Tasks Samples (GoogleCloudPlatform#1529)
* passing create task * Passing tests * updates to region tags
1 parent 785b2b7 commit 84c4317

File tree

3 files changed

+35
-52
lines changed

3 files changed

+35
-52
lines changed

tasks/pull_queue_snippets.py

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
# Copyright 2017 Google Inc.
3+
# Copyright 2018 Google Inc.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -21,91 +21,75 @@
2121
"""
2222

2323
import argparse
24-
import base64
2524

2625

27-
# [START cloud_tasks_create_task]
2826
def create_task(project, queue, location):
27+
# [START tasks_create_task]
2928
"""Create a task for a given queue with an arbitrary payload."""
3029

31-
import googleapiclient.discovery
30+
from google.cloud import tasks_v2beta2
3231

3332
# Create a client.
34-
client = googleapiclient.discovery.build('cloudtasks', 'v2beta2')
33+
client = tasks_v2beta2.CloudTasksClient()
3534

36-
# Prepare the payload.
37-
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()
35+
# Prepare the payload of type bytes.
36+
payload = 'a message for the recipient'.encode()
4637

4738
# Construct the request body.
4839
task = {
49-
'task': {
50-
'pullMessage': {
51-
'payload': converted_payload
52-
}
40+
'pull_message': {
41+
'payload': payload,
5342
}
5443
}
5544

5645
# Construct the fully qualified queue name.
57-
queue_name = 'projects/{}/locations/{}/queues/{}'.format(
58-
project, location, queue)
46+
parent = client.queue_path(project, location, queue)
5947

6048
# Use the client to build and send the task.
61-
response = client.projects().locations().queues().tasks().create(
62-
parent=queue_name, body=task).execute()
49+
response = client.create_task(parent, task)
6350

64-
print('Created task {}'.format(response['name']))
51+
print('Created task: {}'.format(response.name))
6552
return response
66-
# [END cloud_tasks_create_task]
53+
# [END tasks_create_task]
6754

6855

69-
# [START cloud_tasks_lease_and_acknowledge_task]
7056
def lease_task(project, queue, location):
57+
# [START tasks_lease_and_acknowledge_task]
7158
"""Lease a single task from a given queue for 10 minutes."""
7259

73-
import googleapiclient.discovery
60+
from google.cloud import tasks_v2beta2
7461

7562
# Create a client.
76-
client = googleapiclient.discovery.build('cloudtasks', 'v2beta2')
63+
client = tasks_v2beta2.CloudTasksClient()
7764

78-
duration_seconds = '600s'
79-
lease_options = {
80-
'maxTasks': 1,
81-
'leaseDuration': duration_seconds,
82-
'responseView': 'FULL'
83-
}
65+
# Construct the fully qualified queue name.
66+
parent = client.queue_path(project, location, queue)
67+
68+
lease_duration = {'seconds': 600}
8469

85-
queue_name = 'projects/{}/locations/{}/queues/{}'.format(
86-
project, location, queue)
70+
# Send lease request to client.
71+
response = client.lease_tasks(
72+
parent, lease_duration, max_tasks=1, response_view='FULL')
8773

88-
response = client.projects().locations().queues().tasks().lease(
89-
parent=queue_name, body=lease_options).execute()
74+
task = response.tasks[0]
9075

91-
print('Leased task {}'.format(response))
92-
return response['tasks'][0]
76+
print('Leased task {}'.format(task.name))
77+
return task
9378

9479

9580
def acknowledge_task(task):
9681
"""Acknowledge a given task."""
9782

98-
import googleapiclient.discovery
83+
from google.cloud import tasks_v2beta2
9984

10085
# Create a client.
101-
client = googleapiclient.discovery.build('cloudtasks', 'v2beta2')
86+
client = tasks_v2beta2.CloudTasksClient()
10287

103-
body = {'scheduleTime': task['scheduleTime']}
104-
client.projects().locations().queues().tasks().acknowledge(
105-
name=task['name'], body=body).execute()
88+
# Send request to client to acknowledge task.
89+
client.acknowledge_task(task.name, task.schedule_time)
10690

107-
print('Acknowledged task {}'.format(task['name']))
108-
# [END cloud_tasks_lease_and_acknowledge_task]
91+
print('Acknowledged task {}'.format(task.name))
92+
# [END tasks_lease_and_acknowledge_task]
10993

11094

11195
if __name__ == '__main__':

tasks/pull_queue_snippets_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2017 Google Inc.
1+
# Copyright 2018 Google Inc.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -24,12 +24,13 @@
2424
def test_create_task():
2525
result = pull_queue_snippets.create_task(
2626
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
27-
assert TEST_QUEUE_NAME in result['name']
27+
assert TEST_QUEUE_NAME in result.name
2828

2929

3030
def test_lease_and_ack_task():
3131
pull_queue_snippets.create_task(
3232
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
3333
task = pull_queue_snippets.lease_task(
3434
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
35+
assert TEST_QUEUE_NAME in task.name
3536
pull_queue_snippets.acknowledge_task(task)

tasks/requirements.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
google-api-python-client==1.6.5
2-
google-auth==1.4.1
3-
google-auth-httplib2==0.0.3
1+
google-cloud-tasks==0.2.0

0 commit comments

Comments
 (0)