diff --git a/appengine/flexible/tasks/README.md b/appengine/flexible/tasks/README.md index b0bc11b8c78..303fbfcfcaf 100644 --- a/appengine/flexible/tasks/README.md +++ b/appengine/flexible/tasks/README.md @@ -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 @@ -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: @@ -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://.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 ``` diff --git a/appengine/flexible/tasks/app.flexible.yaml b/appengine/flexible/tasks/app.flexible.yaml new file mode 100644 index 00000000000..fdc2d9a1d4e --- /dev/null +++ b/appengine/flexible/tasks/app.flexible.yaml @@ -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 diff --git a/appengine/flexible/tasks/app.yaml b/appengine/flexible/tasks/app.yaml index 8bc4a9bb9c8..620fcb3a3dc 100644 --- a/appengine/flexible/tasks/app.yaml +++ b/appengine/flexible/tasks/app.yaml @@ -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. @@ -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 diff --git a/appengine/flexible/tasks/create_app_engine_queue_task.py b/appengine/flexible/tasks/create_app_engine_queue_task.py index a9fb345756f..71db1cae288 100644 --- a/appengine/flexible/tasks/create_app_engine_queue_task.py +++ b/appengine/flexible/tasks/create_app_engine_queue_task.py @@ -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. diff --git a/appengine/flexible/tasks/create_app_engine_queue_task_test.py b/appengine/flexible/tasks/create_app_engine_queue_task_test.py index bbafd7f415f..39edff12993 100644 --- a/appengine/flexible/tasks/create_app_engine_queue_task_test.py +++ b/appengine/flexible/tasks/create_app_engine_queue_task_test.py @@ -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. diff --git a/appengine/flexible/tasks/create_http_task.py b/appengine/flexible/tasks/create_http_task.py new file mode 100644 index 00000000000..6ff3a6a2cfa --- /dev/null +++ b/appengine/flexible/tasks/create_http_task.py @@ -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://.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) diff --git a/appengine/flexible/tasks/create_http_task_test.py b/appengine/flexible/tasks/create_http_task_test.py new file mode 100644 index 00000000000..d452fb771cf --- /dev/null +++ b/appengine/flexible/tasks/create_http_task_test.py @@ -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 diff --git a/appengine/flexible/tasks/main.py b/appengine/flexible/tasks/main.py index 74b4e39d2e1..5af1777595a 100644 --- a/appengine/flexible/tasks/main.py +++ b/appengine/flexible/tasks/main.py @@ -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. diff --git a/appengine/flexible/tasks/main_test.py b/appengine/flexible/tasks/main_test.py index 4c24d41eec5..916f911241c 100644 --- a/appengine/flexible/tasks/main_test.py +++ b/appengine/flexible/tasks/main_test.py @@ -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.