透過 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Scheduler, Pub/Sub APIs.
-
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」頁面 - 選取專案。
- 按一下「授予存取權」 。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。
- 在「Select a role」(選取角色) 清單中,選取角色。
- 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
- 按一下 [Save]。
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Scheduler, Pub/Sub APIs.
-
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」頁面 - 選取專案。
- 按一下「授予存取權」 。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。
- 在「Select a role」(選取角色) 清單中,選取角色。
- 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
- 按一下 [Save]。
-
建立 Pub/Sub 主題和訂閱項目
Pub/Sub 主題是發布者可傳送訊息的資源。 如要接收發布至某項主題的訊息,您必須為該主題建立訂閱項目。
前往 Google Cloud 控制台的 Pub/Sub 頁面。
按一下「建立主題」。
在「主題 ID」欄位中,輸入 cron-topic 做為主題的 ID。
保留預設值,包括「Add a default subscription」(新增預設訂閱) 選項。
請勿選取任何其他選項。
點選「建立」。
這會建立名為「cron-topic
」的 Pub/Sub 主題,以及名為「cron-topic-sub
」的主題訂閱項目。
使用 Cloud Scheduler 建立 Cron 工作
設定工作的排程單元 (也稱為「Cron 工作」),根據週期性排程將 Cron 工作傳送到目標。
前往 Google Cloud 控制台的「Cloud Scheduler」頁面。
按一下「Create job」(建立工作)。
為工作命名。
在「Region」(區域) 清單中,選取工作所在的區域。
使用以 unix-cron 為基礎的格式為工作指定頻率。例如:
30 16 * * 7
詳情請參閱「設定 Cron 工作時間表」。
在「時區」清單中選取時區。
在某些時區,日光節約時間可能會導致工作意外執行或未執行。詳情請參閱「時區」。
按一下「繼續」。
在「Target type」(目標類型) 清單中,選取「Pub/Sub」。
選取您先前建立的主題:
cron-topic
。在「Message body」(訊息內文) 欄位中,輸入要傳送至 Pub/Sub 目標主題的字串,例如:「Hello world!」
點選「建立」。
您已建立一項工作,會在週日 16:30 將訊息傳送至 Pub/Sub 主題。現在可以執行工作。
執行工作
除了按照指定時間表執行作業外,您也可以強制立即執行作業。
前往 Google Cloud 控制台的「Cloud Scheduler」頁面。
系統應會列出您的工作。
在工作的資料列中,依序按一下「>「Force run」(強制執行)。
Actions」(動作)請注意,由於某些初始設定的影響,在專案中建立的第一項工作可能需要幾分鐘的時間才能執行。
如果在「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,但現在已不再需要這項專案,請刪除專案:
- 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 記錄檔