Stay organized with collections Save and categorize content based on your preferences.

Add a task to a Cloud Tasks queue

This quickstart allows you to try out basic operations using Cloud Tasks queues through the Cloud Tasks API.

The samples in this quickstart target App Engine handlers and so you must deploy an App Engine app. If you are not using App Engine as a target, you do not need to deploy an App Engine app, and you can disable any existing App Engine app.

For example, if you are using an HTTP Target handler exclusively, an App Engine app is not necessary. In the case of generic HTTP Targets, the Cloud Tasks service forwards the task request to the worker, located at any generic HTTP endpoint, based on how the task is configured. For more information on using HTTP Targets, see Create HTTP Target tasks.

Before you begin

To set up your Cloud environment for this quickstart, create a Google Cloud project and add an App Engine application with billing enabled. If you have a billing account when you create your Google Cloud project, then billing is automatically enabled.

Learn more about managing Google Cloud projects, App Engine applications, and billing in general.

  1. Create or select a Google Cloud project:
    Go to the App Engine page
    1. Use the buttons on the top right to select an existing project name or create a new one.
    2. Make a note of the Project ID in the dropdown box on the upper left. It will serve as a parameter for your Cloud Tasks requests.
  2. Add an App Engine application to your project:
    1. On the Welcome to App Engine page click Create application.
    2. Select a region for your application. This location will serve as the LOCATION_ID parameter for your Cloud Tasks requests, so make a note of it. Note that two locations, called europe-west and us-central in App Engine commands, are called, respectively, europe-west1 and us-central1 in Cloud Tasks commands.
    3. On the Get started page, just click Next. You'll take care of this later. If this is a new project, the execution environment is built.
    4. If the Enable billing popup appears, select your billing account. If you do not currently have a billing account, click Create billing account and follow the wizard.
    5. On the Next steps page, just click I'll do this later. You'll access the samples and download the SDK later.

  3. Enable the Cloud Tasks API.
    Go to the Cloud Tasks API page
  4. Set up authentication to the API.
    1. Follow the instructions to create a service account. Download the file with the JSON key associated with the account and store it locally. The service account key authorizes your local code to access Google Cloud programmatically.
    2. The samples use the Google Cloud Client Libraries to interact with App Engine, so you need to set an environment variable to point to the key you downloaded above, which the library uses to authenticate and authorize your requests. It is possible to create your requests without using the client libraries, but they help you manage details of low level communication with the server, including authentication.
  5. Install and setup the gcloud CLI. This gives you access to the gcloud tool.

Set up the sample

These samples provide a framework to try adding tasks to Cloud Tasks queues through Cloud Tasks API .

C#

The C# sample consists of two apps, one (CreateAppEngineTask) run locally as a command line tool to create and add tasks to the queue and one (CloudTasks) deployed on App Engine flex as a worker to "process" the task.

To download and install the sample:

  1. Make sure you have .NET Core SDK, version 2.0 or newer installed.

  2. Make sure you have initialized the gcloud tool and configured it to use the Google Cloud project you created above.

  3. Clone the sample application repository to your local machine:

    git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples

  4. Navigate to the directory that contains the sample code for CreateTask:

    cd dotnet-docs-samples/cloudtasks/api/TasksSample/

  5. Install all dependencies:

    dotnet restore

  6. Navigate to the directory (dotnet-docs-samples/cloudtasks/appengine/) that contains the sample code for CloudTasks and install all dependencies:

    dotnet restore

  7. Compile the application and prepare it for deployment:

    dotnet publish

  8. Deploy the worker service (CloudTasks) to App Engine flex:

    gcloud app deploy .\bin\Debug\netcoreapp2.1\publish\app.yaml

  9. Verify that the index page is serving:

    gcloud app browse

    Your browser opens https://{YOUR_PROJECT_ID}.appspot.com/ and displays Hello, World!.

Python

The Python sample app consists of two files, one (create_app_engine_queue_tasks.py) run locally as a command-line tool to create and add tasks to the queue and one (main.py) deployed on App Engine as a worker to "process" the task.

