Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit d54f501

Browse files
andrewsgJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Add samples for Cloud Tasks [(#1068)](GoogleCloudPlatform/python-docs-samples#1068)
* Add samples for Cloud Tasks * Respond to tasks sample review * Update app engine queues samples * Address review feedback * Address review issues and convert pull queue sample to not use API key auth * Reform pull queues to match appengine queues changes to auth, command line input, readme * flake8 and fix comment
0 parents  commit d54f501

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed

samples/snippets/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Google Cloud Tasks Pull Queue Samples
2+
3+
Sample command-line program for interacting with the Google Cloud Tasks API
4+
using pull queues.
5+
6+
Pull queues let you add tasks to a queue, then programatically remove and
7+
interact with them. Tasks can be added or processed in any environment,
8+
such as on Google App Engine or Google Compute Engine.
9+
10+
`pull_queue_snippets.py` is a simple command-line program to demonstrate listing queues,
11+
creating tasks, and pulling and acknowledging tasks.
12+
13+
## Prerequisites to run locally:
14+
15+
Please refer to [Setting Up a Python Development Environment](https://cloud.google.com/python/setup).
16+
17+
## Authentication
18+
19+
To set up authentication, please refer to our
20+
[authentication getting started guide](https://cloud.google.com/docs/authentication/getting-started).
21+
22+
## Creating a queue
23+
24+
To create a queue using the Cloud SDK, use the following gcloud command:
25+
26+
gcloud alpha tasks queues create-pull-queue my-pull-queue
27+
28+
## Running the Samples
29+
30+
Set the environment variables:
31+
32+
Set environment variables:
33+
34+
First, your project ID:
35+
36+
export PROJECT_ID=my-project-id
37+
38+
Then the queue ID, as specified at queue creation time. Queue IDs already
39+
created can be listed with `gcloud alpha tasks queues list`.
40+
41+
export QUEUE_ID=my-pull-queue
42+
43+
And finally the location ID, which can be discovered with
44+
`gcloud alpha tasks queues describe $QUEUE_ID`, with the location embedded in
45+
the "name" value (for instance, if the name is
46+
"projects/my-project/locations/us-central1/queues/my-pull-queue", then the
47+
location is "us-central1").
48+
49+
export LOCATION_ID=us-central1
50+
51+
Create a task for a queue:
52+
53+
python pull_queue_snippets.py create-task --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID
54+
55+
Pull and acknowledge a task:
56+
57+
python pull_queue_snippets.py pull-and-ack-task --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID
58+
59+
Note that usually, there would be a processing step in between pulling a task and acknowledging it.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 Google Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Sample command-line program for interacting with the Cloud Tasks API.
18+
19+
See README.md for instructions on setting up your development environment
20+
and running the scripts.
21+
"""
22+
23+
import argparse
24+
import base64
25+
26+
from googleapiclient import discovery
27+
28+
29+
def create_task(project, queue, location):
30+
"""Create a task for a given queue with an arbitrary payload."""
31+
32+
DISCOVERY_URL = (
33+
'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2')
34+
client = discovery.build(
35+
'cloudtasks', 'v2beta2', discoveryServiceUrl=DISCOVERY_URL)
36+
37+
payload = 'a message for the recipient'
38+
task = {
39+
'task': {
40+
'pull_task_target': {
41+
'payload': base64.b64encode(payload)
42+
}
43+
}
44+
}
45+
46+
queue_name = 'projects/{}/locations/{}/queues/{}'.format(
47+
project, location, queue)
48+
49+
response = client.projects().locations().queues().tasks().create(
50+
parent=queue_name, body=task).execute()
51+
52+
print('Created task {}'.format(response['name']))
53+
return response
54+
55+
56+
def pull_task(project, queue, location):
57+
"""Pull a single task from a given queue and lease it for 10 minutes."""
58+
59+
DISCOVERY_URL = (
60+
'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2')
61+
client = discovery.build(
62+
'cloudtasks', 'v2beta2', discoveryServiceUrl=DISCOVERY_URL)
63+
64+
duration_seconds = '600s'
65+
pull_options = {
66+
'max_tasks': 1,
67+
'leaseDuration': duration_seconds,
68+
'responseView': 'FULL'
69+
}
70+
71+
queue_name = 'projects/{}/locations/{}/queues/{}'.format(
72+
project, location, queue)
73+
74+
response = client.projects().locations().queues().tasks().pull(
75+
name=queue_name, body=pull_options).execute()
76+
77+
print('Pulled task {}'.format(response))
78+
return response['tasks'][0]
79+
80+
81+
def acknowledge_task(task):
82+
"""Acknowledge a given task."""
83+
84+
DISCOVERY_URL = (
85+
'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2')
86+
client = discovery.build(
87+
'cloudtasks', 'v2beta2', discoveryServiceUrl=DISCOVERY_URL)
88+
89+
body = {'scheduleTime': task['scheduleTime']}
90+
client.projects().locations().queues().tasks().acknowledge(
91+
name=task['name'], body=body).execute()
92+
93+
print('Acknowledged task {}'.format(task['name']))
94+
95+
96+
if __name__ == '__main__':
97+
parser = argparse.ArgumentParser(
98+
description=__doc__,
99+
formatter_class=argparse.RawDescriptionHelpFormatter)
100+
101+
subparsers = parser.add_subparsers(dest='command')
102+
103+
create_task_parser = subparsers.add_parser(
104+
'create-task',
105+
help=create_task.__doc__)
106+
create_task_parser.add_argument(
107+
'--project',
108+
help='Project of the queue to add the task to.'
109+
)
110+
create_task_parser.add_argument(
111+
'--queue',
112+
help='ID (short name) of the queue to add the task to.'
113+
)
114+
create_task_parser.add_argument(
115+
'--location',
116+
help='Location of the queue to add the task to.'
117+
)
118+
119+
pull_and_ack_parser = subparsers.add_parser(
120+
'pull-and-ack-task',
121+
help=create_task.__doc__)
122+
pull_and_ack_parser.add_argument(
123+
'--project',
124+
help='Project of the queue to pull the task from.'
125+
)
126+
pull_and_ack_parser.add_argument(
127+
'--queue',
128+
help='ID (short name) of the queue to pull the task from.'
129+
)
130+
pull_and_ack_parser.add_argument(
131+
'--location',
132+
help='Location of the queue to pull the task from.'
133+
)
134+
135+
args = parser.parse_args()
136+
137+
if args.command == 'create-task':
138+
create_task(args.project, args.queue, args.location)
139+
if args.command == 'pull-and-ack-task':
140+
task = pull_task(args.project, args.queue, args.location)
141+
acknowledge_task(task)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2017 Google Inc.
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 pull_queue_snippets
18+
19+
TEST_PROJECT_ID = os.getenv('GCLOUD_PROJECT')
20+
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
21+
TEST_QUEUE_NAME = os.getenv('TEST_QUEUE_NAME', 'test-queue')
22+
23+
24+
def test_create_task():
25+
result = pull_queue_snippets.create_task(
26+
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
27+
assert TEST_QUEUE_NAME in result['name']
28+
29+
30+
def test_pull_and_ack_task():
31+
pull_queue_snippets.create_task(
32+
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
33+
task = pull_queue_snippets.pull_task(
34+
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
35+
pull_queue_snippets.acknowledge_task(task)

samples/snippets/queue.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
queue:
2+
- name: my-pull-queue
3+
mode: pull

samples/snippets/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-api-python-client==1.6.0

0 commit comments

Comments
 (0)