Skip to content

Commit

Permalink
chore: update samples (#207)
Browse files Browse the repository at this point in the history
* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Charles Engelke <github@engelke.com>
  • Loading branch information
3 people authored Jan 13, 2022
1 parent b17ace8 commit 01acd9f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
17 changes: 15 additions & 2 deletions cloud_tasks/snippets/create_http_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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})

Expand Down
12 changes: 12 additions & 0 deletions cloud_tasks/snippets/create_http_task_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 01acd9f

Please sign in to comment.