App Engine タスクを Cloud Tasks キューに追加する
このクイックスタートでは、Cloud Tasks API を使用して App Engine タスクを 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.
-
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.
-
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager and Cloud Tasks API:
gcloud services enable cloudresourcemanager.googleapis.com tasks.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.
-
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.
-
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager and Cloud Tasks API:
gcloud services enable cloudresourcemanager.googleapis.com tasks.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.
- App Engine のデフォルトのサービス アカウントは、App Engine を使用すると自動的に作成されます。このクイックスタートでは、このサービス アカウントを使用できます。ただし、組織のポリシーの構成によっては、デフォルトのサービス アカウントにプロジェクトに対する
Editor
ロールが自動的に付与されない場合があります。その場合は、サービス アカウントに次のロールを付与する必要があります。- Artifact Registry 管理者(
roles/artifactregistry.admin
) - Artifact Registry の Create-on-Push ライター(
roles/artifactregistry.createOnPushWriter
) - Compute 管理者(
roles/compute.admin
) - ログ ライター(
roles/logging.logWriter
) - Storage オブジェクト閲覧者(
roles/storage.objectViewer
)
- Artifact Registry 管理者(
App Engine アプリケーションを追加する
App Engine タスクをターゲットとする場合は、App Engine スタンダード環境にアプリをデプロイする前に、App Engine アプリケーションをプロジェクトに追加する必要があります。
Google Cloud コンソールで [App Engine] ページに移動します。
[App Engine へようこそ] ダイアログで、次のいずれかを行います。
App Engine アプリケーションをすでに作成していて、[App Engine アプリケーションが作成されました] というメッセージが表示されている場合は、このセクションの残りの手順をスキップして、サンプルをインストールしてデプロイするセクションの手順に進みます。
または
App Engine アプリケーションをまだ作成していない場合は、[アプリケーションを作成] をクリックして、このセクションの残りの手順に進みます。
アプリケーションのリージョンを選択し、メモします。
europe-west
とus-central
は、Cloud Tasks コマンドではそれぞれeurope-west1
とus-central1
と呼ばれます。サービス アカウントは選択しないでください。デフォルトの App Engine サービス アカウントが使用されます。
[次へ] をクリックします。
アプリケーションが構成され、作成されます。完了まで数分かかる場合があります。
Cloud SDK はダウンロードせずに、[後で行う] をクリックします。
[App Engine アプリケーションが作成されました] というメッセージが表示されます。
サンプルをインストールしてデプロイする
このクイックスタートで使用する Node.js サンプルは、2 つのファイルで構成されています。createTask.js
は、タスクを作成してタスクキューに追加するコマンドライン ツールとしてローカルで実行されます。server.js
は、タスクを処理するワーカー サービスとして App Engine にデプロイされます。
ターミナルで、ローカルマシンにサンプルアプリのリポジトリのクローンを作成します。
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
サンプルコードが含まれるディレクトリに移動します。
cd nodejs-docs-samples/cloud-tasks/snippets
Node.js パッケージ管理システムを使用してすべての依存関係をインストールします。
NPM を使用できます。
npm install
または、Yarn を使用することもできます。
yarn install
ワーカー サービス(
server.js
)を App Engine スタンダード環境にデプロイします。gcloud app deploy app.yaml
サービスを含むアプリが実行されていることを確認します。
gcloud app browse
ブラウザで、指定されたリンクに移動します。次に例を示します。
https://PROJECT_ID.uc.r.appspot.com/
Hello, World!
が表示されます。
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 キューにタスクを追加する
タスクを作成して作成したキューに追加し、そのタスクをワーカー サービスに配信します。
次の環境変数を設定します。クライアントは、この情報を使用してリクエストを作成します。
export PROJECT_ID=PROJECT_ID export LOCATION_ID=REGION export QUEUE_ID=QUEUE_NAME
ペイロードとして
hello
を含めたタスクを作成して、キューに追加します。 ペイロードは、ワーカー サービスがタスクを処理するために必要なリクエストのデータです。node createTask.js $PROJECT_ID $QUEUE_ID $LOCATION_ID hello
ワーカー サービスのログを表示して、タスクが実行されたことを確認します。
gcloud app logs read
ログは次のようになります。
2024-06-20 15:00:00 default[20240620t143852] "POST /log_payload HTTP/1.1" 200 2024-06-20 15:00:00 default[20240620t143852] App listening on port 8081 2024-06-20 15:00:00 default[20240620t143852] Press Ctrl+C to quit. 2024-06-20 15:00:00 default[20240620t143852] Received task with payload: hello
クリーンアップ
このページで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、Google Cloud プロジェクトとそのリソースをまとめて削除してください。
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
作成したリソースを削除することもできます。
以下のように、Cloud Tasks キューを削除します。
gcloud tasks queues delete QUEUE_NAME \ --location=REGION