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

Update Cloud Tasks Samples #1529

Merged
merged 3 commits into from
Jun 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 31 additions & 47 deletions tasks/pull_queue_snippets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

# Copyright 2017 Google Inc.
# Copyright 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -21,91 +21,75 @@
"""

import argparse
import base64


# [START cloud_tasks_create_task]
def create_task(project, queue, location):
# [START tasks_create_task]
"""Create a task for a given queue with an arbitrary payload."""

import googleapiclient.discovery
from google.cloud import tasks_v2beta2

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

# Prepare the payload.
payload = 'a message for the recipient'

# The API expects base64 encoding of the payload, so encode the unicode
# `payload` object into a byte string and base64 encode it.
base64_encoded_payload = base64.b64encode(payload.encode())

# The request body object will be emitted in JSON, which requires
# unicode objects, so convert the byte string to unicode (still base64).
converted_payload = base64_encoded_payload.decode()
# Prepare the payload of type bytes.
payload = 'a message for the recipient'.encode()

# Construct the request body.
task = {
'task': {
'pullMessage': {
'payload': converted_payload
}
'pull_message': {
'payload': payload,
}
}

# Construct the fully qualified queue name.
queue_name = 'projects/{}/locations/{}/queues/{}'.format(
project, location, queue)
parent = client.queue_path(project, location, queue)

# Use the client to build and send the task.
response = client.projects().locations().queues().tasks().create(
parent=queue_name, body=task).execute()
response = client.create_task(parent, task)

print('Created task {}'.format(response['name']))
print('Created task: {}'.format(response.name))
return response
# [END cloud_tasks_create_task]
# [END tasks_create_task]


# [START cloud_tasks_lease_and_acknowledge_task]
def lease_task(project, queue, location):
# [START tasks_lease_and_acknowledge_task]
"""Lease a single task from a given queue for 10 minutes."""

import googleapiclient.discovery
from google.cloud import tasks_v2beta2

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

duration_seconds = '600s'
lease_options = {
'maxTasks': 1,
'leaseDuration': duration_seconds,
'responseView': 'FULL'
}
# Construct the fully qualified queue name.
parent = client.queue_path(project, location, queue)

lease_duration = {'seconds': 600}

queue_name = 'projects/{}/locations/{}/queues/{}'.format(
project, location, queue)
# Send lease request to client.
response = client.lease_tasks(
parent, lease_duration, max_tasks=1, response_view='FULL')

response = client.projects().locations().queues().tasks().lease(
parent=queue_name, body=lease_options).execute()
task = response.tasks[0]

print('Leased task {}'.format(response))
return response['tasks'][0]
print('Leased task {}'.format(task.name))
return task


def acknowledge_task(task):
"""Acknowledge a given task."""

import googleapiclient.discovery
from google.cloud import tasks_v2beta2

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

body = {'scheduleTime': task['scheduleTime']}
client.projects().locations().queues().tasks().acknowledge(
name=task['name'], body=body).execute()
# Send request to client to acknowledge task.
client.acknowledge_task(task.name, task.schedule_time)

print('Acknowledged task {}'.format(task['name']))
# [END cloud_tasks_lease_and_acknowledge_task]
print('Acknowledged task {}'.format(task.name))
# [END tasks_lease_and_acknowledge_task]


if __name__ == '__main__':
Expand Down
5 changes: 3 additions & 2 deletions tasks/pull_queue_snippets_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017 Google Inc.
# Copyright 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -24,12 +24,13 @@
def test_create_task():
result = pull_queue_snippets.create_task(
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
assert TEST_QUEUE_NAME in result['name']
assert TEST_QUEUE_NAME in result.name


def test_lease_and_ack_task():
pull_queue_snippets.create_task(
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
task = pull_queue_snippets.lease_task(
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
assert TEST_QUEUE_NAME in task.name
pull_queue_snippets.acknowledge_task(task)
4 changes: 1 addition & 3 deletions tasks/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
google-api-python-client==1.6.5
google-auth==1.4.1
google-auth-httplib2==0.0.3
google-cloud-tasks==0.2.0