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

[Cloud Tasks] Add Http Push Queue Sample #2046

Merged
merged 11 commits into from
Apr 10, 2019
39 changes: 26 additions & 13 deletions appengine/flexible/tasks/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Google Cloud Tasks App Engine Queue Samples
# Google Cloud Tasks Samples

[![Open in Cloud Shell][shell_img]][shell_link]

[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=appengine/flexible/tasks/README.md

Sample command-line program for interacting with the Cloud Tasks API
using App Engine queues.
Sample command-line programs for interacting with the Cloud Tasks API
.

App Engine queues push tasks to an App Engine HTTP target. This directory
contains both the App Engine app to deploy, as well as the snippets to run
Expand Down Expand Up @@ -45,9 +45,14 @@ version unless configured to do otherwise.

Deploy the App Engine app with gcloud:

```
gcloud app deploy
```
* To deploy to the Standard environment:
```
gcloud app deploy app.yaml
```
* To deploy to the Flexible environment:
```
gcloud app deploy app.flexible.yaml
```

Verify the index page is serving:

Expand Down Expand Up @@ -89,23 +94,31 @@ location is "us-central1").
```
export LOCATION_ID=us-central1
```

Running the sample will create a task, targeted at the 'example_task_handler'
### Using App Engine Queues
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
```

Now view that the payload was received and verify the payload:
### Using HTTP Push Queues

Set an environment variable for the endpoint to your task handler. This is an
example url to send requests to the App Engine task handler:
```
gcloud app logs read
export URL=https://<project_id>.appspot.com/example_task_handler
```

Running the sample will create a task and send the task to the specific URL
endpoint, with a payload specified:

```
python create_http_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --url=$URL --payload=hello
```

Create a task that will be scheduled for a time in the future using the
`--in_seconds` flag:
Now view that the payload was received and verify the payload:

```
python create_app_engine_queue_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --payload=hello --in_seconds=30
gcloud app logs read
```
20 changes: 20 additions & 0 deletions appengine/flexible/tasks/app.flexible.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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.

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT --threads=4 main:app

runtime_config:
python_version: 3
9 changes: 2 additions & 7 deletions appengine/flexible/tasks/app.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Google Inc. All Rights Reserved.
# 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.
Expand All @@ -12,9 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
python_version: 3
runtime: python37
2 changes: 1 addition & 1 deletion appengine/flexible/tasks/create_app_engine_queue_task.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Google Inc. All Rights Reserved.
# 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Google Inc. All Rights Reserved.
# 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.
Expand Down
122 changes: 122 additions & 0 deletions appengine/flexible/tasks/create_http_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# 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.

from __future__ import print_function

import argparse
import datetime


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

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

# Create a client.
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'
# url = 'https://<project-id>.appspot.com/example_task_handler'
# payload = 'hello'

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

# Construct the request body.
task = {
'http_request': { # Specify the type of request.
'http_method': 'POST',
'url': url # The full url path that the task will be sent to.
}
}
if payload is not None:
# The API expects a payload of type bytes.
converted_payload = payload.encode()

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

if in_seconds is not None:
# Convert "seconds from now" into an rfc3339 datetime string.
d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)

# Create Timestamp protobuf.
timestamp = timestamp_pb2.Timestamp()
timestamp.FromDatetime(d)

# Add the timestamp to the tasks.
task['schedule_time'] = timestamp

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

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


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=create_http_task.__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)

parser.add_argument(
'--project',
help='Project of the queue to add the task to.',
required=True,
)

parser.add_argument(
'--queue',
help='ID (short name) of the queue to add the task to.',
required=True,
)

parser.add_argument(
'--location',
help='Location of the queue to add the task to.',
required=True,
)

parser.add_argument(
'--url',
help='The full url path that the request will be sent to.',
required=True,
)

parser.add_argument(
'--payload',
help='Optional payload to attach to the push queue.'
)

parser.add_argument(
'--in_seconds', type=int,
help='The number of seconds from now to schedule task attempt.'
)

args = parser.parse_args()

create_http_task(
args.project, args.queue, args.location, args.url,
args.payload, args.in_seconds)
28 changes: 28 additions & 0 deletions appengine/flexible/tasks/create_http_task_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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 create_http_task

TEST_PROJECT_ID = os.getenv('GCLOUD_PROJECT')
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
TEST_QUEUE_NAME = os.getenv('TEST_QUEUE_NAME', 'my-appengine-queue')


def test_create_task():
url = 'https://' + TEST_PROJECT_ID + '.appspot.com/example_task_handler'
result = create_http_task.create_http_task(
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION, url)
assert TEST_QUEUE_NAME in result.name
2 changes: 1 addition & 1 deletion appengine/flexible/tasks/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Google Inc.
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion appengine/flexible/tasks/main_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Google Inc. All Rights Reserved.
# 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.
Expand Down