Agrega una tarea de destino HTTP a una cola de Cloud Tasks

En esta guía de inicio rápido, aprenderás a agregar un Tarea de destino HTTP a una Lista de tareas en cola de Cloud Tasks con la API de Cloud Tasks.

Antes de comenzar

  1. 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.
  2. Install the Google Cloud CLI.
  3. To initialize the gcloud CLI, run the following command:

    gcloud init
  4. 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.

  5. Make sure that billing is enabled for your Google Cloud project.

  6. Enable the Cloud Tasks API:

    gcloud services enable tasks.googleapis.com
  7. 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.

  8. Install the Google Cloud CLI.
  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. 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.

  11. Make sure that billing is enabled for your Google Cloud project.

  12. Enable the Cloud Tasks API:

    gcloud services enable tasks.googleapis.com
  13. 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.

Crea una cola de Cloud Tasks

Usa la gcloud tasks queues create. para crear tu cola.

  1. En la terminal, crea una cola que registre todas las operaciones.

    gcloud tasks queues create QUEUE_NAME \
        --log-sampling-ratio=1.0 \
        --location=REGION
    

    Reemplaza lo siguiente:

    • QUEUE_NAME: Un nombre para la fila de Cloud Tasks
    • REGION: Es la región en la que implementaste tu servicio o app.
  2. Espera a que se inicialice la cola y, luego, verifica que se haya creado. con éxito.

    gcloud tasks queues describe QUEUE_NAME \
        --location=REGION
    

    El resultado debería ser similar al siguiente ejemplo:

     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
    

Agrega una tarea a la cola de Cloud Tasks

Usa el comando gcloud tasks create-http-task para crear una tarea que se oriente a un extremo HTTP y agrégala a tu fila.

  1. Crea una tarea, agrégala a la cola que creaste y entrégala a un extremo HTTP.

    gcloud tasks create-http-task \
        --queue=QUEUE_NAME \
        --url=URL_PATH \
        --method=GET \
        --location=REGION \
        --project=PROJECT_ID
    

    Reemplaza URL_PATH por la ruta de URL completa a la que a la que se enviará la solicitud. Por ejemplo: https://www.google.com.

    La ruta de acceso debe comenzar con http:// o https://.

  2. Verifica que la tarea se haya ejecutado correctamente mediante la lectura de los registros.

    gcloud logging read --limit=3
    

    Los registros deberían ser similares al siguiente:

    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
    

Limpia

Para evitar que se apliquen cargos a tu cuenta de Google Cloud por los recursos que se usaron en esta página, borra el proyecto de Cloud que tiene los recursos.

Delete a Google Cloud project:

gcloud projects delete PROJECT_ID

Como alternativa, puedes borrar la cola de Cloud Tasks:

gcloud tasks queues delete QUEUE_NAME \
    --location=REGION

¿Qué sigue?