Google Cloud コンソールを使用して cron ジョブをスケジュールして実行する
このクイックスタートでは、 Google Cloud コンソールを使用して Cloud Scheduler で基本的なオペレーションを行う方法について説明します。
このクイックスタートでは、以下の操作を行います。
- Cloud Scheduler ジョブのターゲットとして設定する Pub/Sub トピックを作成します。
- Cloud Scheduler を使用して cron ジョブを作成し、ジョブの定期的なスケジュールを構成します。
- Job を実行します。
- ジョブが正常に実行されていることを確認します。
Cloud Scheduler には無料枠があるため、このクイックスタートを実行しても費用は発生しません。詳細については、料金をご覧ください。
このタスクを Google Cloud コンソールで直接行う際の順を追ったガイダンスについては、「ガイドを表示」をクリックしてください。
始める前に
- 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.
-
In the Google Cloud console, on the project selector page, select or create 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.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Scheduler, Pub/Sub APIs.
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. -
Make sure that you have the following role or roles on the project: Cloud Scheduler Admin, Pub/Sub Editor
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
IAM に移動 - プロジェクトを選択します。
- [ アクセスを許可] をクリックします。
-
[新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。
- [ロールを選択] リストでロールを選択します。
- 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
- [保存] をクリックします。
-
-
In the Google Cloud console, on the project selector page, select or create 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.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Scheduler, Pub/Sub APIs.
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. -
Make sure that you have the following role or roles on the project: Cloud Scheduler Admin, Pub/Sub Editor
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
IAM に移動 - プロジェクトを選択します。
- [ アクセスを許可] をクリックします。
-
[新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。
- [ロールを選択] リストでロールを選択します。
- 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
- [保存] をクリックします。
-
Google Cloud コンソールで、Pub/Sub ページに移動します。
[トピックを作成] をクリックします。
[トピック ID] フィールドに、トピックの ID として cron-topic を入力します。
[デフォルトのサブスクリプションを追加する] オプションを含むデフォルトは、そのまま保持します。
他のオプションは選択しないでください。
[作成] をクリックします。
Google Cloud コンソールで [Cloud Scheduler] ページに移動します。
[ジョブを作成] をクリックします。
ジョブに名前を付けます。
[リージョン] リストで、ジョブのリージョンを選択します。
ジョブの [頻度] を、unix-cron に基づく形式で指定します。次に例を示します。
30 16 * * 7
詳細については、cron ジョブ スケジュールの構成をご覧ください。
[タイムゾーン] リストで、タイムゾーンを選択します。
一部のタイムゾーンでは、夏時間が原因で、ジョブが予期せず実行されるか、実行されない可能性があります。詳細については、タイムゾーンをご覧ください。
[続行] をクリックします。
[ターゲット タイプ] リストで、Pub/Sub を選択します。
先ほど作成した、
cron-topic
のトピックを選択します。[メッセージ本文] フィールドに、Pub/Sub ターゲット トピックに送信する文字列を入力します。例: 「Hello world!」
[作成] をクリックします。
Google Cloud コンソールで [Cloud Scheduler] ページに移動します。
ジョブが一覧表示されます。
ジョブの行で、> [強制実行] をクリックします。
[アクション]初期構成によっては、プロジェクトで作成された最初のジョブの実行に数分かかることがあります。
[最後の実行のステータス] 列の
Success
ステータスに、cron ジョブが正常に実行されたことが表示されます。Google Cloud コンソールで、Pub/Sub の [サブスクリプション] ページに移動します。
Pub/Sub トピックのサブスクリプションをクリックします。
[メッセージ] タブをクリックします。
[PULL] をクリックします。
作成した cron ジョブを削除します。
Google Cloud コンソールで [Cloud Scheduler] ページに移動します。
ジョブのチェックボックスを選択します。
[削除] をクリックして、削除を確定します。
Pub/Sub トピックを削除します。
Google Cloud コンソールで、[Pub/Sub] ページに移動します。
トピックのチェックボックスを選択します。
[削除] をクリックして、削除を確定します。
Pub/Sub サブスクリプションを削除します
Google Cloud コンソールで、[Pub/Sub] ページに移動します。
サブスクリプションのチェックボックスを選択します。
[削除] をクリックして、削除を確定します。
また、Cloud Scheduler の学習用に新しいプロジェクトを作成し、そのプロジェクトが不要になった場合は、プロジェクトを削除します。
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
- Cloud Scheduler の詳細
- Cloud Scheduler のログを表示する方法を学ぶ
Pub/Sub トピックとサブスクリプションを作成する
Pub/Sub トピックは、パブリッシャーがメッセージを送信できるリソースです。トピックにパブリッシュされたメッセージを受信するには、そのトピックへのサブスクリプションを作成する必要があります。
これにより、cron-topic
という Pub/Sub トピックと、そのトピックのサブスクリプション cron-topic-sub
が作成されます。
Cloud Scheduler を使用して cron ジョブを作成する
定期的なスケジュールでターゲットに送信される、cron ジョブと呼ばれる定期的な作業単位を設定します。
日曜日の 16:30 に Pub/Sub トピックにメッセージを送信するジョブを作成しました。このジョブを実行できるようになりました。
ジョブを実行する
指定したスケジュールに沿ってジョブを実行するだけでなく、即時実行を強制的に行うこともできます。
次に、Pub/Sub がメッセージを受信したことを確認します。
Pub/Sub で結果を確認する
サブスクリプションから Pub/Sub メッセージを pull して、Pub/Sub トピックがジョブからメッセージを受け取ったことを確認します。
パブリッシュしたメッセージが表示されます。メッセージ本文 Hello world
と、メッセージがパブリッシュされた時刻が表示されます。
クリーンアップ
このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の手順を実施します。