透過 Google Cloud 控制台安排及執行 Cron 工作
本快速入門導覽課程說明如何使用 Google Cloud 控制台,透過 Cloud Scheduler 執行一些基本作業。
在本快速入門導覽課程中,您將:
- 建立 Pub/Sub 主題,做為 Cloud Scheduler 工作目標。
- 使用 Cloud Scheduler 建立 Cron 工作,並為該工作設定週期性排程。
- 執行工作。
- 確認工作已成功執行。
Cloud Scheduler 提供免費方案,因此執行本快速入門導覽課程不會產生任何費用。詳情請參閱「定價」。
如要直接在 Google Cloud 控制台中,按照這項工作的逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
- 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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
如要使用現有專案進行本指南中的操作,請確認您具有所需的權限。如果您建立新專案,則已具備必要權限。
-
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.
-
如要使用現有專案進行本指南中的操作,請確認您具有所需的權限。如果您建立新專案,則已具備必要權限。
-
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. -
Cloud Scheduler 管理員 (
roles/cloudscheduler.admin) -
Pub/Sub 編輯者 (
roles/pubsub.editor) 前往 Google Cloud 控制台的 Pub/Sub 頁面。
按一下「建立主題」。
在「主題 ID」欄位中,輸入 cron-topic 做為主題的 ID。
保留預設值,包括「新增預設訂閱項目」選項。
請勿選取任何其他選項。
點選「建立」。
前往 Google Cloud 控制台的「Cloud Scheduler」頁面。
按一下「Create job」(建立工作)。
為工作命名。
在「Region」(區域) 清單中,選取工作所在的區域。
使用以 unix-cron 為基礎的格式為工作指定頻率。例如:
30 16 * * 7詳情請參閱「設定 Cron 工作時間表」。
在「時區」清單中選取時區。
在某些時區,日光節約時間可能會導致工作意外執行或未執行。詳情請參閱「時區」。
按一下「繼續」。
在「目標類型」清單中,選取「Pub/Sub」。
選取您先前建立的主題:
cron-topic。在「Message body」(訊息內文) 欄位中,輸入要傳送至 Pub/Sub 目標主題的字串,例如:「Hello world!」
點選「建立」。
前往 Google Cloud 控制台的「Cloud Scheduler」頁面。
系統應會列出您的工作。
在工作的資料列中,依序按一下「 Actions」(動作) >「Force run」(強制執行)。
請注意,由於某些初始設定的影響,在專案中建立的第一項工作可能需要幾分鐘的時間才能執行。
如果在「Status of last execution」(上次執行狀態) 欄中看到
Success狀態,表示 Cron 工作執行成功。前往 Google Cloud 控制台的「Pub/Sub subscriptions」(Pub/Sub 訂閱項目) 頁面。
按一下 Pub/Sub 主題的訂閱項目。
按一下「Messages」(訊息) 分頁標籤。
按一下「Pull」(提取)。
刪除您建立的 Cron 工作:
前往 Google Cloud 控制台的「Cloud Scheduler」頁面。
選取工作的核取方塊。
按一下「Delete」(刪除) 並確認刪除。
刪除 Pub/Sub 主題:
前往 Google Cloud 控制台的「Pub/Sub」頁面。
勾選主題的核取方塊。
按一下「Delete」(刪除) 並確認刪除。
刪除 Pub/Sub 訂閱項目:
前往 Google Cloud 控制台的「Pub/Sub」頁面。
勾選訂閱項目的核取方塊。
按一下「Delete」(刪除) 並確認刪除。
或者,如果您建立新專案的目的在於瞭解如何使用 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 記錄檔
必要的角色
如要取得完成本快速入門導覽課程所需的權限,請要求管理員在專案中授予您下列 IAM 角色:
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
建立 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 訊息,確認 Pub/Sub 主題是否收到工作傳送的訊息。
畫面上會顯示您發布的訊息,包括訊息內文、Hello world 和訊息發布時間。
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。