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
- 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.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager and Cloud Tasks API:
gcloud services enable cloudresourcemanager.googleapis.com tasks.googleapis.com
-
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.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager and Cloud Tasks API:
gcloud services enable cloudresourcemanager.googleapis.com tasks.googleapis.com
-
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.
- 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:- Artifact Registry Administrator (
roles/artifactregistry.admin
) - Artifact Registry Create-on-Push Writer (
roles/artifactregistry.createOnPushWriter
) - Compute Admin (
roles/compute.admin
) - Logs Writer (
roles/logging.logWriter
) - Storage Object Viewer (
roles/storage.objectViewer
)
- Artifact Registry Administrator (
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.
In the Google Cloud console, go to the App Engine page.
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.
Select a region for your application and make a note of it.
Note that
europe-west
andus-central
are called, respectively,europe-west1
andus-central1
in Cloud Tasks commands.Don't select a service account; the default App Engine service account is used.
Click Next.
The application is configured and created. This can take a couple of minutes.
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.
In your terminal, 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 using a Node.js package manager.
You can use NPM:
npm install
Or, you can use Yarn:
yarn install
Deploy the worker service (
server.js
) to the App Engine standard environment.gcloud app deploy app.yaml
Make sure that the app containing the service is running.
gcloud app browse
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.
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 queueREGION
: the region you deployed your app in
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.
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
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
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:
Delete the Cloud Tasks queue:
gcloud tasks queues delete QUEUE_NAME \ --location=REGION