Cloud Tasks キューに HTTP ターゲット タスクを追加する
このクイックスタートでは、Cloud Tasks API を使用して HTTP ターゲット タスクを Cloud Tasks キューに追加する方法について説明します。
始める前に
- 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.
-
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, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
- Install the Google Cloud CLI.
-
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, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
Cloud Tasks キューを作成する
gcloud tasks queues create
コマンドを使用してキューを作成します。
ターミナルで、すべてのオペレーションをロギングするキューを作成します。
gcloud tasks queues create QUEUE_NAME \ --log-sampling-ratio=1.0 \ --location=REGION
以下を置き換えます。
QUEUE_NAME
: Cloud Tasks キューの名前REGION
: サービスまたはアプリをデプロイしたリージョン
キューが初期化されるまで待ってから、キューが正常に作成されたことを確認します。
gcloud tasks queues describe QUEUE_NAME \ --location=REGION
出力例を以下に示します。
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
Cloud Tasks キューにタスクを追加する
gcloud tasks create-http-task
コマンドを使用して、HTTP エンドポイントをターゲットとするタスクを作成し、キューにタスクを追加します。
タスクを作成して作成したキューに追加し、そのタスクを HTTP エンドポイントに配信します。
gcloud tasks create-http-task \ --queue=QUEUE_NAME \ --url=URL_PATH \ --method=GET \ --location=REGION \ --project=PROJECT_ID
URL_PATH
は、リクエストを送信する完全な URL パスに置き換えます。例:https://www.google.com
パスは
http://
またはhttps://
で始まる必要があります。ログを読み取って、タスクが正常に実行されたことを確認します。
gcloud logging read --limit=3
ログは次のようになります。
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
クリーンアップ
このページで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、Google Cloud プロジェクトとそのリソースをまとめて削除してください。
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
または、Cloud Tasks キューを削除することもできます。
gcloud tasks queues delete QUEUE_NAME \
--location=REGION