You can use Cloud Tasks to queue a workflow and execute it
asynchronously.

Cloud Tasks offers the following controls:

- Schedule specific delivery times
- Manage delivery rates
- Configure retry behavior
- Access and manage individual tasks in a queue
- Enable task deduplication

These controls can be useful, for example, to manage requests that trigger a
workflow and that could unexpectedly exceed Workflows limits. By
configuring a Cloud Tasks queue with rate limits and retries,
you can ensure that all such requests result in workflow executions.

For more information, see
[Understand Cloud Tasks](https://docs.cloud.google.com/tasks/docs/dual-overview.md.txt).

Note the following:

- You should base the rate limit on the Workflows limit
  for [Execution API write requests](https://docs.cloud.google.com/workflows/quotas.md.txt#quotas). If
  necessary, you can request an adjustment to most quotas in the Google Cloud console.
  Find out more about
  [quota adjustments](https://docs.cloud.google.com/docs/quotas/overview.md.txt#about_increase_requests).

- Cloud Tasks is designed to provide "at least once" delivery;
  however, Workflows doesn't ensure exactly-once processing of
  duplicate requests from Cloud Tasks.

- You can remove execution delays and maximize throughput by enabling
  execution backlogging in Workflows. Backlogged executions
  automatically run as soon as execution concurrency quota becomes available. For
  more information, see
  [Manage execution backlogging](https://docs.cloud.google.com/workflows/docs/executing-workflow.md.txt#manage_execution_backlogging).

## Before you begin

1. If you don't already have a workflow that you would like to queue, [create one](https://docs.cloud.google.com/workflows/docs/creating-updating-workflow.md.txt#creating_a_workflow).
2.


   Enable the Cloud Tasks API, if it is not already enabled.


   **Roles required to enable APIs**


   To enable APIs, you need the `serviceusage.services.enable` permission. If you
   created the project, then you likely already have this permission through the
   Owner role (`roles/owner`). Otherwise, you can get this permission through the
   Service Usage Admin role (`roles/serviceusage.serviceUsageAdmin`).
   [Learn how to grant roles](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).

   [Enable the API](https://console.cloud.google.com/apis/enableflow?apiid=cloudtasks.googleapis.com)

## Queue a task to execute a workflow

1. Create a [service account](https://docs.cloud.google.com/workflows/docs/authentication.md.txt)
   so that Cloud Tasks can make requests to the Workflows API:

   ```bash
   gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
   ```

   Replace `SERVICE_ACCOUNT_NAME` with a name that is
   between 6 and 30 characters. It can contain lowercase alphanumeric characters
   and dashes. After you create a service account, you cannot change its name.
2. To allow the principal that will run your Cloud Tasks
   commands the ability to act as an Identity and Access Management (IAM) service
   account,
   [grant a role that allows the principal to impersonate the service account](https://docs.cloud.google.com/iam/docs/service-account-permissions.md.txt#roles).

3. Grant your new service account the
   [workflows.invoker](https://docs.cloud.google.com/workflows/docs/access-control.md.txt#roles) role so that the
   account has permission to trigger your workflow:

   ```bash
   gcloud projects add-iam-policy-binding PROJECT_NAME \
       --member serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_NAME.iam.gserviceaccount.com \
       --role roles/workflows.invoker
   ```

   Replace the following:
   - `PROJECT_NAME`: the name of your Google Cloud project.
   - `SERVICE_ACCOUNT_NAME`: the name of the service account that you previously created.
4. If you don't already have a Cloud Tasks queue,
   [create one](https://docs.cloud.google.com/tasks/docs/creating-queues.md.txt).

5. Create a task that targets your workflow's HTTP endpoint, using the
   service account you previously created to authenticate:

   ```bash
   gcloud tasks create-http-task \
       --queue="QUEUE_ID" \
       --url="https://workflowexecutions.googleapis.com/v1/projects/PROJECT_NAME/locations/REGION_NAME/workflows/WORKFLOW_NAME/executions" \
       --body-content="{\"argument\": \"DOUBLE_ESCAPED_JSON_STRING\"}" \
       --oauth-service-account-email="SERVICE_ACCOUNT_NAME@PROJECT_NAME.iam.gserviceaccount.com"
   ```

   Replace the following:
   - `QUEUE_ID`: the identifier you assigned to your Cloud Tasks queue; for example, `my-queue`.
   - `PROJECT_NAME`: the name of your Google Cloud project.
   - `REGION_NAME`: the region your workflow is in, such as `us-central1`.
   - `WORKFLOW_NAME`: the name of the workflow you want to create a task for.
   - `DOUBLE_ESCAPED_JSON_STRING`: a JSON encoding of any arguments you are passing. The double quotation marks inside the quoted string are escaped using backslashes (\\). For example: `--body-content="{\"argument\": \"{\\\"foo\\\": \\\"bar\\\"}\"}"`
   - `SERVICE_ACCOUNT_NAME`: the name of the service account that you previously created.

   Your task is created and added to your Cloud Tasks queue.
   The task persists in the queue until the workflow execution starts and
   returns an HTTP `2xx` status code, at which point Cloud Tasks
   considers the task complete.

   > [!NOTE]
   > **Note:** Cloud Tasks retries failures according to the [retry settings](https://docs.cloud.google.com/tasks/docs/reference/rpc/google.cloud.tasks.v2.md.txt#retryconfig), and does not monitor long-running operations. (See how to [configure the maximum duration](https://docs.cloud.google.com/tasks/docs/reference/rpc/google.cloud.tasks.v2.md.txt#task).) Issues starting the workflow execution (for example, an `HTTP 429` when there are too many requests) can be handled by Cloud Tasks but workflow execution failures are not retried. Each execution of a workflow incurs costs, as detailed in [Workflows pricing](https://cloud.google.com/workflows/pricing).

## What's next

- [Tutorial: Use a Cloud Tasks queue to buffer your workflow executions](https://docs.cloud.google.com/workflows/docs/tutorials/buffer-workflows-executions.md.txt)
- [Manage queues and tasks](https://docs.cloud.google.com/tasks/docs/deleting-appengine-queues-and-tasks)
- [Manage execution backlogging](https://docs.cloud.google.com/workflows/docs/executing-workflow.md.txt#manage_execution_backlogging)
- [Pass runtime arguments in an execution request](https://docs.cloud.google.com/workflows/docs/passing-runtime-arguments.md.txt)