From 1e56320af989bd7e42ab2cffdcfb6ba88bfd52b4 Mon Sep 17 00:00:00 2001 From: Averi Kitsch Date: Thu, 13 Jan 2022 15:20:08 -0800 Subject: [PATCH] chore: update samples (#207) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: update samples * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update create_http_task_test.py Co-authored-by: Owl Bot Co-authored-by: Charles Engelke --- cloud-tasks/snippets/create_http_task.py | 17 +++++++++++++++-- cloud-tasks/snippets/create_http_task_test.py | 12 ++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/cloud-tasks/snippets/create_http_task.py b/cloud-tasks/snippets/create_http_task.py index e6d7435e1337..40e735b3cb1f 100644 --- a/cloud-tasks/snippets/create_http_task.py +++ b/cloud-tasks/snippets/create_http_task.py @@ -18,13 +18,20 @@ def create_http_task( - project, queue, location, url, payload=None, in_seconds=None, task_name=None + project, + queue, + location, + url, + payload=None, + in_seconds=None, + task_name=None, + deadline=None, ): # [START cloud_tasks_create_http_task] """Create a task for a given queue with an arbitrary payload.""" from google.cloud import tasks_v2 - from google.protobuf import timestamp_pb2 + from google.protobuf import timestamp_pb2, duration_pb2 import datetime import json @@ -39,6 +46,7 @@ def create_http_task( # payload = 'hello' or {'param': 'value'} for application/json # in_seconds = 180 # task_name = 'my-unique-task' + # deadline = 900 # Construct the fully qualified queue name. parent = client.queue_path(project, location, queue) @@ -78,6 +86,11 @@ def create_http_task( # Add the name to tasks. task["name"] = client.task_path(project, location, queue, task_name) + if deadline is not None: + # Add dispatch deadline for requests sent to the worker. + duration = duration_pb2.Duration() + task["dispatch_deadline"] = duration.FromSeconds(deadline) + # Use the client to build and send the task. response = client.create_task(request={"parent": parent, "task": task}) diff --git a/cloud-tasks/snippets/create_http_task_test.py b/cloud-tasks/snippets/create_http_task_test.py index 20cfced96b9c..a66f3287d1e1 100644 --- a/cloud-tasks/snippets/create_http_task_test.py +++ b/cloud-tasks/snippets/create_http_task_test.py @@ -46,3 +46,15 @@ def test_create_http_task(test_queue): TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION, url ) assert TEST_QUEUE_NAME in result.name + + result = create_http_task.create_http_task( + TEST_PROJECT_ID, + TEST_QUEUE_NAME, + TEST_LOCATION, + url, + payload="hello", + in_seconds=180, + task_name=uuid.uuid4().hex, + deadline=900, + ) + assert TEST_QUEUE_NAME in result.name