Quickstart for Cloud Tasks queues

The following instructions allow you to try out basic operations using Cloud Tasks queues via Cloud Tasks API :

  1. Before you begin (set up your environment)
  2. Set up the sample code
  3. Create a queue
  4. Create a task and add it to the queue
  5. Clean up your resources

Before you begin

To set up your Cloud environment, create a GCP project and add an App Engine application with billing enabled. You must have an App Engine application in your project to run your queue. For more information on GCP projects, App Engine applications, and billing in general, see here.

  1. Create or select a GCP 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 backend 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. A service account allows you to authenticate with 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 your requests. It is possible to create your requests without using the client libraries, but they can help you manage details of low level communication with the server, including authentication.
  5. Install and setup the Cloud SDK. 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 via 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. The queue itself runs on App Engine.

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 GCP 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. The queue itself runs on App Engine.

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/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

The Java 8 sample app consists of two files, one (CreateTask.java) run locally as a command line tool to create and add tasks to the queue and one (TaskServlet.java) deployed on App Engine as a worker to "process" the task. The queue itself runs on App Engine. To run the sample using Java 11, see Java 11 Executing code asynchronously.

Note: To use the Java sample app, you must also have these additional components:

  • Maven
  • The app-engine-java component for the Cloud SDK
  • The App Engine Maven plugin, which has already been specified in the provided pom.xml file for the sample

For more information on these components, see Using Apache Maven and the App Engine Plugin and Maven Plugin Goals and Parameters.

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

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

    cd java-docs-samples/appengine-java8/tasks/quickstart/

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

  4. Build and run the local command-line tool (CreateTask.java):

    mvn appengine:run

  5. Deploy the worker service (TaskServlet.java) to App Engine:

    mvn appengine:deploy

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. The queue itself runs on App Engine.

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/php72/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 GCP 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. The queue itself runs on App Engine.

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. The queue itself runs on App Engine.

To download and install the sample:

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

    git clone https://github.com/googleapis/nodejs-tasks.git

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

    cd nodejs-tasks/samples

  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. The queue itself runs on App Engine.

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 Cloud SDK 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 via 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 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 via 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. Set the following environment variables on your machine either manually, in the code in your sample app, or via an alias. The sample app uses these to create the request that adds task 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

  2. Create a task, targeted at the /task/create endpoint on the servlet you deployed, with a payload of hello. The payload can be any data from the request that the worker needs to complete processing the task:

    mvn exec:java -Dexec.mainClass="com.example.task.CreateTask"
      -Dexec.args="--project-id $GOOGLE_CLOUD_PROJECT
      --queue $QUEUE_ID --location $LOCATION_ID --payload hello"
    

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

    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 via 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=$PROJECT_ID --location=$LOCATION_ID --queue=$QUEUE_ID --payload=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 via 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 via 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 via 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 GCP account for the resources used in this quickstart:

  1. In the GCP Console, go to the Manage resources page.

    Go to the Manage resources page

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

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Tasks Documentation