The following instructions allow you to try out basic operations using Cloud Tasks queues via Cloud Tasks API :
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 enabled 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.- Create or select a GCP 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 backend 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. A service account allows you to authenticate with 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 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.
- 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:
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 GCP 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. The queue itself runs on
App Engine.
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/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
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:
Clone the sample application repository to your local machine:
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Navigate to the top level directory for the Tasks sample:
cd java-docs-samples/appengine-java8/tasks/quickstart/
Make sure you have initialized the
gcloud
tool and configured it to use the project you created above.Build and run the local command-line tool (
CreateTask.java
):mvn appengine:run
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:
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/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 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 GCP 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. The queue itself runs on App Engine.
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. The queue itself runs on App Engine.
To download and install the sample:
Clone the sample application repository to your local machine:
git clone https://github.com/googleapis/nodejs-tasks.git
Navigate to the directory that contains the sample code:
cd nodejs-tasks/samples
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. The queue itself runs on App Engine.
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 Cloud SDK 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 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
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 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 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:
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 followinggcloud
command:gcloud tasks locations list
Create a task, targeted at the
/task/create
endpoint on the servlet you deployed, with a payload ofhello
. 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"
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:
Navigate to the
snippets
directory.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 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 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 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 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 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 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 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 in this quickstart, follow these steps.
- In the 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
- Create and configure Cloud Tasks queues with the Creating Cloud Tasks Queues guide.
- Find out more about Cloud Tasks overall in the Cloud Tasks Overview
- Learn more about queue management using RPC in the RPC API reference.
- Learn more about queue management using REST in the REST API reference.