透過 Google Cloud 控制台安排及執行 Cron 工作

本快速入門導覽課程說明如何使用 Google Cloud 控制台,透過 Cloud Scheduler 執行一些基本作業。

在本快速入門導覽課程中,您將:

  1. 建立 Pub/Sub 主題,做為 Cloud Scheduler 工作目標。
  2. 使用 Cloud Scheduler 建立 Cron 工作,並為該工作設定週期性排程。
  3. 執行工作。
  4. 確認工作已成功執行。

Cloud Scheduler 提供免費方案,因此執行本快速入門導覽課程不會產生任何費用。詳情請參閱「定價」。


如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


事前準備

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Scheduler, Pub/Sub APIs.

    Enable the APIs

  5. Make sure that you have the following role or roles on the project: Cloud Scheduler Admin, Pub/Sub Editor

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. 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.

    4. 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

    1. In the Google Cloud console, go to the IAM page.

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Cloud Scheduler, Pub/Sub APIs.

    Enable the APIs

  9. Make sure that you have the following role or roles on the project: Cloud Scheduler Admin, Pub/Sub Editor

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. 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.

    4. 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

    1. In the Google Cloud console, go to the IAM page.

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]

建立 Pub/Sub 主題和訂閱項目

Pub/Sub 主題是發布者可傳送訊息的資源。 如要接收發布至某項主題的訊息,您必須為該主題建立訂閱項目。

  1. 前往 Google Cloud 控制台的 Pub/Sub 頁面。

    前往 Pub/Sub

  2. 按一下「建立主題」

  3. 在「主題 ID」欄位中,輸入 cron-topic 做為主題的 ID。

  4. 保留預設值,包括「Add a default subscription」(新增預設訂閱) 選項。

  5. 請勿選取任何其他選項。

  6. 點選「建立」

這會建立名為「cron-topic」的 Pub/Sub 主題,以及名為「cron-topic-sub」的主題訂閱項目。

使用 Cloud Scheduler 建立 Cron 工作

設定工作的排程單元 (也稱為「Cron 工作」),根據週期性排程將 Cron 工作傳送到目標。

  1. 前往 Google Cloud 控制台的「Cloud Scheduler」頁面。

    前往 Cloud Scheduler

  2. 按一下「Create job」(建立工作)

  3. 為工作命名。

  4. 在「Region」(區域) 清單中,選取工作所在的區域。

  5. 使用以 unix-cron 為基礎的格式為工作指定頻率。例如:

    30 16 * * 7
    

    詳情請參閱「設定 Cron 工作時間表」。

  6. 在「時區」清單中選取時區。

    在某些時區,日光節約時間可能會導致工作意外執行或未執行。詳情請參閱「時區」。

  7. 按一下「繼續」

  8. 在「Target type」(目標類型) 清單中,選取「Pub/Sub」

  9. 選取您先前建立的主題:cron-topic

  10. 「Message body」(訊息內文) 欄位中,輸入要傳送至 Pub/Sub 目標主題的字串,例如:「Hello world!」

  11. 點選「建立」

您已建立一項工作,會在週日 16:30 將訊息傳送至 Pub/Sub 主題。現在可以執行工作。

執行工作

除了按照指定時間表執行作業外,您也可以強制立即執行作業。

  1. 前往 Google Cloud 控制台的「Cloud Scheduler」頁面。

    前往 Cloud Scheduler

    系統應會列出您的工作。

  2. 在工作的資料列中,依序按一下「 Actions」(動作) >「Force run」(強制執行)

    請注意,由於某些初始設定的影響,在專案中建立的第一項工作可能需要幾分鐘的時間才能執行。

  3. 如果在「Status of last execution」(上次執行狀態) 欄中看到 Success 狀態,表示 Cron 工作執行成功。

接著,您可以確認 Pub/Sub 是否收到訊息。

在 Pub/Sub 中驗證結果

從訂閱項目提取 Pub/Sub 訊息,確認 Pub/Sub 主題是否收到作業傳送的訊息。

  1. 前往 Google Cloud 控制台的「Pub/Sub subscriptions」(Pub/Sub 訂閱項目) 頁面。

    前往 Pub/Sub

  2. 按一下 Pub/Sub 主題的訂閱項目。

  3. 按一下「Messages」(訊息) 分頁標籤。

  4. 按一下「Pull」(提取)

您應該會看到發布的訊息,其中包含訊息主體、Hello world 和訊息發布時間。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。

  1. 刪除您建立的 Cron 工作:

    1. 前往 Google Cloud 控制台的「Cloud Scheduler」頁面。

      前往 Cloud Scheduler

    2. 選取工作的核取方塊。

    3. 按一下「Delete」(刪除) 並確認刪除。

  2. 刪除 Pub/Sub 主題:

    1. 前往 Google Cloud 控制台的「Pub/Sub」頁面。

      前往 Pub/Sub

    2. 勾選主題的核取方塊。

    3. 按一下「Delete」(刪除) 並確認刪除。

  3. 刪除 Pub/Sub 訂閱項目:

    1. 前往 Google Cloud 控制台的「Pub/Sub」頁面。

      前往 Pub/Sub

    2. 勾選訂閱項目的核取方塊。

    3. 按一下「Delete」(刪除) 並確認刪除。

  4. 或者,如果您建立新專案的目的在於瞭解如何使用 Cloud Scheduler,但現在已不再需要這項專案,請刪除專案:

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

後續步驟