|
| 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 |
0 commit comments