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