Aggiungi un'attività di destinazione HTTP a una coda Cloud Tasks

Questa guida rapida mostra come aggiungere un'attività di destinazione HTTP a una coda Cloud Tasks utilizzando l'API Cloud Tasks.

Prima di iniziare

  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. Se utilizzi un provider di identità (IdP) esterno, devi prima accedere alla gcloud CLI con la tua identità federata.

  4. Per inizializzare gcloud CLI, esegui questo comando:

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

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Tasks API:

    gcloud services enable cloudtasks.googleapis.com
  8. 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, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  9. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/cloudtasks.admin, roles/logging.viewer

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, myemail@example.com.
    • ROLE: the IAM role that you grant to your user account.
  10. Install the Google Cloud CLI.

  11. Se utilizzi un provider di identità (IdP) esterno, devi prima accedere alla gcloud CLI con la tua identità federata.

  12. Per inizializzare gcloud CLI, esegui questo comando:

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

  14. Verify that billing is enabled for your Google Cloud project.

  15. Enable the Cloud Tasks API:

    gcloud services enable cloudtasks.googleapis.com
  16. 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, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  17. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/cloudtasks.admin, roles/logging.viewer

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, myemail@example.com.
    • ROLE: the IAM role that you grant to your user account.
  18. Crea una coda di attività Cloud Tasks

    Utilizza il comando gcloud tasks queues create per creare la coda.

    1. Nel terminale, crea una coda che registri tutte le operazioni.

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

      Sostituisci quanto segue:

      • QUEUE_NAME: un nome per la coda Cloud Tasks
      • REGION: la regione supportata in cui hai eseguito il deployment del servizio o dell'app
    2. Attendi l'inizializzazione della coda e verifica che sia stata creata correttamente.

      gcloud tasks queues describe QUEUE_NAME \
          --location=REGION
      

      L'output dovrebbe essere simile al seguente:

       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
      

    Aggiungi un'attività alla coda Cloud Tasks

    Utilizza il comando gcloud tasks create-http-task per creare un'attività che ha come target un endpoint HTTP e aggiungerla alla coda.

    1. Crea un'attività, aggiungila alla coda che hai creato e inviala a un endpoint HTTP.

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

      Sostituisci URL_PATH con il percorso URL completo a cui verrà inviata la richiesta. Ad esempio: https://www.google.com

      Il percorso deve iniziare con http:// o https://.

    2. Verifica che l'attività sia stata eseguita correttamente leggendo i log.

      gcloud logging read --limit=3
      

      I log dovrebbero avere un aspetto simile al seguente:

      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
      

    Esegui la pulizia

    Per evitare che al tuo account Google Cloud vengano addebitati costi relativi alle risorse utilizzate in questa pagina, elimina il progetto Google Cloud con le risorse.

    Delete a Google Cloud project:

    gcloud projects delete PROJECT_ID

    In alternativa, puoi eliminare la coda Cloud Tasks:

    gcloud tasks queues delete QUEUE_NAME \
        --location=REGION
    

    Passaggi successivi