使用 Google Cloud 控制台安排和运行 Cron 作业

本快速入门介绍如何使用 Google Cloud 控制台通过 Cloud Scheduler 执行一些基本操作。

在本快速入门中,您将执行以下操作:

  1. 创建一个 Pub/Sub 主题,以设置为 Cloud Scheduler 作业目标。
  2. 使用 Cloud Scheduler 创建 Cron 作业,并为该作业配置周期性时间表。
  3. 运行作业。
  4. 验证作业是否已成功运行。

Cloud Scheduler 提供免费层级,运行此快速入门不应产生任何费用。要了解详情,请参阅价格


如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示

操作演示


准备工作

  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.

    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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. 如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。

  4. Verify that billing is enabled for your Google Cloud project.

  5. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  6. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. 如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。

  8. Verify that billing is enabled for your Google Cloud project.

  9. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  10. 所需的角色

    如需获得完成本快速入门所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:

    如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

    您也可以通过自定义角色或其他预定义角色来获取所需的权限。

    创建 Pub/Sub 主题和订阅

    Pub/Sub 主题是一种资源,发布者可以向其发送消息。如需接收发布到某个主题的消息,您必须创建对该主题的订阅。

    1. 在 Google Cloud 控制台中,前往 Pub/Sub 页面。

      前往 Pub/Sub

    2. 点击创建主题

    3. 主题 ID 字段中,输入 cron-topic 作为主题的 ID。

    4. 保留默认设置,包括添加默认订阅选项。

    5. 请勿选择任何其他选项。

    6. 点击创建

    这会创建一个名为 cron-topic 的 Pub/Sub 主题,以及一个名为 cron-topic-sub 的订阅。

    使用 Cloud Scheduler 创建 Cron 作业

    设置工作单元(称为 Cron 作业)的日程安排,以便按周期性时间表将该工作单元发送到目标。

    1. 在 Google Cloud 控制台中,前往 Cloud Scheduler 页面。

      前往 Cloud Scheduler

    2. 点击创建作业

    3. 为作业指定名称。

    4. 区域列表中,为您的作业选择一个区域。

    5. 使用基于 unix-cron 的格式为您的作业指定频率。例如:

      30 16 * * 7
      

      如需了解详情,请参阅配置 Cron 作业时间表

    6. 时区列表中,选择一个时区。

      在某些时区,夏时制可能会导致作业意外运行或不运行。如需了解详情,请参阅时区

    7. 点击继续

    8. 目标类型列表中,选择 Pub/Sub

    9. 选择您之前创建的主题:cron-topic

    10. 消息正文字段中,输入一条要发送到您的 Pub/Sub 目标主题的字符串,例如:“Hello world!”

    11. 点击创建

    您已创建一项作业,该作业会在周日 16:30 向 Pub/Sub 主题发送消息。您现在可以运行作业了。

    运行作业

    除了按照指定的时间表执行作业之外,您还可以强制作业立即运行。

    1. 在 Google Cloud 控制台中,前往 Cloud Scheduler 页面。

      前往 Cloud Scheduler

      系统应该会列出您的作业。

    2. 在您的作业所在的行中,点击 操作 > 强制运行

      请注意,由于需要进行一些初始配置,因此在项目中创建的第一项作业可能需要几分钟时间才能运行完毕。

    3. 上次执行的状态列中,Success 状态表示您已成功运行 Cron 作业。

    接下来,您可以验证 Pub/Sub 是否收到了消息。

    验证 Pub/Sub 中的结果

    通过从订阅中拉取 Pub/Sub 消息,验证您的 Pub/Sub 主题是否将收到来自作业的消息。

    1. 在 Google Cloud 控制台中,前往 Pub/Sub 订阅页面。

      前往 Pub/Sub

    2. 点击 Pub/Sub 主题的订阅。

    3. 点击消息标签页。

    4. 点击拉取

    您应该会看到您发布的消息,其中包含消息正文 Hello world 以及消息发布时间。

    清理

    为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

    1. 删除您创建的 Cron 作业:

      1. 在 Google Cloud 控制台中,前往 Cloud Scheduler 页面。

        前往 Cloud Scheduler

      2. 选中您的作业所对应的复选框。

      3. 点击删除并确认删除。

    2. 删除 Pub/Sub 主题:

      1. 在 Google Cloud 控制台中,前往 Pub/Sub 页面。

        前往 Pub/Sub

      2. 选中您的主题所对应的复选框。

      3. 点击删除并确认删除。

    3. 删除 Pub/Sub 订阅:

      1. 在 Google Cloud 控制台中,前往 Pub/Sub 页面。

        前往 Pub/Sub

      2. 选中您的订阅对应的复选框。

      3. 点击删除并确认删除。

    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.

    后续步骤