Creating Cloud Tasks Queues

This page describes how to create and configure Cloud Tasks queues using the gcloud command of the Google Cloud SDK.

Before you begin

To access the Cloud Tasks service using Cloud Tasks API , you must have a project that contains an App Engine app located in a specific region. 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. The app serves as the location for whatever queues the developer creates. The underlying Cloud Tasks service itself runs in that same location.

Creating a Cloud Tasks queue

Use the Google Cloud SDK to create your queue:

gcloud tasks queues create [QUEUE_ID]

where:

  • QUEUE_ID is the identifier you assign to your queue, for example, my-queue.

It can take a few minutes for a newly created queue to be available. Then use describe to verify that your queue was created successfully:

gcloud tasks queues describe [QUEUE_ID]

The output should be something like:

appEngineHttpQueue: {}
name: projects/[PROJECT_ID]/locations/[LOCATION_ID]/queues/[QUEUE_ID]
rateLimits:
  maxBurstSize: 100
  maxConcurrentDispatches: 1000
  maxDispatchesPerSecond: 500.0
retryConfig:
  maxAttempts: 100
  maxBackoff: 3600s
  maxDoublings: 16
  minBackoff: 0.100s
state: RUNNING

If you do not see the above output, you can try the following:

  • Wait at least one minute for the queue to initialize.
  • Make sure a queue with the same ID does not already exist. You can view all queues in your project by calling:

    gcloud tasks queues list
    
  • Check if you've reached your queue limit:

    Go to the Cloud Tasks API page

    and select Quotas.

    To increase your resource limits, select Edit Quotas from the Quotas page of the GCP Console

  • Check each part of the command and re-run it.

Configuring your Cloud Tasks queue

You can configure your Cloud Tasks queue when you create the queue or anytime afterwards, and the configuration will apply to all tasks in that queue.

There are three basic aspects to configuring your queues:

Configure routing (App Engine queues only)

The queue needs to know the name and version of the service that contains the appropriate worker. This is known as the target. There are three ways to set the target:

  • Do not explicitly set the target. In this case, the default service is used.
  • Explicitly declare the target in the task itself. This is the preferred method if you wish to use a target other than the default.
  • Explicitly route all tasks in a queue to a non-default target. This method overrides any routing that might be set in the task itself.

To set up non-default queue-level routing that will override any task-level routing:

gcloud tasks queues update [QUEUE_ID] \
    --routing-override=service:[SERVICE],version:[VERSION]

where:

  • SERVICE is the App Engine worker service responsible for task handling.
  • VERSION is the app version.

For example, if you set up a worker service called worker to handle all tasks in a queue called barbequeue, you can route to that service and the default version by calling:

gcloud tasks queues update barbequeue \
    --routing-override=service:worker

Describe the queue:

gcloud tasks queues describe barbequeue

The output should be something like:

appEngineHttpQueue:
  appEngineRoutingOverride:
    host: worker.[PROJECT_ID].appspot.com
    service: worker
name: projects/[PROJECT_ID]/locations/[LOCATION_ID]/queues/barbequeue
rateLimits:
  maxBurstSize: 100
  maxConcurrentDispatches: 1000
  maxDispatchesPerSecond: 500.0
retryConfig:
  maxAttempts: 100
  maxBackoff: 3600s
  maxDoublings: 16
  minBackoff: 0.100s
state: RUNNING

Establish rate limits

You can set the maximum rate and number of concurrent tasks that can be dispatched by a queue.

gcloud tasks queues update-app-engine-queue [QUEUE_ID] \
    --max-dispatches-per-second=[DISPATCH_RATE] \
    --max-concurrent-dispatches=[MAX_RUNNING]

where:

  • DISPATCH_RATE is the maximum rate at which the queue dispatches its tasks.
  • MAX_RUNNING is the maximum number of tasks in the queue that can run at once.

For example, if you created a queue called barbequeue without setting any parameters, you can update the maximum number of concurrent tasks by calling:

gcloud tasks queues update-app-engine-queue barbequeue \
        --max-concurrent-dispatches=20

Describe the queue:

gcloud tasks queues describe barbequeue

The output should be:

appEngineHttpTarget: {}
name: projects/[PROJECT_ID]/locations/[LOCATION_ID]/queues/barbequeue
rateLimits:
  maxBurstSize: 100
  maxConcurrentDispatches: 20
  maxDispatchesPerSecond: 500.0
retryConfig:
  maxAttempts: 100
  maxBackoff: 3600s
  maxDoublings: 16
  minBackoff: 0.100s
state: RUNNING

Set retry parameters

If a task does not complete successfully, then Cloud Tasks will retry the task with exponential backoff according to the parameters you have set. You can specify the maximum number of times to retry failed tasks in the queue, set a time limit for retry attempts, and control the interval between attempts.

gcloud tasks queues update-app-engine-queue [QUEUE_ID] \
    --max-attempts=[MAX_ATTEMPTS] \
    --min-backoff=[MIN_INTERVAL] \
    --max-backoff=[MAX_INTERVAL] \
    --max-doublings=[MAX_DOUBLINGS] \
    --max-retry-duration=[MAX_RETRY_DURATION]

where:

  • MAX_ATTEMPTS is the maximum number of attempts for a task, including the first attempt. You can allow unlimited retries by setting this flag to unlimited.
  • MIN_INTERVAL is the minimum amount of time to wait between retry attempts. The value must be a string that ends in "s," such as 5s.
  • MAX_INTERVAL is the maximum amount of time to wait between retry attempts. The value must be a string that ends in "s," such as 5s.
  • MAX_DOUBLINGS is the maximum number of times that the interval between failed task retries will be doubled before the increase becomes constant.
  • MAX_RETRY_DURATION is the maximum amount of time for retrying a failed task. The value must be a string that ends in "s," such as 5s.

Verify your queue was configured successfully:

gcloud tasks queues describe [QUEUE_ID]

Turning on logging

Stackdriver logging can be turned on using a flag with either gcloud beta create or update:

gcloud beta tasks queues create [QUEUE_ID] --log-sampling-ratio=1.0

or

gcloud beta tasks queues update [QUEUE_ID] --log-sampling-ratio=1.0

where the log-sampling-ratio value indicates what percentage of the operations on the queue are logged. Setting it to any value between 1.0 and 0.0 logs that fraction of operations.

Turning off logging

Turn off logging by setting the flag to 0.0.

gcloud beta tasks queues update [QUEUE_ID] --log-sampling-ratio=0.0

Logged operations

  • Task Operations
    • CreateTask
    • DeleteTask
  • Attempt Operations
    • AttemptDispatch
    • AttemptResponse

Viewing logs

To see the logs, go to the Stackdriver Logging > Logs (Logs Viewer) page in the GCP Console:

Go to the Logs Viewer page

For more information on reading the logs page, see Viewing logs.

What's next

本頁內容對您是否有任何幫助?請提供意見:

傳送您對下列選項的寶貴意見...

這個網頁
Cloud Tasks Documentation