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 Push Queue Sample #1698

Merged
merged 3 commits into from
Sep 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
11 changes: 6 additions & 5 deletions appengine/flexible/tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ gcloud beta tasks queues create-app-engine-queue my-appengine-queue
Note: A newly created queue will route to the default App Engine service and
version unless configured to do otherwise.

## Deploying the App Engine app
## Deploying the App Engine App

Deploy the App Engine app with gcloud:

Expand All @@ -56,14 +56,14 @@ gcloud app browse
```

The App Engine app serves as a target for the push requests. It has an
endpoint `/log_payload` that reads the payload (i.e., the request body) of the
HTTP POST request and logs it. The log output can be viewed with:
endpoint `/example_task_handler` that reads the payload (i.e., the request body)
of the HTTP POST request and logs it. The log output can be viewed with:

```
gcloud app logs read
```

## Running the Samples
## Run the Sample Using the Command Line

Set environment variables:

Expand All @@ -90,7 +90,8 @@ location is "us-central1").
export LOCATION_ID=us-central1
```

Create a task, targeted at the `log_payload` endpoint, with a payload specified:
Running the sample will create a task, targeted at the 'example_task_handler'
endpoint, with a payload specified:

```
python create_app_engine_queue_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --payload=hello
Expand Down
24 changes: 15 additions & 9 deletions appengine/flexible/tasks/create_app_engine_queue_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,38 @@
import datetime


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

from google.cloud import tasks_v2beta2
from google.cloud import tasks_v2beta3
from google.protobuf import timestamp_pb2

# Create a client.
client = tasks_v2beta2.CloudTasksClient()
client = tasks_v2beta3.CloudTasksClient()

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

# Construct the fully qualified queue name.
parent = client.queue_path(project, location, queue)

# Construct the request body.
task = {
'app_engine_http_request': { # Specify the type of request.
'http_method': 'POST',
'relative_url': '/example_task_handler'
'relative_uri': '/example_task_handler'
}
}
if payload is not None:
# The API expects a payload of type bytes.
converted_payload = payload.encode()

# Add the payload to the request.
task['app_engine_http_request']['payload'] = converted_payload
task['app_engine_http_request']['body'] = converted_payload

if in_seconds is not None:
# Convert "seconds from now" into an rfc3339 datetime string.
Expand All @@ -50,12 +59,9 @@ def create_task(project, queue, location, payload=None, in_seconds=None):
timestamp = timestamp_pb2.Timestamp()
timestamp.FromDatetime(d)

# Add the rfc3339 datetime string to the request.
# Add the timestamp to the tasks.
task['schedule_time'] = timestamp

# Construct the fully qualified queue name.
parent = client.queue_path(project, location, queue)

# Use the client to build and send the task.
response = client.create_task(parent, task)

Expand Down
2 changes: 1 addition & 1 deletion appengine/flexible/tasks/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==1.0.2
gunicorn==19.9.0
google-cloud-tasks==0.2.0
google-cloud-tasks==0.3.0
62 changes: 0 additions & 62 deletions tasks/README.md

This file was deleted.

146 changes: 0 additions & 146 deletions tasks/pull_queue_snippets.py

This file was deleted.

36 changes: 0 additions & 36 deletions tasks/pull_queue_snippets_test.py

This file was deleted.

1 change: 0 additions & 1 deletion tasks/requirements.txt

This file was deleted.