|
| 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 | +`tasks.php` is a simple command-line program to create tasks to be pushed to |
| 11 | +the App Engine app. |
| 12 | + |
| 13 | +`src/create_task.php` is a simple function to create tasks to be pushed to |
| 14 | +the App Engine app. |
| 15 | + |
| 16 | +`index.php` is the main App Engine app. This app serves as an endpoint to receive |
| 17 | +App Engine task attempts. |
| 18 | + |
| 19 | +`app.yaml` configures the App Engine app. |
| 20 | + |
| 21 | +## Setup: |
| 22 | + |
| 23 | +1. **Enable APIs** - [Enable the Cloud Tasks API](https://console.cloud.google.com/flows/enableapi?apiid=cloudtasks) |
| 24 | + and create a new project or select an existing project. |
| 25 | +2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials" |
| 26 | + and select "Service Account Key". Create a new service account, use the JSON key type, and |
| 27 | + select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` |
| 28 | + to the path of the JSON key that was downloaded. |
| 29 | +3. **Clone the repo** and cd into this directory |
| 30 | + |
| 31 | + ```sh |
| 32 | + $ git clone https://github.com/GoogleCloudPlatform/php-docs-samples |
| 33 | + $ cd php-docs-samples/appengine/flexible/tasks |
| 34 | + ``` |
| 35 | +4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md). |
| 36 | + Run `php composer.phar install` (if composer is installed locally) or `composer install` |
| 37 | + (if composer is installed globally). |
| 38 | + |
| 39 | +## Creating a queue |
| 40 | + |
| 41 | +To create a queue using the Cloud SDK, use the following gcloud command: |
| 42 | + |
| 43 | + gcloud alpha tasks queues create-app-engine-queue my-appengine-queue |
| 44 | + |
| 45 | +Note: A newly created queue will route to the default App Engine service and |
| 46 | +version unless configured to do otherwise. |
| 47 | + |
| 48 | +## Deploying the App Engine app |
| 49 | + |
| 50 | +Deploy the App Engine app with gcloud: |
| 51 | + |
| 52 | + gcloud app deploy |
| 53 | + |
| 54 | +Verify the index page is serving: |
| 55 | + |
| 56 | + gcloud app browse |
| 57 | + |
| 58 | +The App Engine app serves as a target for the push requests. It has an |
| 59 | +endpoint `/example_task_handler` that reads the payload (i.e., the request |
| 60 | +body) of the HTTP POST request and logs it to the `my-log` log. The log output can be viewed with: |
| 61 | + |
| 62 | + gcloud logging read my-log |
| 63 | + |
| 64 | +## Running the Samples |
| 65 | + |
| 66 | +Set environment variables: |
| 67 | + |
| 68 | +First, your project ID: |
| 69 | + |
| 70 | + export PROJECT_ID=my-project-id |
| 71 | + |
| 72 | +Then the queue ID, as specified at queue creation time. Queue IDs already |
| 73 | +created can be listed with `gcloud alpha tasks queues list`. |
| 74 | + |
| 75 | + export QUEUE_ID=my-appengine-queue |
| 76 | + |
| 77 | +And finally the location ID, which can be discovered with |
| 78 | +`gcloud alpha tasks queues describe $QUEUE_ID`, with the location embedded in |
| 79 | +the "name" value (for instance, if the name is |
| 80 | +"projects/my-project/locations/us-central1/queues/my-appengine-queue", then the |
| 81 | +location is "us-central1"). |
| 82 | + |
| 83 | + export LOCATION_ID=us-central1 |
| 84 | + |
| 85 | +Create a task, targeted at the `example_task_handler` endpoint, with a payload specified: |
| 86 | + |
| 87 | + php tasks.php create-task $PROJECT_ID $QUEUE_ID $LOCATION_ID --payload=hello |
| 88 | + |
| 89 | +Now view that the payload was received and verify the payload: |
| 90 | + |
| 91 | + gcloud logging read my-log |
| 92 | + |
| 93 | +Create a task that will be scheduled for a time in the future using the |
| 94 | +`--seconds` flag: |
| 95 | + |
| 96 | + php tasks.php create-task $PROJECT_ID $QUEUE_ID $LOCATION_ID --payload=hello --seconds=2 |
0 commit comments