Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit 26270d5

Browse filesBrowse files
authored
Add Migration Guide Snippets for Cloud Tasks [(#2316)](GoogleCloudPlatform/python-docs-samples#2316)
* Migration guide * remove app and update migraitonn * snippets for migration guide - tests added * lint * remove print statements * Styling changes * Travis trigger
1 parent fce8e57 commit 26270d5
Copy full SHA for 26270d5

File tree

Expand file treeCollapse file tree

2 files changed

+395
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+395
-0
lines changed
Open diff view settings
Collapse file
+285Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
# Copyright 2019 Google LLC All Rights Reserved.
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+
from google.cloud import tasks
16+
17+
18+
def create_queue(project, location, queue_blue_name, queue_red_name):
19+
# [START taskqueues_using_yaml]
20+
client = tasks.CloudTasksClient()
21+
22+
# TODO(developer): Uncomment these lines and replace with your values.
23+
# project = 'my-project-id'
24+
# location = 'us- central1'
25+
# queue_blue_name = 'queue-blue'
26+
# queue_red_name = 'queue-red'
27+
28+
parent = client.location_path(project, location)
29+
30+
queue_blue = {
31+
'name': client.queue_path(project, location, queue_blue_name),
32+
'rate_limits': {
33+
'max_dispatches_per_second': 5
34+
},
35+
'app_engine_routing_override': {
36+
'version': 'v2',
37+
'service': 'task-module'
38+
}
39+
}
40+
41+
queue_red = {
42+
'name': client.queue_path(project, location, queue_red_name),
43+
'rate_limits': {
44+
'max_dispatches_per_second': 1
45+
}
46+
}
47+
48+
queues = [queue_blue, queue_red]
49+
for queue in queues:
50+
response = client.create_queue(parent, queue)
51+
print(response)
52+
# [END taskqueues_using_yaml]
53+
return response
54+
55+
56+
def update_queue(project, location, queue):
57+
# [START taskqueues_processing_rate]
58+
client = tasks.CloudTasksClient()
59+
60+
# TODO(developer): Uncomment these lines and replace with your values.
61+
# project = 'my-project-id'
62+
# location = 'us- central1'
63+
# queue = 'queue-blue'
64+
65+
# Get queue object
66+
queue_path = client.queue_path(project, location, queue)
67+
queue = client.get_queue(queue_path)
68+
69+
# Update queue object
70+
queue.rate_limits.max_dispatches_per_second = 20
71+
queue.rate_limits.max_concurrent_dispatches = 10
72+
73+
response = client.update_queue(queue)
74+
print(response)
75+
# [END taskqueues_processing_rate]
76+
return response
77+
78+
79+
def create_task(project, location, queue):
80+
# [START taskqueues_new_task]
81+
client = tasks.CloudTasksClient()
82+
83+
# TODO(developer): Uncomment these lines and replace with your values.
84+
# project = 'my-project-id'
85+
# location = 'us- central1'
86+
# queue = 'default'
87+
amount = 10
88+
89+
parent = client.queue_path(project, location, queue)
90+
91+
task = {
92+
'app_engine_http_request': {
93+
'http_method': 'POST',
94+
'relative_uri': '/update_counter',
95+
'app_engine_routing': {
96+
'service': 'worker'
97+
},
98+
'body': str(amount).encode()
99+
}
100+
}
101+
102+
response = client.create_task(parent, task)
103+
eta = response.schedule_time.ToDatetime().strftime("%m/%d/%Y, %H:%M:%S")
104+
print('Task {} enqueued, ETA {}.'.format(response.name, eta))
105+
# [END taskqueues_new_task]
106+
return response
107+
108+
109+
def create_tasks_with_data(project, location, queue):
110+
# [START taskqueues_passing_data]
111+
import json
112+
client = tasks.CloudTasksClient()
113+
114+
# TODO(developer): Uncomment these lines and replace with your values.
115+
# project = 'my-project-id'
116+
# location = 'us- central1'
117+
# queue = 'default'
118+
119+
parent = client.queue_path(project, location, queue)
120+
121+
task1 = {
122+
'app_engine_http_request': {
123+
'http_method': 'POST',
124+
'relative_uri': '/update_counter?key=blue',
125+
'app_engine_routing': {
126+
'service': 'worker'
127+
}
128+
}
129+
}
130+
131+
task2 = {
132+
'app_engine_http_request': {
133+
'http_method': 'POST',
134+
'relative_uri': '/update_counter',
135+
'app_engine_routing': {
136+
'service': 'worker'
137+
},
138+
'headers': {
139+
'Content-Type': 'application/json'
140+
},
141+
'body': json.dumps({'key': 'blue'}).encode()
142+
}
143+
}
144+
145+
response = client.create_task(parent, task1)
146+
print(response)
147+
response = client.create_task(parent, task2)
148+
print(response)
149+
# [END taskqueues_passing_data]
150+
return response
151+
152+
153+
def create_task_with_name(project, location, queue, task_name):
154+
# [START taskqueues_naming_tasks]
155+
client = tasks.CloudTasksClient()
156+
157+
# TODO(developer): Uncomment these lines and replace with your values.
158+
# project = 'my-project-id'
159+
# location = 'us- central1'
160+
# queue = 'default'
161+
# task_name = 'first-try'
162+
163+
parent = client.queue_path(project, location, queue)
164+
165+
task = {
166+
'name': client.task_path(project, location, queue, task_name),
167+
'app_engine_http_request': {
168+
'http_method': 'GET',
169+
'relative_uri': '/url/path'
170+
}
171+
}
172+
response = client.create_task(parent, task)
173+
print(response)
174+
# [END taskqueues_naming_tasks]
175+
return response
176+
177+
178+
def delete_task(project, location, queue):
179+
# [START taskqueues_setup]
180+
client = tasks.CloudTasksClient()
181+
182+
# TODO(developer): Uncomment these lines and replace with your values.
183+
# project = 'my-project-id'
184+
# location = 'us- central1'
185+
# queue = 'queue1'
186+
# [START taskqueues_setup]
187+
188+
# [START taskqueues_deleting_tasks]
189+
task_path = client.task_path(project, location, queue, 'foo')
190+
response = client.delete_task(task_path)
191+
# [END taskqueues_deleting_tasks]
192+
193+
# [START taskqueues_purging_tasks]
194+
queue_path = client.queue_path(project, location, queue)
195+
response = client.purge_queue(queue_path)
196+
# [END taskqueues_purging_tasks]
197+
198+
# [START taskqueues_pause_queue]
199+
queue_path = client.queue_path(project, location, queue)
200+
response = client.pause_queue(queue_path)
201+
# [END taskqueues_pause_queues]
202+
return response
203+
204+
205+
def delete_queue(project, location, queue):
206+
client = tasks.CloudTasksClient()
207+
208+
# TODO(developer): Uncomment these lines and replace with your values.
209+
# project = 'my-project-id'
210+
# location = 'us- central1'
211+
# queue = 'queue1'
212+
213+
# [START taskqueues_deleting_queues]
214+
queue_path = client.queue_path(project, location, queue)
215+
response = client.delete_queue(queue_path)
216+
# [END taskqueues_deleting_queues]
217+
return response
218+
219+
220+
def retry_task(project, location, fooqueue, barqueue, bazqueue):
221+
# [START taskqueues_retrying_tasks]
222+
from google.protobuf import duration_pb2
223+
224+
client = tasks.CloudTasksClient()
225+
226+
# TODO(developer): Uncomment these lines and replace with your values.
227+
# project = 'my-project-id'
228+
# location = 'us- central1'
229+
# fooqueue = 'fooqueue'
230+
# barqueue = 'barqueue'
231+
# bazqueue = 'bazqueue'
232+
233+
parent = client.location_path(project, location)
234+
235+
max_retry = duration_pb2.Duration()
236+
max_retry.seconds = 2*60*60*24
237+
238+
foo = {
239+
'name': client.queue_path(project, location, fooqueue),
240+
'rate_limits': {
241+
'max_dispatches_per_second': 1
242+
},
243+
'retry_config': {
244+
'max_attempts': 7,
245+
'max_retry_duration': max_retry
246+
}
247+
}
248+
249+
min = duration_pb2.Duration()
250+
min.seconds = 10
251+
252+
max = duration_pb2.Duration()
253+
max.seconds = 200
254+
255+
bar = {
256+
'name': client.queue_path(project, location, barqueue),
257+
'rate_limits': {
258+
'max_dispatches_per_second': 1
259+
},
260+
'retry_config': {
261+
'min_backoff': min,
262+
'max_backoff': max,
263+
'max_doublings': 0
264+
}
265+
}
266+
267+
max.seconds = 300
268+
baz = {
269+
'name': client.queue_path(project, location, bazqueue),
270+
'rate_limits': {
271+
'max_dispatches_per_second': 1
272+
},
273+
'retry_config': {
274+
'min_backoff': min,
275+
'max_backoff': max,
276+
'max_doublings': 3
277+
}
278+
}
279+
280+
queues = [foo, bar, baz]
281+
for queue in queues:
282+
response = client.create_queue(parent, queue)
283+
print(response)
284+
# [END taskqueues_retrying_tasks]
285+
return response
Collapse file
+110Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Copyright 2019 Google LLC All Rights Reserved.
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+
import pytest
17+
import uuid
18+
19+
import snippets
20+
21+
TEST_PROJECT_ID = os.getenv('GCLOUD_PROJECT')
22+
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
23+
QUEUE_NAME_1 = "queue-{}".format(uuid.uuid4())
24+
QUEUE_NAME_2 = "queue-{}".format(uuid.uuid4())
25+
26+
27+
@pytest.mark.order1
28+
def test_create_queue():
29+
name = "projects/{}/locations/{}/queues/{}".format(
30+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_2)
31+
result = snippets.create_queue(
32+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1, QUEUE_NAME_2)
33+
assert name in result.name
34+
35+
36+
@pytest.mark.order2
37+
def test_update_queue():
38+
name = "projects/{}/locations/{}/queues/{}".format(
39+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
40+
result = snippets.update_queue(
41+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
42+
assert name in result.name
43+
44+
45+
@pytest.mark.order3
46+
def test_create_task():
47+
name = "projects/{}/locations/{}/queues/{}".format(
48+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
49+
result = snippets.create_task(
50+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
51+
assert name in result.name
52+
53+
54+
@pytest.mark.order4
55+
def test_create_task_with_data():
56+
name = "projects/{}/locations/{}/queues/{}".format(
57+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
58+
result = snippets.create_tasks_with_data(
59+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
60+
assert name in result.name
61+
62+
63+
@pytest.mark.order5
64+
def test_create_task_with_name():
65+
name = "projects/{}/locations/{}/queues/{}".format(
66+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
67+
result = snippets.create_task_with_name(
68+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1, 'foo')
69+
assert name in result.name
70+
71+
72+
@pytest.mark.order6
73+
def test_delete_task():
74+
name = "projects/{}/locations/{}/queues/{}".format(
75+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
76+
result = snippets.delete_task(
77+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
78+
assert name in result.name
79+
80+
81+
@pytest.mark.order7
82+
def test_delete_queue():
83+
result = snippets.delete_queue(
84+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
85+
assert result is None
86+
87+
result = snippets.delete_queue(
88+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_2)
89+
assert result is None
90+
91+
92+
@pytest.mark.order8
93+
def test_retry_task():
94+
QUEUE_SIZE = 3
95+
QUEUE_NAME = []
96+
for i in range(QUEUE_SIZE):
97+
QUEUE_NAME.append("queue-{}".format(uuid.uuid4()))
98+
99+
name = "projects/{}/locations/{}/queues/{}".format(
100+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME[2])
101+
result = snippets.retry_task(
102+
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME[0], QUEUE_NAME[1],
103+
QUEUE_NAME[2])
104+
assert name in result.name
105+
106+
for i in range(QUEUE_SIZE):
107+
snippets.delete_queue(
108+
project=TEST_PROJECT_ID,
109+
location=TEST_LOCATION,
110+
queue=QUEUE_NAME[i])

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.