The documentation you are viewing is for Dapr v1.6 which is an older version of Dapr. For up-to-date documentation, see the latest version.

Cron 绑定规范

Cron 绑定组件的详细文档

配置

要设置 cron 绑定,请创建一个类型为 bindings.cron 的组件。 请参阅本指南,了解如何创建和应用绑定配置。

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: <NAME>
  namespace: <NAMESPACE>
spec:
  type: bindings.cron
  version: v1
  metadata:
  - name: schedule
    value: "@every 15m" # valid cron schedule

元数据字段规范

字段 必填 绑定支持 详情 示例
schedule Input/Output 要用的有效的 cron 时间表。 请参阅这里了解更多详情。 "@every 15m"

计划格式

Dapr cron 绑定支持以下格式:

字符 描述器 可接受值
1 0 to 59, or *
2 分钟 0 to 59, or *
3 小时 0 to 23, or * (UTC)
4 月份中的天 1 to 31, or *
5 1 to 12, or *
6 一周中的一天 0-7(0和7代表星期日),或 *

例如:

  • 30 * * * * * - 每 30 秒
  • 0 15 * * * - 每 15 分钟
  • 0 30 3-6, 20-23 * * - 每半小时在上午3-6点,晚上8-11点范围内
  • CRON_TZ=America/New_York 0 30 04 * * * - 在每天纽约时间凌晨 4:30

您可以在这里了解更多关于cron和支持的格式

为便于使用,Dapr cron 绑定也支持少量快捷方式:

  • @every 15 ss 就是秒, m 为分钟, g 就是小时
  • @daily@hourly 它是从绑定初始化之时起运行的

监听 cron 绑定

在设置了cron绑定之后,您需要做的就是监听与您的组件名称匹配的 endpoint。 假设 [NAME] 是 scheduled。 这将作为一个 HTTP POST 请求。 下面的例子展示了一个简单的 Node.js Express 应用程序如何接收 /scheduled endpoint 上的调用,并将消息写入控制台。

app.post('/scheduled', async function(req, res){
    console.log("scheduled endpoint called", req.body)
    res.status(200).send()
});

在运行这段代码时,请注意 /scheduled endpoint 每五分钟被 Dapr sidecar 调用一次。

绑定支持

此组件支持 输入和输出 绑定接口。

该组件支持如下操作的 输出绑定

  • delete

相关链接