To download and install the sample:

  1. Clone the sample application repository to your local machine:

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

  2. Navigate to the directory that contains the sample code:

    cd python-docs-samples/appengine/flexible_python37_and_earlier/tasks/

  3. Install all dependencies:

    pip install -r requirements.txt

  4. Make sure you have initialized the gcloud tool and configured it to use the project you created above.

  5. Deploy the worker service (main.py) to App Engine:

    gcloud app deploy

  6. Verify that the index page is serving:

    gcloud app browse

    Your browser opens https://{YOUR_PROJECT_ID}.appspot.com/ and displays Hello, World!.

Java

This Java 11 example uses two applications:

  • The tasks app runs locally as a command line tool to create and add tasks to the queue.
  • The tasks-handler Spring Boot app processes Cloud Tasks requests as a worker by serving as an endpoint to receive task requests. This is the main App Engine app you deploy.

To deploy the sample app:

  1. Download and install the Java SE 11 Development Kit (JDK).
  2. Complete the Cloud Tasks setup instructions.
  3. Download and install Maven to build, deploy, and manage your app.
  4. Initialize the gcloud CLI and configure it to use the project you created above.
  5. Install the gcloud CLI app-engine-java component:

    gcloud components install app-engine-java
    

To download and install the sample:

  1. Clone the sample application repository to your local machine:

    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
    Alternatively, you can download the sample as a zip file and extract it.

  2. Navigate to the top level directory for the tasks-handler app:

    cd java-docs-samples/appengine-java11/tasks-handler/

  3. Deploy the tasks-handler app to App Engine:

    mvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID

    Replace PROJECT_ID with the ID of your Cloud project. If your pom.xml file already specifies your project ID, you don't need to include the -Dapp.deploy.projectId property in the command you run.

PHP

The PHP sample consists of two files, one (/snippets/src/create_task.php) run locally as a command line tool to create and add tasks to the queue and one (/apps/handler/index.php) deployed on App Engine as a worker to "process" the task.

To download and install the sample:

  1. Clone the sample application repository to your local machine:

    git clone https://github.com/GoogleCloudPlatform/php-docs-samples

  2. Navigate to the top level directory for the tasks sample:

    cd php-docs-samples/appengine/standard/tasks

    In the tasks directory you'll see two sub-directories:

    • apps/handler: contains code for the worker service to be deployed to App Engine
    • snippets: contains code for the local command line tool
  3. Make sure you have Composer installed. If you want to use it locally, make sure composer.phar is located in both sub-directories. See the Composer docs for information on running locally versus running globally.

  4. In the snippets directory, install all dependencies:

    composer install
    or for local only:
    php composer.phar install

  5. In the apps/handler directory, install all dependencies:

    composer install
    or for local only:
    php composer.phar install

  6. Make sure you have initialized the gcloud tool and configured it to use the Google Cloud project you created above.

  7. From the apps/handler directory, deploy the worker service to App Engine using the gcloud app deploy command:

    gcloud app deploy

  8. Check to make sure the app containing the service is running:

    gcloud app browse

    Your browser opens https://{YOUR_PROJECT_ID}.appspot.com/ and displays Hello, World!.

Go

The Golang sample consists of two files, one (tasks/create_task/create_task.go) run locally as a command line tool to create and add tasks to the queue and one (tasks/handle_task/handle_task.go) deployed on App Engine as a worker to "process" the task.

To download and install the sample:

  1. Make sure you have installed the gcloud App Engine Go component:

    gcloud components install app-engine-go

  2. Make sure you have initialized the gcloud tool and configured it to use the project you created above.

  3. Copy the sample application repository to your local machine:

    go get github.com/GoogleCloudPlatform/golang-samples/appengine/go11x/tasks/...

  4. Navigate to the tasks/handle_task directory for the worker sample code.

    cd golang-samples/appengine/go11x/tasks/handle_task

  5. Notice the app.yaml file, which contains necessary configuration information.

  6. Deploy the worker service (handle_task.go) to App Engine:

    gcloud app deploy

  7. Check to make sure the app containing the service is running:

    gcloud app browse

    Your browser opens https://{YOUR_PROJECT_ID}.appspot.com/ and displays Hello, World!.

Node.js

