Add an App Engine task to a Cloud Tasks queue

This quickstart shows you how to add an App Engine task to a Cloud Tasks queue using the Cloud Tasks API.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.
  3. To initialize the gcloud CLI, run the following command:

    gcloud init
  4. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  5. Make sure that billing is enabled for your Google Cloud project.

  6. Enable the Cloud Resource Manager and Cloud Tasks API:

    gcloud services enable cloudresourcemanager.googleapis.com tasks.googleapis.com
  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  8. Install the Google Cloud CLI.
  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  11. Make sure that billing is enabled for your Google Cloud project.

  12. Enable the Cloud Resource Manager and Cloud Tasks API:

    gcloud services enable cloudresourcemanager.googleapis.com tasks.googleapis.com
  13. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  14. The App Engine 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 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)

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 to your project.

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

    Go to App Engine

  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 section.

      or

    • 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 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.

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

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

    You can use NPM:

    npm install
    

    Or, you can use Yarn:

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

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

    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 command to create your queue in the environment you've prepared.

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

    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.

    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.

    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.

    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.

    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.

Delete a Google Cloud project:

gcloud projects delete PROJECT_ID

Alternatively, you can delete the resources you created:

  1. Delete the Cloud Tasks queue:

    gcloud tasks queues delete QUEUE_NAME \
        --location=REGION
    
  2. Disable the App Engine application.

What's next