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.
- Create or select a Google Cloud project:
Go to the App Engine page - Use the buttons on the top right to select an existing project name or create a new one.
- 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.
- Add an App Engine application to your project:
- On the Welcome to App Engine page click Create application.
- 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
andus-central
in App Engine commands, are called, respectively,europe-west1
andus-central1
in Cloud Tasks commands. - 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.
- 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.
- On the Next steps page, just click I'll do this later. You'll access the samples and download the SDK later.
- Enable the Cloud Tasks API.
Go to the Cloud Tasks API page - Set up authentication to the API.
- 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.
- 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.
- 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:
Make sure you have .NET Core SDK, version 2.0 or newer installed.
Make sure you have initialized the
gcloud
tool and configured it to use the Google Cloud project you created above.Clone the sample application repository to your local machine:
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples
Navigate to the directory that contains the sample code for
CreateTask
:cd dotnet-docs-samples/cloudtasks/api/TasksSample/
Install all dependencies:
dotnet restore
Navigate to the directory (
dotnet-docs-samples/cloudtasks/appengine/
) that contains the sample code forCloudTasks
and install all dependencies:dotnet restore
Compile the application and prepare it for deployment:
dotnet publish
Deploy the worker service (
CloudTasks
) to App Engine flex:gcloud app deploy .\bin\Debug\netcoreapp2.1\publish\app.yaml
Verify that the index page is serving:
gcloud app browse
Your browser opens
https://{YOUR_PROJECT_ID}.appspot.com/
and displaysHello, 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:
Clone the sample application repository to your local machine:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Navigate to the directory that contains the sample code:
cd python-docs-samples/appengine/flexible_python37_and_earlier/tasks/
Install all dependencies:
pip install -r requirements.txt
Make sure you have initialized the
gcloud
tool and configured it to use the project you created above.Deploy the worker service (
main.py
) to App Engine:gcloud app deploy
Verify that the index page is serving:
gcloud app browse
Your browser opens
https://{YOUR_PROJECT_ID}.appspot.com/
and displaysHello, 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:
- Download and install the Java SE 11 Development Kit (JDK).
- Complete the Cloud Tasks setup instructions.
- Download and install Maven to build, deploy, and manage your app.
- Initialize the gcloud CLI and configure it to use the project you created above.
Install the gcloud CLI
app-engine-java
component:gcloud components install app-engine-java
To download and install the sample:
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.Navigate to the top level directory for the
tasks-handler
app:cd java-docs-samples/appengine-java11/tasks-handler/
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:
Clone the sample application repository to your local machine:
git clone https://github.com/GoogleCloudPlatform/php-docs-samples
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 Enginesnippets
: contains code for the local command line tool
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.In the
snippets
directory, install all dependencies:composer install
or for local only:php composer.phar install
In the
apps/handler
directory, install all dependencies:composer install
or for local only:php composer.phar install
Make sure you have initialized the
gcloud
tool and configured it to use the Google Cloud project you created above.From the
apps/handler
directory, deploy the worker service to App Engine using thegcloud app deploy
command:gcloud app deploy
Check to make sure the app containing the service is running:
gcloud app browse
Your browser opens
https://{YOUR_PROJECT_ID}.appspot.com/
and displaysHello, 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:
Make sure you have installed the
gcloud
App Engine Go component:gcloud components install app-engine-go
Make sure you have initialized the
gcloud
tool and configured it to use the project you created above.Copy the sample application repository to your local machine:
go get github.com/GoogleCloudPlatform/golang-samples/appengine/go11x/tasks/...
Navigate to the
tasks/handle_task
directory for the worker sample code.cd golang-samples/appengine/go11x/tasks/handle_task
Notice the
app.yaml
file, which contains necessary configuration information.Deploy the worker service (
handle_task.go
) to App Engine:gcloud app deploy
Check to make sure the app containing the service is running:
gcloud app browse
Your browser opens
https://{YOUR_PROJECT_ID}.appspot.com/
and displaysHello, 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:
Clone the sample application repository to your local machine:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Navigate to the directory that contains the sample code:
cd nodejs-docs-samples/cloud-tasks/snippets
Install all dependencies.
You can use npm:
npm install
Or you can use yarn:yarn install
Make sure you have initialized the
gcloud
tool and configured it to use the project you created above.Deploy the worker service (
server.js
) to App Engine standard environment:gcloud app deploy app.yaml
Check to make sure the app containing the service is running:
gcloud app browse
Your browser opens
https://{YOUR_PROJECT_ID}.appspot.com/
and displaysHello, 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:
Clone the sample application repository to your local machine:
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
Navigate to the directory that contains the sample code:
cd ruby-docs-samples/appengine/cloud-tasks
Install dependencies:
bundle install
Make sure you have initialized the
gcloud
tool and configured it to use the project you created above.Deploy the worker service (
app.rb
) to App Engine flexible environment:gcloud app deploy app.yaml
Check to make sure the app containing the service is running:
gcloud app browse
Your browser opens
https://{YOUR_PROJECT_ID}.appspot.com/
and displaysHello, 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.
At the command line, enter the following:
gcloud tasks queues create my-queue
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
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:
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
Navigate to the
TasksSample
folder:cd ../api/TasksSample
Using
test
, create a task and add a payload, the string 'hello'. TheCloudTasks
app that you deployed previously has an endpoint,log_payload
that processes the payload by logging it.dotnet test
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:
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 followinggcloud
command:gcloud tasks locations list
Use the local
create_app_engine_queue_task.py
to create a task and add a payload ofhello
. 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
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:
Move into the
appengine-java11/tasks
directory and compile the app:cd ../tasks mvn package
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 followinggcloud
command:gcloud tasks locations list
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.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:
Navigate to the
snippets
directory.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 followinggcloud
command:gcloud tasks locations list
Use the local snippet
create_task.php
to create a task and add a payload ofhello
. 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
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:
If you are still in the
tasks/handle_task
directory, navigate to thetasks/create_task
directory.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 followinggcloud
command:gcloud tasks locations list
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
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:
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 followinggcloud
command:gcloud tasks locations list
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
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:
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 followinggcloud
command:gcloud tasks locations list
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
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.
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- 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:
- Manage existing tasks and queues, including deleting them with Managing Queues and Tasks.
- Create and configure Cloud Tasks queues with the Creating Cloud Tasks Queues guide.
- Learn more about queue management using gRPC in the gRPC API reference.
- Learn more about queue management using REST in the REST API reference.
- Find out more about Cloud Tasks queues with Cloud Tasks Overview.