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.
-
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
-
gcloud CLI を初期化するには、次のコマンドを実行します。
gcloud init
-
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
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:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles.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.
-
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
-
gcloud CLI を初期化するには、次のコマンドを実行します。
gcloud init
-
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
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:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles.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 管理者(
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 アプリケーションが作成されました」というメッセージが表示されます。
ターミナルで、ローカルマシンにサンプル アプリケーション リポジトリのクローンを作成します。
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!
が表示されます。ターミナルで、すべてのオペレーションをロギングするキューを作成します。
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
次の環境変数を設定します。クライアントは、この情報を使用してリクエストを作成します。
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
以下のように、Cloud Tasks キューを削除します。
gcloud tasks queues delete QUEUE_NAME \ --location=REGION
App Engine アプリケーションを追加する
App Engine タスクをターゲットにする場合、App Engine スタンダード環境にアプリをデプロイする前に、App Engine アプリケーションをプロジェクトに追加する必要があります。
サンプルをインストールしてデプロイする
このクイックスタートで使用する Node.js サンプルは 2 つのファイルで構成されています。createTask.js
は、タスクを作成して Tasks キューに追加するコマンドライン ツールとしてローカルで実行されます。server.js
は、タスクを処理するワーカー サービスとして App Engine にデプロイされます。
Cloud Tasks キューを作成する
gcloud tasks queues create
コマンドを使用して、準備した環境にキューを作成します。
Cloud Tasks キューにタスクを追加する
タスクを作成して作成したキューに追加し、そのタスクをワーカー サービスに配信します。
クリーンアップ
このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、リソースを含む Google Cloud プロジェクトを削除します。
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
また、作成したリソースを削除することもできます。