The Node.js sample consists of two files, one (createTask.js) run locally as a command-line tool to create and add tasks to the queue and one (server.js) deployed on App Engine as a worker to "process" the task.

To download and install the sample:

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

    You can use npm:

    npm install
    Or you can use yarn:
    yarn install

  4. Make sure you have initialized the gcloud tool and configured it to use the project you created above.

  5. Deploy the worker service (server.js) to App Engine standard environment:

    gcloud app deploy app.yaml

  6. Check to make sure the app containing the service is running:

    gcloud app browse

    Your browser opens https://{YOUR_PROJECT_ID}.appspot.com/ and displays Hello, World!.

Ruby

The Ruby sample consists of two files, one (create_Task.rb) run locally as a command line tool to create and add tasks to the queue and one (app.rb) deployed on App Engine as a worker to "process" the task.

To download and install the sample:

  1. Clone the sample application repository to your local machine:

    git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git

  2. Navigate to the directory that contains the sample code:

    cd ruby-docs-samples/appengine/cloud-tasks

  3. Install dependencies:

    bundle install

  4. Make sure you have initialized the gcloud tool and configured it to use the project you created above.

  5. Deploy the worker service (app.rb) to App Engine flexible environment:

    gcloud app deploy app.yaml

  6. Check to make sure the app containing the service is running:

    gcloud app browse

    Your browser opens https://{YOUR_PROJECT_ID}.appspot.com/ and displays Hello, World!.

For more information on creating App Engine task handlers, see Creating App Engine Task Handlers.

Create a Cloud Tasks queue

Use the gcloud CLI gcloud queue management function to create your queue in the environment you prepared above.

  1. At the command line, enter the following:

    gcloud tasks queues create my-queue

  2. Wait a bit for the queue to initialize and then use describe to verify that it was created successfully:

    gcloud tasks queues describe my-queue

  3. Check that the output is similar to this:

    name: projects/[PROJECT_ID]/locations/[LOCATION_ID]/queues/my-appengine-queue # Note these ids
    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

C#

Create a task locally, add it to the queue you set up, and deliver that task to an asynchronous worker:

  1. Set the following environment variables on your machine, either manually, in the code in your sample app, or through an alias. The sample app uses these variables to create the request that adds tasks to your queue:

    export GOOGLE_PROJECT_ID=my-project-id # The project ID you set up above
    export GCP_QUEUE=my-queue # the queue you created above
    export LOCATION_ID=my-location-id # The region in which your queue is running

    You can find the location ID for the App Engine app by using the following gcloud command:

    gcloud tasks locations list

  2. Navigate to the TasksSample folder:

    cd ../api/TasksSample

  3. Using test, create a task and add a payload, the string 'hello'. The CloudTasksapp that you deployed previously has an endpoint,log_payload that processes the payload by logging it.

    dotnet test

  4. Check the log to make sure the task hit the endpoint:

    gcloud app logs read

Python

Create a task locally, add it to the queue you set up, and deliver that task to an asynchronous worker:

  1. Set the following environment variables on your machine either manually, in the code in your sample app, or through an alias. The sample app uses these variables to create the request that adds tasks to your queue:

    export PROJECT_ID=PROJECT_ID # The project ID you set up above
    export LOCATION_ID=LOCATION_ID # The region in which your queue is running
    export QUEUE_ID=my-queue # The queue you created above
    Note: You can find the location ID by using the following gcloud command:
    gcloud tasks locations list

  2. Use the local create_app_engine_queue_task.py to create a task and add a payload of hello. The payload can be any data from the request that the worker needs to complete processing the task:

    python create_app_engine_queue_task.py --project=$PROJECT_ID \
    --location=$LOCATION_ID --queue=$QUEUE_ID --payload=hello

  3. Verify that the payload was received:

    gcloud app logs read

Java

