|
| 1 | +# Google Cloud Tasks Samples |
| 2 | + |
| 3 | +[![Open in Cloud Shell][shell_img]][shell_link] |
| 4 | + |
| 5 | +[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png |
| 6 | +[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 |
| 7 | + |
| 8 | +Sample command-line programs for interacting with the Cloud Tasks API |
| 9 | +. |
| 10 | + |
| 11 | +App Engine queues push tasks to an App Engine HTTP target. This directory |
| 12 | +contains both the App Engine app to deploy, as well as the snippets to run |
| 13 | +locally to push tasks to it, which could also be called on App Engine. |
| 14 | + |
| 15 | +`create_app_engine_queue_task.py` is a simple command-line program to create |
| 16 | +tasks to be pushed to the App Engine app. |
| 17 | + |
| 18 | +`main.py` is the main App Engine app. This app serves as an endpoint to receive |
| 19 | +App Engine task attempts. |
| 20 | + |
| 21 | +`app.yaml` configures the App Engine app. |
| 22 | + |
| 23 | + |
| 24 | +## Prerequisites to run locally: |
| 25 | + |
| 26 | +Please refer to [Setting Up a Python Development Environment](https://cloud.google.com/python/setup). |
| 27 | + |
| 28 | +## Authentication |
| 29 | + |
| 30 | +To set up authentication, please refer to our |
| 31 | +[authentication getting started guide](https://cloud.google.com/docs/authentication/getting-started). |
| 32 | + |
| 33 | +## Creating a queue |
| 34 | + |
| 35 | +To create a queue using the Cloud SDK, use the following gcloud command: |
| 36 | + |
| 37 | +``` |
| 38 | +gcloud beta tasks queues create-app-engine-queue my-appengine-queue |
| 39 | +``` |
| 40 | + |
| 41 | +Note: A newly created queue will route to the default App Engine service and |
| 42 | +version unless configured to do otherwise. |
| 43 | + |
| 44 | +## Deploying the App Engine App |
| 45 | + |
| 46 | +Deploy the App Engine app with gcloud: |
| 47 | + |
| 48 | +* To deploy to the Standard environment: |
| 49 | + ``` |
| 50 | + gcloud app deploy app.yaml |
| 51 | + ``` |
| 52 | +* To deploy to the Flexible environment: |
| 53 | + ``` |
| 54 | + gcloud app deploy app.flexible.yaml |
| 55 | + ``` |
| 56 | + |
| 57 | +Verify the index page is serving: |
| 58 | + |
| 59 | +``` |
| 60 | +gcloud app browse |
| 61 | +``` |
| 62 | + |
| 63 | +The App Engine app serves as a target for the push requests. It has an |
| 64 | +endpoint `/example_task_handler` that reads the payload (i.e., the request body) |
| 65 | +of the HTTP POST request and logs it. The log output can be viewed with: |
| 66 | + |
| 67 | +``` |
| 68 | +gcloud app logs read |
| 69 | +``` |
| 70 | + |
| 71 | +## Run the Sample Using the Command Line |
| 72 | + |
| 73 | +Set environment variables: |
| 74 | + |
| 75 | +First, your project ID: |
| 76 | + |
| 77 | +``` |
| 78 | +export PROJECT_ID=my-project-id |
| 79 | +``` |
| 80 | + |
| 81 | +Then the queue ID, as specified at queue creation time. Queue IDs already |
| 82 | +created can be listed with `gcloud beta tasks queues list`. |
| 83 | + |
| 84 | +``` |
| 85 | +export QUEUE_ID=my-appengine-queue |
| 86 | +``` |
| 87 | + |
| 88 | +And finally the location ID, which can be discovered with |
| 89 | +`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in |
| 90 | +the "name" value (for instance, if the name is |
| 91 | +"projects/my-project/locations/us-central1/queues/my-appengine-queue", then the |
| 92 | +location is "us-central1"). |
| 93 | + |
| 94 | +``` |
| 95 | +export LOCATION_ID=us-central1 |
| 96 | +``` |
| 97 | + |
| 98 | +### Using HTTP Push Queues |
| 99 | + |
| 100 | +Set an environment variable for the endpoint to your task handler. This is an |
| 101 | +example url to send requests to the App Engine task handler: |
| 102 | +``` |
| 103 | +export URL=https://<project_id>.appspot.com/example_task_handler |
| 104 | +``` |
| 105 | + |
| 106 | +Running the sample will create a task and send the task to the specific URL |
| 107 | +endpoint, with a payload specified: |
| 108 | + |
| 109 | +``` |
| 110 | +python create_http_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --url=$URL --payload=hello |
| 111 | +``` |
0 commit comments