|
| 1 | +# Google Cloud Tasks App Engine Queue Samples |
| 2 | + |
| 3 | +Sample command-line program for interacting with the Cloud Tasks API |
| 4 | +using App Engine queues. |
| 5 | + |
| 6 | +App Engine queues push tasks to an App Engine HTTP target. This directory |
| 7 | +contains both the App Engine app to deploy, as well as the snippets to run |
| 8 | +locally to push tasks to it, which could also be called on App Engine. |
| 9 | + |
| 10 | +`CreateTask.java` is a simple command-line program to create |
| 11 | +tasks to be pushed to the App Engine app. |
| 12 | + |
| 13 | +`TaskServlet.java` is the main App Engine app. This app serves as an endpoint to receive |
| 14 | +App Engine task attempts. |
| 15 | + |
| 16 | + |
| 17 | +## Initial Setup |
| 18 | + |
| 19 | + * Set up a Google Cloud Project and enable billing. |
| 20 | + * Enable the |
| 21 | + [Cloud Tasks API](https://console.cloud.google.com/launcher/details/google/cloudtasks.googleapis.com). |
| 22 | + * Download and install the [Cloud SDK](https://cloud.google.com/sdk). |
| 23 | + * Download and install [Maven](http://maven.apache.org/install.html). |
| 24 | + |
| 25 | +## Creating a queue |
| 26 | + |
| 27 | +To create a queue using the Cloud SDK, use the following gcloud command: |
| 28 | + |
| 29 | +``` |
| 30 | +gcloud beta tasks queues create-app-engine-queue my-appengine-queue |
| 31 | +``` |
| 32 | + |
| 33 | +Note: A newly created queue will route to the default App Engine service and |
| 34 | +version unless configured to do otherwise. |
| 35 | + |
| 36 | +## Deploying the App Engine app |
| 37 | +[Using Maven and the App Engine Plugin](https://cloud.google.com/appengine/docs/flexible/java/using-maven) |
| 38 | +& [Maven Plugin Goals and Parameters](https://cloud.google.com/appengine/docs/flexible/java/maven-reference) |
| 39 | + |
| 40 | +### Running locally |
| 41 | + |
| 42 | +``` |
| 43 | +mvn appengine:run |
| 44 | +``` |
| 45 | +### Deploying |
| 46 | + |
| 47 | +``` |
| 48 | +mvn appengine:deploy |
| 49 | +``` |
| 50 | + |
| 51 | +## Running the Sample |
| 52 | + |
| 53 | +Set environment variables: |
| 54 | + |
| 55 | +First, your project ID: |
| 56 | + |
| 57 | +``` |
| 58 | +export GOOGLE_CLOUD_PROJECT=<YOUR_PROJECT_ID> |
| 59 | +``` |
| 60 | + |
| 61 | +Then the queue ID, as specified at queue creation time. Queue IDs already |
| 62 | +created can be listed with `gcloud beta tasks queues list`. |
| 63 | + |
| 64 | +``` |
| 65 | +export QUEUE_ID=my-appengine-queue |
| 66 | +``` |
| 67 | + |
| 68 | +And finally the location ID, which can be discovered with |
| 69 | +`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in |
| 70 | +the "name" value (for instance, if the name is |
| 71 | +"projects/my-project/locations/us-central1/queues/my-appengine-queue", then the |
| 72 | +location is "us-central1"). |
| 73 | + |
| 74 | +``` |
| 75 | +export LOCATION_ID=<YOUR_ZONE> |
| 76 | +``` |
| 77 | + |
| 78 | +Create a task, targeted at the `/tasks/create` endpoint, with a payload specified: |
| 79 | + |
| 80 | +``` |
| 81 | +mvn exec:java -Dexec.mainClass="com.example.task.CreateTask" \ |
| 82 | + -Dexec.args="--project-id $GOOGLE_CLOUD_PROJECT \ |
| 83 | + --queue $QUEUE_ID --location $LOCATION_ID --payload hello" |
| 84 | +``` |
| 85 | + |
| 86 | +The App Engine app serves as a target for the push requests. It has an |
| 87 | +endpoint `/tasks/create` that reads the payload (i.e., the request body) of the |
| 88 | +HTTP POST request and logs it. The log output can be viewed with: |
| 89 | + |
| 90 | +``` |
| 91 | +gcloud app logs read |
| 92 | +``` |
| 93 | + |
| 94 | +Create a task that will be scheduled for a time in the future using the |
| 95 | +`--in-seconds` flag: |
| 96 | + |
| 97 | +``` |
| 98 | +mvn exec:java -Dexec.mainClass="com.example.task.CreateTask" \ |
| 99 | + -Dexec.args="--project-id $GOOGLE_CLOUD_PROJECT \ |
| 100 | + --queue $QUEUE_ID --location $LOCATION_ID --payload hello --in-seconds 30" |
| 101 | +``` |
0 commit comments