From 31f6eaefda61ba14d4a8672db73033b674072de2 Mon Sep 17 00:00:00 2001 From: Averi Kitsch Date: Tue, 11 Sep 2018 09:45:45 -0700 Subject: [PATCH 1/3] deleted pull queues --- tasks/README.md | 62 ------------- tasks/pull_queue_snippets.py | 146 ------------------------------ tasks/pull_queue_snippets_test.py | 36 -------- tasks/requirements.txt | 1 - 4 files changed, 245 deletions(-) delete mode 100644 tasks/README.md delete mode 100644 tasks/pull_queue_snippets.py delete mode 100644 tasks/pull_queue_snippets_test.py delete mode 100644 tasks/requirements.txt diff --git a/tasks/README.md b/tasks/README.md deleted file mode 100644 index 407f7db2bee1..000000000000 --- a/tasks/README.md +++ /dev/null @@ -1,62 +0,0 @@ -# Google Cloud Tasks Pull Queue 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=tasks/README.md - -Sample command-line program for interacting with the Google Cloud Tasks API -using pull queues. - -Pull queues let you add tasks to a queue, then programatically remove and -interact with them. Tasks can be added or processed in any environment, -such as on Google App Engine or Google Compute Engine. - -`pull_queue_snippets.py` is a simple command-line program to demonstrate listing queues, - creating tasks, and pulling and acknowledging tasks. - -## Prerequisites to run locally: - -Please refer to [Setting Up a Python Development Environment](https://cloud.google.com/python/setup). - -## Authentication - -To set up authentication, please refer to our -[authentication getting started guide](https://cloud.google.com/docs/authentication/getting-started). - -## Creating a queue - -To create a queue using the Cloud SDK, use the following gcloud command: - - gcloud beta tasks queues create-pull-queue my-pull-queue - -## Running the Samples - -Set the environment variables: - -First, your 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 beta tasks queues list`. - - export QUEUE_ID=my-pull-queue - -And finally the location ID, which can be discovered with -`gcloud beta 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-pull-queue", then the -location is "us-central1"). - - export LOCATION_ID=us-central1 - -Create a task for a queue: - - python pull_queue_snippets.py create-task --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID - -Pull and acknowledge a task: - - python pull_queue_snippets.py lease-and-ack-task --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID - -Note that usually, there would be a processing step in between pulling a task and acknowledging it. diff --git a/tasks/pull_queue_snippets.py b/tasks/pull_queue_snippets.py deleted file mode 100644 index 1f91b074d040..000000000000 --- a/tasks/pull_queue_snippets.py +++ /dev/null @@ -1,146 +0,0 @@ -#!/usr/bin/env python - -# 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. -# 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. - -"""Sample command-line program for interacting with the Cloud Tasks API. - -See README.md for instructions on setting up your development environment -and running the scripts. -""" - -import argparse - - -def create_task(project, queue, location): - # [START cloud_tasks_create_task] - """Create a task for a given queue with an arbitrary payload.""" - - from google.cloud import tasks_v2beta2 - - # Create a client. - client = tasks_v2beta2.CloudTasksClient() - - # Prepare the payload of type bytes. - payload = 'a message for the recipient'.encode() - - # Construct the request body. - task = { - 'pull_message': { - 'payload': payload, - } - } - - # 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) - - print('Created task: {}'.format(response.name)) - return response - # [END cloud_tasks_create_task] - - -def lease_task(project, queue, location): - # [START cloud_tasks_lease_and_acknowledge_task] - """Lease a single task from a given queue for 10 minutes.""" - - from google.cloud import tasks_v2beta2 - - # Create a client. - client = tasks_v2beta2.CloudTasksClient() - - # Construct the fully qualified queue name. - parent = client.queue_path(project, location, queue) - - lease_duration = {'seconds': 600} - - # Send lease request to client. - response = client.lease_tasks( - parent, lease_duration, max_tasks=1, response_view='FULL') - - task = response.tasks[0] - - print('Leased task {}'.format(task.name)) - return task - - -def acknowledge_task(task): - """Acknowledge a given task.""" - - from google.cloud import tasks_v2beta2 - - # Create a client. - client = tasks_v2beta2.CloudTasksClient() - - # Send request to client to acknowledge task. - client.acknowledge_task(task.name, task.schedule_time) - - print('Acknowledged task {}'.format(task.name)) - # [END cloud_tasks_lease_and_acknowledge_task] - - -if __name__ == '__main__': - parser = argparse.ArgumentParser( - description=__doc__, - formatter_class=argparse.RawDescriptionHelpFormatter) - - subparsers = parser.add_subparsers(dest='command') - - create_task_parser = subparsers.add_parser( - 'create-task', - help=create_task.__doc__) - create_task_parser.add_argument( - '--project', - help='Project ID.', - required=True, - ) - create_task_parser.add_argument( - '--queue', - help='Queue ID (short name).', - required=True, - ) - create_task_parser.add_argument( - '--location', - help='Location of the queue, e.g. \'us-central1\'.', - required=True, - ) - - lease_and_ack_parser = subparsers.add_parser( - 'lease-and-ack-task', - help=create_task.__doc__) - lease_and_ack_parser.add_argument( - '--project', - help='Project ID.', - required=True, - ) - lease_and_ack_parser.add_argument( - '--queue', - help='Queue ID (short name).', - required=True, - ) - lease_and_ack_parser.add_argument( - '--location', - help='Location of the queue, e.g. \'us-central1\'.', - required=True, - ) - - args = parser.parse_args() - - if args.command == 'create-task': - create_task(args.project, args.queue, args.location) - if args.command == 'lease-and-ack-task': - task = lease_task(args.project, args.queue, args.location) - acknowledge_task(task) diff --git a/tasks/pull_queue_snippets_test.py b/tasks/pull_queue_snippets_test.py deleted file mode 100644 index 69797f1529e0..000000000000 --- a/tasks/pull_queue_snippets_test.py +++ /dev/null @@ -1,36 +0,0 @@ -# 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. -# 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 pull_queue_snippets - -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-pull-queue') - - -def test_create_task(): - result = pull_queue_snippets.create_task( - TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION) - assert TEST_QUEUE_NAME in result.name - - -def test_lease_and_ack_task(): - pull_queue_snippets.create_task( - TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION) - task = pull_queue_snippets.lease_task( - TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION) - assert TEST_QUEUE_NAME in task.name - pull_queue_snippets.acknowledge_task(task) diff --git a/tasks/requirements.txt b/tasks/requirements.txt deleted file mode 100644 index e46eda2680c7..000000000000 --- a/tasks/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -google-cloud-tasks==0.2.0 From 00b03a0856e9e9acc3a8fb9f811c340d6c931627 Mon Sep 17 00:00:00 2001 From: Averi Kitsch Date: Tue, 11 Sep 2018 09:51:02 -0700 Subject: [PATCH 2/3] updated samples --- appengine/flexible/tasks/README.md | 11 +++++---- .../tasks/create_app_engine_queue_task.py | 24 ++++++++++++------- appengine/flexible/tasks/requirements.txt | 6 ++--- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/appengine/flexible/tasks/README.md b/appengine/flexible/tasks/README.md index 1469225ea3f3..b0bc11b8c789 100644 --- a/appengine/flexible/tasks/README.md +++ b/appengine/flexible/tasks/README.md @@ -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: @@ -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: @@ -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 diff --git a/appengine/flexible/tasks/create_app_engine_queue_task.py b/appengine/flexible/tasks/create_app_engine_queue_task.py index 93c8df4e4cbc..16642d23d9da 100644 --- a/appengine/flexible/tasks/create_app_engine_queue_task.py +++ b/appengine/flexible/tasks/create_app_engine_queue_task.py @@ -18,21 +18,30 @@ 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: @@ -40,7 +49,7 @@ def create_task(project, queue, location, payload=None, in_seconds=None): 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. @@ -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) diff --git a/appengine/flexible/tasks/requirements.txt b/appengine/flexible/tasks/requirements.txt index 0df56e15b324..46b27a2bc282 100644 --- a/appengine/flexible/tasks/requirements.txt +++ b/appengine/flexible/tasks/requirements.txt @@ -1,3 +1,3 @@ -Flask==1.0.2 -gunicorn==19.9.0 -google-cloud-tasks==0.2.0 +Flask==0.12.2 +gunicorn==19.7.1 +google-cloud-tasks==0.3.0 From 04baff96269f98cf3489f359d6de269ee84f10bf Mon Sep 17 00:00:00 2001 From: Averi Kitsch Date: Wed, 12 Sep 2018 12:12:54 -0700 Subject: [PATCH 3/3] fix dependency versions --- appengine/flexible/tasks/requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appengine/flexible/tasks/requirements.txt b/appengine/flexible/tasks/requirements.txt index 46b27a2bc282..b61a2181b701 100644 --- a/appengine/flexible/tasks/requirements.txt +++ b/appengine/flexible/tasks/requirements.txt @@ -1,3 +1,3 @@ -Flask==0.12.2 -gunicorn==19.7.1 +Flask==1.0.2 +gunicorn==19.9.0 google-cloud-tasks==0.3.0