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

Add Migration Guide Snippets for Cloud Tasks #2316

Merged
merged 7 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
snippets for migration guide - tests added
  • Loading branch information
averikitsch committed Aug 6, 2019
commit 7dd0ac5bc9532c1ceb8e8a54261c14894653b9ba
130 changes: 85 additions & 45 deletions tasks/migration.py → appengine/flexible/tasks/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@

from google.cloud import tasks

def create_queue():
def create_queue(project, location, queue_blue_name, queue_red_name):
# [START taskqueues_using_yaml]
client = tasks.CloudTasksClient()

project = 'my-project-id'
location = 'us-central1'
# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# location = 'us- central1'
# queue_blue_name = 'queue-blue'
# queue_red_name = 'queue-red'

parent = client.location_path(project, location)

queue_blue = {
'name': client.queue_path(project, location, 'queue-blue'),
'name': client.queue_path(project, location, queue_blue_name),
'rate_limits': {
'max_dispatches_per_second': 5
},
Expand All @@ -34,7 +38,7 @@ def create_queue():
}

queue_red = {
'name': client.queue_path(project, location, 'queue-red'),
'name': client.queue_path(project, location, queue_red_name),
'rate_limits': {
'max_dispatches_per_second': 1
}
Expand All @@ -43,38 +47,46 @@ def create_queue():
queues = [queue_blue, queue_red]
for queue in queues:
response = client.create_queue(parent, queue)
print("Created queue: ")
print(response)
# [END taskqueues_using_yaml]
return response


def update_queue():
def update_queue(project, location, queue):
# [START taskqueues_processing_rate]
client = tasks.CloudTasksClient()

project = 'my-project-id'
location = 'us-central1'
# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# location = 'us- central1'
# queue = 'queue-blue'

# Get queue object
queue_path = client.queue_path(project, location, 'queue-blue')
queue_path = client.queue_path(project, location, queue)
queue = client.get_queue(queue_path)

# Update queue object
queue.rate_limits.max_dispatches_per_second = 20
queue.rate_limits.max_concurrent_dispatches = 10

response = client.update_queue(queue)
print("Updated queue: ")
print(response)
# [END taskqueues_processing_rate]
return response


def create_task():
def create_task(project, location, queue):
# [START taskqueues_new_task]
client = tasks.CloudTasksClient()

project = 'my-project-id'
location = 'us-central1'
queue = 'default'
# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# location = 'us- central1'
# queue = 'default'
amount = 10

parent = client.queue_path(project, location, queue)

task = {
Expand All @@ -92,15 +104,18 @@ def create_task():
eta = response.schedule_time.ToDatetime().strftime("%m/%d/%Y, %H:%M:%S")
print('Task {} enqueued, ETA {}.'.format(response.name, eta))
# [END taskqueues_new_task]
return response

def create_tasks_with_data():
def create_tasks_with_data(project, location, queue):
# [START taskqueues_passing_data]
import json
client = tasks.CloudTasksClient()

project = 'my-project-id'
location = 'us-central1'
queue = 'default'
# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# location = 'us- central1'
# queue = 'default'

parent = client.queue_path(project, location, queue)

task1 = {
Expand All @@ -127,24 +142,28 @@ def create_tasks_with_data():
}
}

tasks = [task1, task2]
for task in tasks:
response = client.create_task(parent, task)
print(response)
response = client.create_task(parent, task1)
print(response)
response = client.create_task(parent, task2)
print(response)
# [END taskqueues_passing_data]
return response


def create_task_with_name():
def create_task_with_name(project, location, queue, task_name):
# [START taskqueues_naming_tasks]
client = tasks.CloudTasksClient()

project = 'my-project-id'
location = 'us-central1'
queue = "default"
# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# location = 'us- central1'
# queue = 'default'
# task_name = 'first-try'

parent = client.queue_path(project, location, queue)

task = {
'name': client.task_path(project, location, queue, 'first-try'),
'name': client.task_path(project, location, queue, task_name),
'app_engine_http_request': {
'http_method': 'GET',
'relative_uri': '/url/path'
Expand All @@ -153,52 +172,72 @@ def create_task_with_name():
response = client.create_task(parent, task)
print(response)
# [END taskqueues_naming_tasks]
return response


def delete_tasks():
def delete_task(project, location, queue):
# [START taskqueues_setup]
client = tasks.CloudTasksClient()

project = 'my-project-id'
location = 'us-central1'
# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# location = 'us- central1'
# queue = 'queue1'
# [START taskqueues_setup]

# [START taskqueues_deleting_tasks]
task_path = client.task_path(project, location, 'queue1', 'foo')
task_path = client.task_path(project, location, queue, 'foo')
response = client.delete_task(task_path)
# [END taskqueues_deleting_tasks]

# [START taskqueues_purging_tasks]
queue_path = client.queue_path(project, location, 'queue1')
queue_path = client.queue_path(project, location, queue)
response = client.purge_queue(queue_path)
# [END taskqueues_purging_tasks]

# [START taskqueues_pause_queue]
queue_path = client.queue_path(project, location, 'queue1')
queue_path = client.queue_path(project, location, queue)
response = client.pause_queue(queue_path)
# [END taskqueues_pause_queues]
return response


def delete_queue(project, location, queue):
client = tasks.CloudTasksClient()

# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# location = 'us- central1'
# queue = 'queue1'

# [START taskqueues_deleting_queues]
queue_path = client.queue_path(project, location, 'queue1')
queue_path = client.queue_path(project, location, queue)
response = client.delete_queue(queue_path)
# [END taskqueues_deleting_queues]
return response


def retry_task():
def retry_task(project, location, fooqueue, barqueue, bazqueue):
# [START taskqueues_retrying_tasks]
from google.protobuf import duration_pb2

client = tasks.CloudTasksClient()

project = 'my-project-id'
location = 'us-central1'
# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# location = 'us- central1'
# fooqueue = 'fooqueue'
# barqueue = 'barqueue'
# bazqueue = 'bazqueue'


parent = client.location_path(project, location)

max_retry = duration_pb2.Duration()
max_retry.seconds = 2*60*60*24

fooqueue = {
'name': client.queue_path(project, location, 'fooqueue'),
foo = {
'name': client.queue_path(project, location, fooqueue),
'rate_limits': {
'max_dispatches_per_second': 1
},
Expand All @@ -214,33 +253,34 @@ def retry_task():
max = duration_pb2.Duration()
max.seconds = 200

barqueue = {
'name': client.queue_path(project, location, 'barqueue'),
bar = {
'name': client.queue_path(project, location, barqueue),
'rate_limits': {
'max_dispatches_per_second': 1
},
'retry_config': {
'min_backoff': min,
'max_backoff': max,
'nax_doublings': 0
'max_doublings': 0
}
}

max.seconds = 300
bazqueue = {
'name': client.queue_path(project, location, 'bazqueue'),
baz = {
'name': client.queue_path(project, location, bazqueue),
'rate_limits': {
'max_dispatches_per_second': 1
},
'retry_config': {
'min_backoff': min,
'max_backoff': max,
'nax_doublings': 3
'max_doublings': 3
}
}

queues = [fooqueue, barqueue, bazqueue]
queues = [foo, bar, baz]
for queue in queues:
response = client.create_queue(parent, queue)
print(response)
# [END taskqueues_retrying_tasks]
return response
109 changes: 109 additions & 0 deletions appengine/flexible/tasks/snippets_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Copyright 2019 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import pytest
import uuid

import snippets

TEST_PROJECT_ID = os.getenv('GCLOUD_PROJECT')
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
QUEUE_NAME_1 = "queue-{}".format(uuid.uuid4())
QUEUE_NAME_2 = "queue-{}".format(uuid.uuid4())

@pytest.mark.order1
def test_create_queue():
name = "projects/{}/locations/{}/queues/{}".format(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_2)
result = snippets.create_queue(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1, QUEUE_NAME_2)
assert name in result.name


@pytest.mark.order2
def test_update_queue():
name = "projects/{}/locations/{}/queues/{}".format(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
result = snippets.update_queue(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
assert name in result.name


@pytest.mark.order3
def test_create_task():
name = "projects/{}/locations/{}/queues/{}".format(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
result = snippets.create_task(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
assert name in result.name


@pytest.mark.order4
def test_create_task_with_data():
name = "projects/{}/locations/{}/queues/{}".format(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
result = snippets.create_tasks_with_data(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
assert name in result.name


@pytest.mark.order5
def test_create_task_with_name():
name = "projects/{}/locations/{}/queues/{}".format(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
result = snippets.create_task_with_name(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1, 'foo')
assert name in result.name


@pytest.mark.order6
def test_delete_task():
name = "projects/{}/locations/{}/queues/{}".format(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
result = snippets.delete_task(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
assert name in result.name


@pytest.mark.order8
def test_delete_queue():
name = "projects/{}/locations/{}/queues/{}".format(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
result = snippets.delete_queue(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_1)
assert None == result

name = "projects/{}/locations/{}/queues/{}".format(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_2)
result = snippets.delete_queue(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME_2)
assert None == result


@pytest.mark.order7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It would make sense if @pytest.mark.order7 came before @pytest.mark.order8 if possible.

def test_retry_task():
QUEUE_NAME = []
for i in range(3):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 should be a constant.

Like QUEUE_SIZE = 3.

Otherwise, we may break the tests if we change L95 without changing L105 vice versa.

QUEUE_NAME.append("queue-{}".format(uuid.uuid4()))

name = "projects/{}/locations/{}/queues/{}".format(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME[2])
result = snippets.retry_task(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME[0], QUEUE_NAME[1], QUEUE_NAME[2])
assert name in result.name

for i in range(3):
snippets.delete_queue(
TEST_PROJECT_ID, TEST_LOCATION, QUEUE_NAME[i])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line could be more readable with named parameters

project=TEST_PROJECT_ID, location=TEST_LOCATION, queue=QUEUE_NAME[i]