适用于 Cloud Scheduler 的连接器

定义用于在工作流内访问 Cloud Scheduler 的内置函数的 Workflows 连接器。

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

YAML

# This workflow demonstrates how to use the Cloud Scheduler connector:
# Create a Pub/Sub topic and subscription for notifications
# Create a Cloud Scheduler job that runs every minute and publishes
# a message to the Pub/Sub topic when finished
# Retrieve message from the subscription and then delete all resources
# Expected output: "SUCCESS"
- init:
    assign:
      - project_id: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
      - location_id: "us-central1"
      - job_id: "example-job-id"
      - topic: "example-topic-id"
      - subscription: "example-subscription-id"
- create_topic:
    call: googleapis.pubsub.v1.projects.topics.create
    args:
      name: ${"projects/" + project_id + "/topics/" + topic}
- create_subscription_to_topic:
    call: googleapis.pubsub.v1.projects.subscriptions.create
    args:
      name: ${"projects/" + project_id + "/subscriptions/" + subscription}
      body:
        name: ${"projects/" + project_id + "/subscriptions/" + subscription}
        topic: ${"projects/" + project_id + "/topics/" + topic}
- create_job:
    call: googleapis.cloudscheduler.v1.projects.locations.jobs.create
    args:
      parent: ${"projects/" + project_id + "/locations/" + location_id}
      body:
        name: ${"projects/" + project_id + "/locations/" + location_id + "/jobs/" + job_id}
        description: "An example job created by Workflows to test the Cloud Scheduler connector."
        schedule: "* * * * *"  # runs job every minute
        pubsubTarget:
          topicName: ${"projects/" + project_id + "/topics/" + topic}
          attributes:
            job: "finished!"
- sleep:
    call: sys.sleep
    args:
      seconds: 100  # sleeps for 100 seconds to allow job to run
- pull_message:
    call: googleapis.pubsub.v1.projects.subscriptions.pull
    args:
      subscription: ${"projects/" + project_id + "/subscriptions/" + subscription}
      body:
        maxMessages: 1
- delete_job:
    call: googleapis.cloudscheduler.v1.projects.locations.jobs.delete
    args:
      name: ${"projects/" + project_id + "/locations/" + location_id + "/jobs/" + job_id}
- delete_subscription:
    call: googleapis.pubsub.v1.projects.subscriptions.delete
    args:
      subscription: ${"projects/" + project_id + "/subscriptions/" + subscription}
- delete_topic:
    call: googleapis.pubsub.v1.projects.topics.delete
    args:
      topic: ${"projects/" + project_id + "/topics/" + topic}
- the_end:
    return: "SUCCESS"

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器