Adicionar uma tarefa de destino HTTP a uma fila do Cloud Tasks
Neste guia de início rápido, mostramos como adicionar uma tarefa de destino HTTP a uma fila do Cloud Tasks usando a API Cloud Tasks.
Antes de começar
- 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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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 Tasks API:
gcloud services enable cloudtasks.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.
If an authentication error is returned, confirm that you have configured the gcloud CLI to use Workforce Identity Federation.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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 Tasks API:
gcloud services enable cloudtasks.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.
If an authentication error is returned, confirm that you have configured the gcloud CLI to use Workforce Identity Federation.
Criar uma fila do Cloud Tasks
Use o comando gcloud tasks queues create
para criar a fila.
No terminal, crie uma fila que registre todas as operações.
gcloud tasks queues create QUEUE_NAME \ --log-sampling-ratio=1.0 \ --location=REGION
Substitua:
QUEUE_NAME
: um nome para a fila de tarefas do Cloud TasksREGION
: a região em que você implantou o serviço ou app
Aguarde a inicialização da fila e verifique se ela foi criada com sucesso.
gcloud tasks queues describe QUEUE_NAME \ --location=REGION
A saída será semelhante a esta:
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
Adicionar uma tarefa à fila do Cloud Tasks
Use o comando
gcloud tasks create-http-task
para criar uma tarefa que tenha como destino um endpoint HTTP e adicione a tarefa à
fila.
Crie uma tarefa, adicione-a à fila criada e entregue-a a um endpoint HTTP.
gcloud tasks create-http-task \ --queue=QUEUE_NAME \ --url=URL_PATH \ --method=GET \ --location=REGION \ --project=PROJECT_ID
Substitua
URL_PATH
pelo caminho de URL completo para onde a solicitação será enviada. Por exemplo:https://www.google.com
O caminho precisa começar com
http://
ouhttps://
.Leia os registros para verificar se a tarefa foi executada.
gcloud logging read --limit=3
Os registros devem ser semelhantes a este:
jsonPayload: '@type': type.googleapis.com/google.cloud.tasks.logging.v1.TaskActivityLog task: projects/PROJECT_ID/locations/REGION/queues/QUEUE_NAME/tasks/TASK_ID taskCreationLog: scheduleTime: '2024-07-04T19:00:27.801837Z' status: OK targetAddress: GET https://www.google.com/ targetType: HTTP
Limpar
Para evitar cobranças na sua conta do Google Cloud pelos recursos usados nesta página, exclua o projeto do Google Cloud com esses recursos.
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
Também é possível excluir a fila do Cloud Tasks:
gcloud tasks queues delete QUEUE_NAME \
--location=REGION