Create a task locally, add it to the queue you set up, and deliver the task to an asynchronous worker:

  1. Move into the appengine-java11/tasks directory and compile the app:

    cd ../tasks
    mvn package

  2. Set the following environment variables on your machine. The sample app uses these to create the request that adds tasks to your queue:

    export GOOGLE_CLOUD_PROJECT=PROJECT_ID // The project ID you set up above
    export LOCATION_ID=LOCATION_ID // The region in which your queue is running
    export QUEUE_ID=MY_QUEUE // The queue you created above
    Note: You can find the location ID by using the following gcloud command:
    gcloud tasks locations list

  3. Create a task, targeted at the /tasks/create endpoint on the task handler controller you deployed, with a specified payload. The payload can be any data from the request that the worker needs to complete processing the task; in this example, it's hard-coded into the sample and doesn't need to be specified:

     mvn exec:java -Dexec.mainClass="com.example.task.CreateTask"
     
    Once the task gets passed to the worker and the worker processes the task, the worker will return a 2xx success status code to the Cloud Tasks service and delete the task from the queue automatically.

  4. Verify that the task was received by displaying the logs of the worker service:

    gcloud app logs read

PHP

Create a task locally, add it to the queue you set up, and deliver that task to an asynchronous worker:

  1. Navigate to the snippets directory.

  2. Set the following environment variables on your machine either manually, in the code in your sample app, or through an alias. The sample app uses these to create the request that adds your task to your queue:

    export PROJECT_ID=PROJECT_ID # The project ID from above
    export LOCATION_ID=LOCATION_ID # The region in which your queue is running
    export QUEUE_ID=my-queue # The queue you created above
    Note: You can find the location ID by using the following gcloud command:
    gcloud tasks locations list

  3. Use the local snippet create_task.php to create a task and add a payload of hello. The payload can be any data from the request that the worker needs to complete processing the task:

    php src/create_task.php $PROJECT_ID $LOCATION_ID $QUEUE_ID hello
    

  4. Check the logs to verify that the payload was received:

    gcloud app logs read

Go

Create a task locally, add it to the queue you set up, and deliver that task to an asynchronous worker:

  1. If you are still in the tasks/handle_task directory, navigate to the tasks/create_task directory.

  2. Set the following environment variables on your machine either manually, in the code in your sample app, or through an alias. The client uses this information to create the request:

     export PROJECT_ID=PROJECT_ID # The project ID from above
     export LOCATION_ID=LOCATION_ID # The region in which your queue is running
     export QUEUE_ID=my-queue # The queue you created above
    Note: You can find the location ID by using the following gcloud command:
    gcloud tasks locations list

  3. 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 needs to complete processing the task:

    go run . $PROJECT_ID $LOCATION_ID $QUEUE_ID hello 

  4. Verify that the payload was received by displaying the logs of the worker service:

    gcloud app logs read

Node.js

Create a task locally, add it to the queue you set up, and deliver that task to an asynchronous worker:

  1. Set the following environment variables on your machine either manually, in the code in your sample app, or through an alias. The client uses this information to create the request:

    export PROJECT_ID=PROJECT_ID # The project ID from above
    export LOCATION_ID=LOCATION_ID # The region in which your queue is running
    export QUEUE_ID=my-queue # The queue you created above
    Note: You can find the location ID by using the following gcloud command:
    gcloud tasks locations list

  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 needs to complete processing the task:

    node createTask.js $PROJECT_ID $QUEUE_ID $LOCATION_ID hello 

  3. Verify that the payload was received by displaying the logs of the worker service:

    gcloud app logs read

Ruby

Create a task locally, add it to the queue you set up, and deliver that task to an asynchronous worker:

  1. Set the following environment variables on your machine either manually, in the code in your sample app, or through an alias. The client uses this information to create the request:

    export GOOGLE_CLOUD_PROJECT=my-project-id # The project ID from above
    export LOCATION_ID=my-location-id # The region in which your queue is running
    export QUEUE_ID=my-queue # The queue you created above
    Note: You can find the location ID by using the following gcloud command:
    gcloud tasks locations list

  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 needs to complete processing the task:

    ruby create_task.rb $GOOGLE_CLOUD_PROJECT_ID $LOCATION_ID $QUEUE_ID hello

  3. Verify that the payload was received by displaying the logs of the worker service:

    gcloud app logs read

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next

Now that you've completed adding a task to a Cloud Tasks queue, continue exploring Cloud Tasks by looking at the following pages: