# Add an App Engine task to a Cloud Tasks queue

Cloud Tasks lets you dispatch asynchronous tasks to App Engine
services to process background workloads. In this quickstart, you use the
Cloud Tasks API to create and add an
[App Engine task](https://docs.cloud.google.com/tasks/docs/dual-overview.md.txt#appe) to a
Cloud Tasks queue.

## Before you begin

1. The [App Engine default service account](https://docs.cloud.google.com/appengine/docs/standard/configure-service-accounts.md.txt#default_service_account) is automatically created when you use App Engine. You can use this service account when trying out this quickstart. However, depending on your organization policy configuration, the default service account might not automatically be granted the `Editor` role on your project. If that is the case, you must [grant the service account](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt#grant-single-role) the following roles:
   1. Artifact Registry Administrator (`roles/artifactregistry.admin`)
   2. Artifact Registry Create-on-Push Writer (`roles/artifactregistry.createOnPushWriter`)
   3. Compute Admin (`roles/compute.admin`)
   4. Logs Writer (`roles/logging.logWriter`)
   5. Storage Object Viewer (`roles/storage.objectViewer`)

<br />

## Add an App Engine application

When you target an App Engine task, and before you can deploy an app to
the App Engine standard environment, you must add an
[App Engine application](https://docs.cloud.google.com/appengine/docs/standard/managing-projects-apps-billing.md.txt)
to your project.

1. In the Google Cloud console, go to the **App Engine** page.


   [Go to App Engine](https://console.cloud.google.com/appengine/start)

   <br />

2. In the **Welcome to App Engine** dialog, do one of the following:

   - If you have already created an App Engine application and there is a
     **Your App Engine application has been created** message displayed, you can
     then skip the remaining steps in this section and proceed with the steps in
     the [Install and deploy the sample](https://docs.cloud.google.com/tasks/docs/add-task-queue-app-eng.md.txt#deploy-sample) section.

     or

     <br />

   - If you haven't created an App Engine application yet, then click
     **Create application** and continue with the remaining steps in this section.

3. Select a [region](https://docs.cloud.google.com/appengine/docs/locations) for your
   application and make a note of it.

   Note that `europe-west` and `us-central` are called, respectively,
   `europe-west1` and `us-central1` in Cloud Tasks commands.
4. Don't select a service account; the default App Engine service
   account is used.

5. Click **Next**.

   The application is configured and created. This can take a couple of minutes.
6. Don't download the Cloud SDK; instead, click **I'll do this later**.

   You should see a **Your App Engine application has been created** message.

## Install and deploy the sample

The Node.js sample used in this quickstart consists of two files:
`createTask.js` is run locally as a command-line tool to create and add tasks to
the Tasks queue; `server.js` is deployed on App Engine as
a worker service to process the task.

1. In your terminal, clone the sample application repository to your local
   machine.

   ```bash
   git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
   ```
2. Navigate to the directory that contains the sample code.

   ```bash
   cd nodejs-docs-samples/cloud-tasks/snippets
   ```
3. Install all dependencies using a Node.js package manager.

   You can use NPM:

   ```bash
   npm install
   ```

   Or, you can use Yarn:

   ```bash
   yarn install
   ```
4. Deploy the worker service (`server.js`) to the App Engine standard
   environment.

   ```bash
   gcloud app deploy app.yaml
   ```
5. Make sure that the app containing the service is running.

   ```bash
   gcloud app browse
   ```
6. In your browser, go to the provided link. For example:

       https://PROJECT_ID.uc.r.appspot.com/

   You should see `Hello, World!` displayed.

## Create a Cloud Tasks queue

Use the [`gcloud tasks queues create`](https://docs.cloud.google.com/sdk/gcloud/reference/tasks/queues/create)
command to create your queue in the environment you've prepared.

> [!CAUTION]
> **Caution:** Using Cloud Tasks `gcloud` queue management methods carries some risk if used together with methods based on uploading `queue.yaml/queue.xml`. For more information, see [Use Queue Management or queue.yaml](https://docs.cloud.google.com/tasks/docs/queue-yaml.md.txt).

1. In your terminal, create a queue that logs all operations.

   ```bash
   gcloud tasks queues create QUEUE_NAME \
       --log-sampling-ratio=1.0 \
       --location=REGION
   ```

   Replace the following:
   - `QUEUE_NAME`: a name for your Cloud Tasks queue
   - `REGION`: the region you deployed your app in
2. Wait for the queue to initialize and then verify that it was created
   successfully.

   ```bash
   gcloud tasks queues describe QUEUE_NAME \
       --location=REGION
   ```

   The output should be similar to the following:

        name: projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_NAME
        rateLimits:
          maxBurstSize: 100
          maxConcurrentDispatches: 1000
          maxDispatchesPerSecond: 500.0
        retryConfig:
          maxAttempts: 100
          maxBackoff: 3600s
          maxDoublings: 16
          minBackoff: 0.100s
        state: RUNNING

## Add a task to the Cloud Tasks queue

Create a task, add it to the queue you created, and deliver that task to the
worker service.

1. Set the following environment variables. The client uses this information to
   create the request.

   ```bash
   export PROJECT_ID=PROJECT_ID
   export LOCATION_ID=REGION
   export QUEUE_ID=QUEUE_NAME
   ```
2. Create a task with a payload of `hello` and add that task to your queue. The
   payload can be any data from the request that the worker service needs to
   process the task.

   ```javascript
   node createTask.js $PROJECT_ID $QUEUE_ID $LOCATION_ID hello
   ```
3. Verify that the task was executed by displaying the logs of the worker
   service.

   ```bash
   gcloud app logs read
   ```

   The logs should look similar to the following:

       2024-06-20 15:00:00 default[20240620t143852]  "POST /log_payload HTTP/1.1" 200
       2024-06-20 15:00:00 default[20240620t143852]  App listening on port 8081
       2024-06-20 15:00:00 default[20240620t143852]  Press Ctrl+C to quit.
       2024-06-20 15:00:00 default[20240620t143852]  Received task with payload: hello

## Clean up


To avoid incurring charges to your Google Cloud account for
the resources used on this page, delete the Google Cloud project with the
resources.

> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

Delete a Google Cloud project:

```
gcloud projects delete PROJECT_ID
```

Alternatively, you can delete the resources you created:

1. Delete the Cloud Tasks queue:

   ```bash
   gcloud tasks queues delete QUEUE_NAME \
       --location=REGION
   ```
2. [Disable the App Engine application](https://docs.cloud.google.com/appengine/docs/standard/managing-projects-apps-billing.md.txt#disabling_an_application).

## What's next

- [Manage queues and tasks](https://docs.cloud.google.com/tasks/docs/deleting-appengine-queues-and-tasks)
- [Create Cloud Tasks queues](https://docs.cloud.google.com/tasks/docs/creating-queues.md.txt)
- [RPC API reference](https://docs.cloud.google.com/tasks/docs/reference/rpc.md.txt)
- [REST API reference](https://docs.cloud.google.com/tasks/docs/reference/rest.md.txt)