透過 Google Cloud 控制台安排及執行 Cron 工作
本快速入門導覽課程說明如何使用 Google Cloud 控制台,透過 Cloud Scheduler 執行一些基本作業。
在本快速入門導覽課程中,您將:
- 建立 Pub/Sub 主題,做為 Cloud Scheduler 工作目標。
- 使用 Cloud Scheduler 建立 Cron 工作,並為該工作設定週期性排程。
- 執行工作。
- 確認工作已成功執行。
Cloud Scheduler 提供免費方案,因此執行本快速入門導覽課程不會產生任何費用。詳情請參閱「定價」。
如要直接在 Google Cloud 控制台中,按照這項工作的逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Scheduler and 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.enablepermission. Learn how to grant roles.-
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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Scheduler and 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.enablepermission. Learn how to grant roles.
必要的角色
如要取得完成本快速入門導覽課程所需的權限,請要求管理員在專案中授予您下列 IAM 角色:
-
Cloud Scheduler 管理員 (
roles/cloudscheduler.admin) -
Pub/Sub 編輯者 (
roles/pubsub.editor)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
建立 Pub/Sub 主題和訂閱項目
Pub/Sub 主題是發布端可傳送訊息的資源。如要接收發布至某項主題的訊息,您必須為該主題建立訂閱項目。
前往 Google Cloud 控制台的 Pub/Sub 頁面。
按一下「建立主題」。
在「主題 ID」欄位中,輸入 cron-topic 做為主題的 ID。
保留預設值,包括「新增預設訂閱項目」選項。
請勿選取任何其他選項。
點選「建立」。
這會建立名為 cron-topic 的 Pub/Sub 主題,以及名為 cron-topic-sub 的主題訂閱項目。
使用 Cloud Scheduler 建立 Cron 工作
設定排程工作單元 (也稱為「Cron 工作」),根據週期性排程將工作傳送到目標。
前往 Google Cloud 控制台的「Cloud Scheduler」頁面。
按一下「Create job」(建立工作)。
為工作命名。
在「Region」(區域) 清單中,選取工作所在的區域。
使用以 unix-cron 為基礎的格式為工作指定頻率。例如:
30 16 * * 7詳情請參閱「設定 Cron 工作時間表」。
在「時區」清單中選取時區。
在某些時區,日光節約時間可能會導致工作意外執行或未執行。詳情請參閱「時區」。
按一下「繼續」。
在「目標類型」清單中,選取「Pub/Sub」。
選取您先前建立的主題:
cron-topic。在「Message body」(郵件內文) 欄位中,輸入要傳送至 Pub/Sub 目標主題的字串,例如:「Hello world!」
點選「建立」。
您已建立一項工作,會在星期日 16:30 將訊息傳送至 Pub/Sub 主題。現在可以執行工作。
執行工作
除了按照指定時間表執行作業外,您也可以強制立即執行作業。
前往 Google Cloud 控制台的「Cloud Scheduler」頁面。
系統應會列出您的工作。
在工作的資料列中,依序按一下「 Actions」(動作) >「Force run」(強制執行)。
請注意,由於某些初始設定的影響,在專案中建立的第一項工作可能需要幾分鐘的時間才能執行。
如果在「Status of last execution」(上次執行狀態) 欄中看到
Success狀態,表示 Cron 工作執行成功。
接著,您可以確認 Pub/Sub 是否收到訊息。
在 Pub/Sub 中驗證結果
從訂閱項目提取 Pub/Sub 訊息,確認 Pub/Sub 主題是否收到工作傳送的訊息。
前往 Google Cloud 控制台的「Pub/Sub subscriptions」(Pub/Sub 訂閱項目) 頁面。
按一下 Pub/Sub 主題的訂閱項目。
按一下「Messages」(訊息) 分頁標籤。
按一下「Pull」(提取)。
您應該會看到發布的訊息,其中包含郵件內文、Hello world 和訊息發布時間。
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。
刪除您建立的 Cron 工作:
前往 Google Cloud 控制台的「Cloud Scheduler」頁面。
選取工作的核取方塊。
按一下「Delete」(刪除) 並確認刪除。
刪除 Pub/Sub 主題:
前往 Google Cloud 控制台的「Pub/Sub」頁面。
勾選主題的核取方塊。
按一下「Delete」(刪除) 並確認刪除。
刪除 Pub/Sub 訂閱項目:
前往 Google Cloud 控制台的「Pub/Sub」頁面。
勾選訂閱項目的核取方塊。
按一下「Delete」(刪除) 並確認刪除。
或者,如果您建立新專案的目的在於瞭解如何使用 Cloud Scheduler,但現在已不再需要這項專案,請刪除專案:
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。
後續步驟
- 進一步瞭解 Cloud Scheduler
- 瞭解如何查看 Cloud Scheduler 記錄檔