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 Tasks Sample for App Engine #1541

Merged
merged 4 commits into from
Jun 27, 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
40 changes: 30 additions & 10 deletions appengine/flexible/tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ To set up authentication, please refer to our

To create a queue using the Cloud SDK, use the following gcloud command:

gcloud alpha tasks queues create-app-engine-queue my-appengine-queue
```
gcloud alpha tasks queues create app-engine my-appengine-queue
```

Note: A newly created queue will route to the default App Engine service and
version unless configured to do otherwise.
Expand All @@ -43,48 +45,66 @@ version unless configured to do otherwise.

Deploy the App Engine app with gcloud:

gcloud app deploy
```
gcloud app deploy
```

Verify the index page is serving:

gcloud app browse
```
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:

gcloud app logs read
```
gcloud app logs read
```

## Running the Samples

Set environment variables:

First, your project ID:

export PROJECT_ID=my-project-id
```
export PROJECT_ID=my-project-id
```

Then the queue ID, as specified at queue creation time. Queue IDs already
created can be listed with `gcloud alpha tasks queues list`.

export QUEUE_ID=my-appengine-queue
```
export QUEUE_ID=my-appengine-queue
```

And finally the location ID, which can be discovered with
`gcloud alpha tasks queues describe $QUEUE_ID`, with the location embedded in
the "name" value (for instance, if the name is
"projects/my-project/locations/us-central1/queues/my-appengine-queue", then the
location is "us-central1").

export LOCATION_ID=us-central1
```
export LOCATION_ID=us-central1
```

Create a task, targeted at the `log_payload` endpoint, with a payload specified:

python create_app_engine_queue_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --payload=hello
```
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:

gcloud app logs read
```
gcloud app logs read
```

Create a task that will be scheduled for a time in the future using the
`--in_seconds` flag:

python create_app_engine_queue_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --payload=hello --in_seconds=30
```
python create_app_engine_queue_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --payload=hello --in_seconds=30
```
14 changes: 14 additions & 0 deletions appengine/flexible/tasks/app.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2018 Google Inc. All Rights Reserved.
Copy link
Member

Choose a reason for hiding this comment

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

We don't need the copyright header for .yaml files, I believe.

#
# 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 main:app
Expand Down
49 changes: 19 additions & 30 deletions appengine/flexible/tasks/create_app_engine_queue_task.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017 Google Inc. All Rights Reserved.
# Copyright 2018 Google Inc. 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 @@ -15,62 +15,51 @@
from __future__ import print_function

import argparse
import base64
import datetime
import json


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

import googleapiclient.discovery
from google.cloud import tasks_v2beta2
from google.protobuf import timestamp_pb2

# Create a client.
client = googleapiclient.discovery.build('cloudtasks', 'v2beta2')
client = tasks_v2beta2.CloudTasksClient()

# Construct the request body.
url = '/example_task_handler'
body = {
'task': {
'appEngineHttpRequest': { # Specify the type of request.
'httpMethod': 'POST',
'relativeUrl': url
task = {
'app_engine_http_request': { # Specify the type of request.
'http_method': 'POST',
'relative_url': '/example_task_handler'
}
}
}

if payload is not None:
# The API expects base64 encoding of the payload, so encode the unicode
# `payload` object into a byte string and base64 encode it.
base64_encoded_payload = base64.b64encode(payload.encode())

# The request body object will be emitted in JSON, which requires
# unicode objects, so convert the byte string to unicode, still base64.
converted_payload = base64_encoded_payload.decode()
# The API expects a payload of type bytes.
converted_payload = payload.encode()

# Add the payload to the request.
body['task']['appEngineHttpRequest']['payload'] = converted_payload
task['app_engine_http_request']['payload'] = 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)
scheduled_time = d.isoformat('T') + 'Z'

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

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

# Construct the fully qualified queue name.
queue_name = 'projects/{}/locations/{}/queues/{}'.format(
project, location, queue)

print('Sending task {}'.format(json.dumps(body)))
parent = client.queue_path(project, location, queue)

# Use the client to build and send the task.
response = client.projects().locations().queues().tasks().create(
parent=queue_name, body=body).execute()
response = client.create_task(parent, task)

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

Expand Down
24 changes: 9 additions & 15 deletions appengine/flexible/tasks/create_app_engine_queue_task_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016 Google Inc. All Rights Reserved.
# Copyright 2018 Google Inc. 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,22 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import mock
import os

import create_app_engine_queue_task

TEST_PROJECT = 'mock-project'
TEST_LOCATION = 'us-central1'
TEST_QUEUE = 'my-appengine-queue'
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')


@mock.patch('googleapiclient.discovery.build')
def test_create_task(build):
projects = build.return_value.projects.return_value
locations = projects.locations.return_value
create_function = locations.queues.return_value.tasks.return_value.create
execute_function = create_function.return_value.execute
execute_function.return_value = {'name': 'task_name'}
create_app_engine_queue_task.create_task(
TEST_PROJECT, TEST_QUEUE, TEST_LOCATION)
assert execute_function.called
def test_create_task():
result = create_app_engine_queue_task.create_task(
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
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 2016 Google Inc.
# Copyright 2018 Google Inc.
#
# 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 2016 Google Inc. All Rights Reserved.
# Copyright 2018 Google Inc. 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
6 changes: 1 addition & 5 deletions appengine/flexible/tasks/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
Flask==0.12.2
google-api-python-client==1.6.5
google-auth==1.4.1
google-auth-httplib2==0.0.3
google-cloud-datastore==1.6.0
gunicorn==19.7.1
google-cloud-tasks==0.2.0
2 changes: 1 addition & 1 deletion tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ To set up authentication, please refer to our

To create a queue using the Cloud SDK, use the following gcloud command:

gcloud alpha tasks queues create-pull-queue my-pull-queue
gcloud alpha tasks queues create pull my-pull-queue

## Running the Samples

Expand Down