Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename pull to lease and fix name/parent confusion #1311

Merged
merged 3 commits into from
Jan 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions tasks/pull_queue_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ def create_task(project, queue, location):


# [START cloud_tasks_pull_task]
def pull_task(project, queue, location):
"""Pull a single task from a given queue and lease it for 10 minutes."""
def lease_task(project, queue, location):
"""Lease a single task from a given queue for 10 minutes."""

import googleapiclient.discovery

# Create a client.
client = googleapiclient.discovery.build('cloudtasks', 'v2beta2')

duration_seconds = '600s'
pull_options = {
lease_options = {
'maxTasks': 1,
'leaseDuration': duration_seconds,
'responseView': 'FULL'
Expand All @@ -85,10 +85,10 @@ def pull_task(project, queue, location):
queue_name = 'projects/{}/locations/{}/queues/{}'.format(
project, location, queue)

response = client.projects().locations().queues().tasks().pull(
name=queue_name, body=pull_options).execute()
response = client.projects().locations().queues().tasks().lease(
parent=queue_name, body=lease_options).execute()

print('Pulled task {}'.format(response))
print('Leased task {}'.format(response))
return response['tasks'][0]
# [END cloud_tasks_pull_task]

Expand Down Expand Up @@ -120,43 +120,43 @@ def acknowledge_task(task):
help=create_task.__doc__)
create_task_parser.add_argument(
'--project',
help='Project of the queue to add the task to.',
help='Project ID.',
required=True,
)
create_task_parser.add_argument(
'--queue',
help='ID (short name) of the queue to add the task to.',
help='Queue ID (short name).',
required=True,
)
create_task_parser.add_argument(
'--location',
help='Location of the queue to add the task to.',
help='Location of the queue, e.g. \'us-central1\'.',
required=True,
)

pull_and_ack_parser = subparsers.add_parser(
'pull-and-ack-task',
lease_and_ack_parser = subparsers.add_parser(
'lease-and-ack-task',
help=create_task.__doc__)
pull_and_ack_parser.add_argument(
lease_and_ack_parser.add_argument(
'--project',
help='Project of the queue to pull the task from.',
help='Project ID.',
required=True,
)
pull_and_ack_parser.add_argument(
lease_and_ack_parser.add_argument(
'--queue',
help='ID (short name) of the queue to pull the task from.',
help='Queue ID (short name).',
required=True,
)
pull_and_ack_parser.add_argument(
lease_and_ack_parser.add_argument(
'--location',
help='Location of the queue to pull the task from.',
help='Location of the queue, e.g. \'us-central1\'.',
required=True,
)

args = parser.parse_args()

if args.command == 'create-task':
create_task(args.project, args.queue, args.location)
if args.command == 'pull-and-ack-task':
task = pull_task(args.project, args.queue, args.location)
if args.command == 'lease-and-ack-task':
task = lease_task(args.project, args.queue, args.location)
acknowledge_task(task)
4 changes: 2 additions & 2 deletions tasks/pull_queue_snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def test_create_task():
assert TEST_QUEUE_NAME in result['name']


def test_pull_and_ack_task():
def test_lease_and_ack_task():
pull_queue_snippets.create_task(
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
task = pull_queue_snippets.pull_task(
task = pull_queue_snippets.lease_task(
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
pull_queue_snippets.acknowledge_task(task)