Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 926dd53

Browse files
Update dependency google-cloud-tasks to v2 (GoogleCloudPlatform#4622)
* Update dependency google-cloud-tasks to v2 * fix: update appengine tasks samples for google-cloud-tasks v2 * remove location path * fix datatetime * fix lint Co-authored-by: Bu Sun Kim <[email protected]>
1 parent 05f8f85 commit 926dd53

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

appengine/flexible/tasks/create_app_engine_queue_task.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def create_task(project, queue, location, payload=None, in_seconds=None):
2323
"""Create a task for a given queue with an arbitrary payload."""
2424

2525
from google.cloud import tasks_v2
26-
from google.protobuf import timestamp_pb2
2726

2827
# Create a client.
2928
client = tasks_v2.CloudTasksClient()
@@ -53,17 +52,13 @@ def create_task(project, queue, location, payload=None, in_seconds=None):
5352

5453
if in_seconds is not None:
5554
# Convert "seconds from now" into an rfc3339 datetime string.
56-
d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)
57-
58-
# Create Timestamp protobuf.
59-
timestamp = timestamp_pb2.Timestamp()
60-
timestamp.FromDatetime(d)
55+
timestamp = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)
6156

6257
# Add the timestamp to the tasks.
6358
task['schedule_time'] = timestamp
6459

6560
# Use the client to build and send the task.
66-
response = client.create_task(parent, task)
61+
response = client.create_task(parent=parent, task=task)
6762

6863
print('Created task {}'.format(response.name))
6964
return response
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Flask==1.1.2
22
gunicorn==20.0.4
3-
google-cloud-tasks==1.5.0
3+
google-cloud-tasks==2.0.0

appengine/flexible/tasks/snippets.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create_queue(project, location, queue_blue_name, queue_red_name):
2525
# queue_blue_name = 'queue-blue'
2626
# queue_red_name = 'queue-red'
2727

28-
parent = client.location_path(project, location)
28+
parent = f"projects/{project}/locations/{location}"
2929

3030
queue_blue = {
3131
'name': client.queue_path(project, location, queue_blue_name),
@@ -47,7 +47,7 @@ def create_queue(project, location, queue_blue_name, queue_red_name):
4747

4848
queues = [queue_blue, queue_red]
4949
for queue in queues:
50-
response = client.create_queue(parent, queue)
50+
response = client.create_queue(parent=parent, queue=queue)
5151
print(response)
5252
# [END taskqueues_using_yaml]
5353
return response
@@ -64,13 +64,13 @@ def update_queue(project, location, queue):
6464

6565
# Get queue object
6666
queue_path = client.queue_path(project, location, queue)
67-
queue = client.get_queue(queue_path)
67+
queue = client.get_queue(name=queue_path)
6868

6969
# Update queue object
7070
queue.rate_limits.max_dispatches_per_second = 20
7171
queue.rate_limits.max_concurrent_dispatches = 10
7272

73-
response = client.update_queue(queue)
73+
response = client.update_queue(queue=queue)
7474
print(response)
7575
# [END taskqueues_processing_rate]
7676
return response
@@ -99,8 +99,8 @@ def create_task(project, location, queue):
9999
}
100100
}
101101

102-
response = client.create_task(parent, task)
103-
eta = response.schedule_time.ToDatetime().strftime("%m/%d/%Y, %H:%M:%S")
102+
response = client.create_task(parent=parent, task=task)
103+
eta = response.schedule_time.strftime("%m/%d/%Y, %H:%M:%S")
104104
print('Task {} enqueued, ETA {}.'.format(response.name, eta))
105105
# [END taskqueues_new_task]
106106
return response
@@ -142,9 +142,9 @@ def create_tasks_with_data(project, location, queue):
142142
}
143143
}
144144

145-
response = client.create_task(parent, task1)
145+
response = client.create_task(parent=parent, task=task1)
146146
print(response)
147-
response = client.create_task(parent, task2)
147+
response = client.create_task(parent=parent, task=task2)
148148
print(response)
149149
# [END taskqueues_passing_data]
150150
return response
@@ -169,7 +169,7 @@ def create_task_with_name(project, location, queue, task_name):
169169
'relative_uri': '/url/path'
170170
}
171171
}
172-
response = client.create_task(parent, task)
172+
response = client.create_task(parent=parent, task=task)
173173
print(response)
174174
# [END taskqueues_naming_tasks]
175175
return response
@@ -185,7 +185,7 @@ def delete_task(project, location, queue):
185185
# queue = 'queue1'
186186

187187
task_path = client.task_path(project, location, queue, 'foo')
188-
response = client.delete_task(task_path)
188+
response = client.delete_task(name=task_path)
189189
# [END taskqueues_deleting_tasks]
190190
return response
191191

@@ -200,7 +200,7 @@ def purge_queue(project, location, queue):
200200
# queue = 'queue1'
201201

202202
queue_path = client.queue_path(project, location, queue)
203-
response = client.purge_queue(queue_path)
203+
response = client.purge_queue(name=queue_path)
204204
# [END taskqueues_purging_tasks]
205205
return response
206206

@@ -215,7 +215,7 @@ def pause_queue(project, location, queue):
215215
# queue = 'queue1'
216216

217217
queue_path = client.queue_path(project, location, queue)
218-
response = client.pause_queue(queue_path)
218+
response = client.pause_queue(name=queue_path)
219219
# [END taskqueues_pause_queue]
220220
return response
221221

@@ -230,7 +230,7 @@ def delete_queue(project, location, queue):
230230
# queue = 'queue1'
231231

232232
queue_path = client.queue_path(project, location, queue)
233-
response = client.delete_queue(queue_path)
233+
response = client.delete_queue(name=queue_path)
234234
# [END taskqueues_deleting_queues]
235235
return response
236236

@@ -248,7 +248,7 @@ def retry_task(project, location, fooqueue, barqueue, bazqueue):
248248
# barqueue = 'barqueue'
249249
# bazqueue = 'bazqueue'
250250

251-
parent = client.location_path(project, location)
251+
parent = f"projects/{project}/locations/{location}"
252252

253253
max_retry = duration_pb2.Duration()
254254
max_retry.seconds = 2*60*60*24
@@ -297,7 +297,7 @@ def retry_task(project, location, fooqueue, barqueue, bazqueue):
297297

298298
queues = [foo, bar, baz]
299299
for queue in queues:
300-
response = client.create_queue(parent, queue)
300+
response = client.create_queue(parent=parent, queue=queue)
301301
print(response)
302302
# [END taskqueues_retrying_tasks]
303303
return response

0 commit comments

Comments
 (0)