|
| 1 | +# Google Cloud Tasks Samples |
| 2 | + |
| 3 | +* |
| 4 | +Cloud Tasks is a whitelist-only Alpha. Request an invite here |
| 5 | +https://docs.google.com/forms/d/1g6yRocQ3wtdTArfO4JX8DoqOhYmsoTVgrlFnS0mV1bo/viewform?edit_requested=true |
| 6 | +* |
| 7 | + |
| 8 | +Sample program for interacting with the Cloud Tasks API. |
| 9 | + |
| 10 | +This repo contains samples for interacting with Cloud Tasks pull queues |
| 11 | +and App Engine queues. |
| 12 | + |
| 13 | +`CloudTasksPullQueuesSnippets.java` is a simple command-line program to |
| 14 | +demonstrate listing queues, creating tasks for pull queues, and pulling and |
| 15 | +acknowledging tasks. |
| 16 | + |
| 17 | +The `com.google.cloud.tasks.appenginequeues` package contains both an App Engine |
| 18 | +servlet app to target the tasks at, as well as a command line tool to create |
| 19 | +sample tasks with a payload. The servlet app will store the payload in Cloud |
| 20 | +Datastore so you can verify the task has run. |
| 21 | + |
| 22 | +## Prerequisites to run locally: |
| 23 | + |
| 24 | +The samples require a Java environment with |
| 25 | +[Maven](https://maven.apache.org/what-is-maven.html) installed. |
| 26 | + |
| 27 | +All samples require a Google Cloud Project whitelisted for the Cloud Tasks API. |
| 28 | +To create a project and enable the API, go to the [Google Developers |
| 29 | +Console](https://console.developer.google.com). From there, |
| 30 | + |
| 31 | + * Enable the Cloud Tasks API |
| 32 | + * Enable the Cloud Datastore API (used by the sample App Engine app) |
| 33 | + |
| 34 | +To build the client library, run the following commands: |
| 35 | + |
| 36 | + * cd cloud-tasks-client |
| 37 | + * mvn install |
| 38 | + |
| 39 | +To install the Java application dependencies, run the following commands: |
| 40 | + |
| 41 | + * mvn install |
| 42 | + |
| 43 | +### Authentication |
| 44 | + |
| 45 | +To set up authentication locally, download the |
| 46 | +[Cloud SDK](https://cloud.google.com/sdk), and run |
| 47 | + |
| 48 | + gcloud auth application-default login |
| 49 | + |
| 50 | +On App Engine, authentication credentials will be automatically detected. |
| 51 | + |
| 52 | +On Compute Engine and Container Engine, authentication credentials will be |
| 53 | +automatically detected, but the instances must have been created with the |
| 54 | +necessary scopes. |
| 55 | + |
| 56 | +In any other environment, for example Compute Engine instance without the |
| 57 | +necessary scopes, you should set `GOOGLE_APPLICATION_CREDENTIALS` environment |
| 58 | +variable to a JSON key file for a service account. |
| 59 | + |
| 60 | +See the [authentication guide](https://cloud.google.com/docs/authentication) |
| 61 | +for more information. |
| 62 | + |
| 63 | +## Creating the queues |
| 64 | + |
| 65 | +Queues can not currently be created by the API. To create the queues using the |
| 66 | +Cloud SDK, use the provided queue.yaml: |
| 67 | + |
| 68 | + gcloud app deploy queue.yaml |
| 69 | + |
| 70 | +After running this command, a pull queue with queue ID `my-pull-queue` and an |
| 71 | +App Engine queue with a queue ID of `my-appengine-ID` will be created. |
| 72 | + |
| 73 | +## Running the Pull Queue Samples |
| 74 | + |
| 75 | +Set the following environment variables |
| 76 | + |
| 77 | + export GOOGLE_CLOUD_PROJECT=your-google-cloud-project-id |
| 78 | + export LOCATION_ID=us-central1 |
| 79 | + export QUEUE_ID=my-pull-queue |
| 80 | + |
| 81 | +Run: |
| 82 | + |
| 83 | + ./run_pull_queue_example.sh $GOOGLE_CLOUD_PROJECT $LOCATION_ID $QUEUE_ID |
| 84 | + |
| 85 | +The sample program will list queues, create a task, pull the task, and |
| 86 | +acknowledge the task. |
| 87 | + |
| 88 | +## Running the App Engine Queue samples |
| 89 | + |
| 90 | +### Deploying the App Engine app |
| 91 | + |
| 92 | +To deploy the App Engine app: |
| 93 | + |
| 94 | + mvn clean appengine:deploy |
| 95 | + |
| 96 | +Verify the index page is serving: |
| 97 | + |
| 98 | + gcloud app browse |
| 99 | + |
| 100 | +The App Engine app serves as a target for the push requests. It has an |
| 101 | +endpoint `/payload` that stores the payload it receives in the HTTP POST |
| 102 | +data in Cloud Datastore. The payload can be accessed in your browser at the |
| 103 | +same `/payload` endpoint with a GET request. |
| 104 | + |
| 105 | +### Creating a Task targeting the App Engine app |
| 106 | + |
| 107 | +Set the following environment variables |
| 108 | + |
| 109 | + export GOOGLE_CLOUD_PROJECT=your-google-cloud-project-id |
| 110 | + export LOCATION_ID=us-central1 |
| 111 | + export QUEUE_ID=my-appengine-queue |
| 112 | + export QUEUE_NAME=projects/$GOOGLE_CLOUD_PROJECT/locations/$LOCATION_ID/queues/$QUEUE_ID |
| 113 | + export PAYLOAD=mytaskpayload |
| 114 | + |
| 115 | +Now run the sample app to create a Cloud Task targeting your App Engine app. |
| 116 | + |
| 117 | + ./run_create_appengine_task.sh $QUEUE_NAME $PAYLOAD |
| 118 | + |
| 119 | +Now if you view your App Engine app: |
| 120 | + |
| 121 | + gcloud app browse |
| 122 | + |
| 123 | +You will see `mytaskpayload` value set from the command line argument above |
| 124 | +has been set in Cloud Datastore. |
| 125 | + |
| 126 | +You can look in `CloudTasksAppEngineQueueSnippets.java` to see how the Task |
| 127 | +with an App Engine target was created. |
| 128 | + |
| 129 | +## Testing the Samples |
| 130 | + |
| 131 | +To run the integration tests, you must have deployed the App Engine app using |
| 132 | +the instructions above. |
| 133 | + |
| 134 | +Set the environment variable to your project variables: |
| 135 | + |
| 136 | + export GOOGLE_CLOUD_PROJECT=your-google-cloud-project-id |
| 137 | + export LOCATION_ID=us-central1 |
| 138 | + export APPENGINE_QUEUE_ID=my-appengine-queue |
| 139 | + export PULL_QUEUE_ID=my-pull-queue |
| 140 | + |
| 141 | +Then run: |
| 142 | + |
| 143 | + mvn install -DskipTests=False |
| 144 | + |
| 145 | +## Contributing changes |
| 146 | + |
| 147 | +Contributions are not accepted during Alpha. |
| 148 | + |
| 149 | +## Licensing |
| 150 | + |
| 151 | +* See [LICENSE](LICENSE) |
| 152 | + |
| 153 | + |
0 commit comments