|
| 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=tasks/README.md |
| 7 | + |
| 8 | +This sample demonstrates how to use the |
| 9 | +[Cloud Tasks](https://cloud.google.com/tasks/docs/) client library. |
| 10 | + |
| 11 | +`create_http_task.py` is a simple command-line program to create |
| 12 | +tasks to be pushed to an URL endpoint. |
| 13 | + |
| 14 | +`create_http_task_with_token.py` is a simple command-line program to create |
| 15 | +tasks to be pushed to an URL endpoint with authorization header. |
| 16 | + |
| 17 | +## Prerequisites to run locally: |
| 18 | + |
| 19 | +Please refer to [Setting Up a Python Development Environment](https://cloud.google.com/python/setup). |
| 20 | + |
| 21 | +## Authentication |
| 22 | + |
| 23 | +To set up authentication, please refer to our |
| 24 | +[authentication getting started guide](https://cloud.google.com/docs/authentication/getting-started). |
| 25 | + |
| 26 | +## Install Dependencies |
| 27 | + |
| 28 | +To install the dependencies for this sample, use the following command: |
| 29 | + |
| 30 | +``` |
| 31 | +pip install -r requirements.txt |
| 32 | +``` |
| 33 | + |
| 34 | +This sample uses the common protos in the [googleapis](https://github.com/googleapis/googleapis) |
| 35 | +repository. For more info, see |
| 36 | +[Protocol Buffer Basics](https://developers.google.com/protocol-buffers/docs/pythontutorial). |
| 37 | + |
| 38 | +## Creating a queue |
| 39 | + |
| 40 | +To create a queue (named `my-queue`) using the Cloud SDK, use the following |
| 41 | +gcloud command: |
| 42 | + |
| 43 | +``` |
| 44 | +gcloud tasks queues create my-queue |
| 45 | +``` |
| 46 | + |
| 47 | +## Run the Sample Using the Command Line |
| 48 | + |
| 49 | +Set environment variables: |
| 50 | + |
| 51 | +First, your project ID: |
| 52 | + |
| 53 | +``` |
| 54 | +export PROJECT_ID=my-project-id |
| 55 | +``` |
| 56 | + |
| 57 | +Then the queue ID, as specified at queue creation time. Queue IDs already |
| 58 | +created can be listed with `gcloud tasks queues list`. |
| 59 | + |
| 60 | +``` |
| 61 | +export QUEUE_ID=my-queue |
| 62 | +``` |
| 63 | + |
| 64 | +And finally the location ID, which can be discovered with |
| 65 | +`gcloud tasks queues describe my-queue`, with the location embedded in |
| 66 | +the "name" value (for instance, if the name is |
| 67 | +"projects/my-project/locations/us-central1/queues/my-queue", then the |
| 68 | +location is "us-central1"). |
| 69 | + |
| 70 | +``` |
| 71 | +export LOCATION_ID=us-central1 |
| 72 | +``` |
| 73 | + |
| 74 | +### Creating Tasks with HTTP Targets |
| 75 | + |
| 76 | +Set an environment variable for the endpoint to your task handler. This is an |
| 77 | +example url: |
| 78 | +``` |
| 79 | +export URL=https://example.com/task_handler |
| 80 | +``` |
| 81 | + |
| 82 | +Running the sample will create a task and send the task to the specific URL |
| 83 | +endpoint, with a payload specified: |
| 84 | + |
| 85 | +``` |
| 86 | +python create_http_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --url=$URL --payload=hello |
| 87 | +``` |
0 commit comments