跳到主要内容

工作流触发器

触发器是工作流的起点,决定何时启动工作流执行。GeniSpace提供多种触发器类型,使您可以根据不同条件自动启动工作流。本文档将详细介绍各种触发器类型、配置方法和最佳实践。

触发器类型

时间触发器

时间触发器允许您基于时间表或定时设置来执行工作流。

定时触发器

按照固定的时间间隔执行工作流:

{
"type": "interval",
"config": {
"interval": 15,
"unit": "minutes" // seconds, minutes, hours, days
}
}

计划触发器

在特定时间点执行工作流:

{
"type": "schedule",
"config": {
"type": "daily",
"time": "09:00",
"timezone": "Asia/Shanghai"
}
}

Cron 触发器

使用 Cron 表达式设置更复杂的执行计划:

{
"type": "cron",
"config": {
"expression": "0 0 9 * * 1-5", // 周一至周五上午9点
"timezone": "Asia/Shanghai"
}
}

事件触发器

事件触发器响应系统内部或外部的特定事件。

WebHook 触发器

通过 HTTP 请求启动工作流:

{
"type": "webhook",
"config": {
"method": "POST",
"authentication": {
"type": "apiKey",
"headerName": "X-API-KEY"
},
"responseTemplate": {
"success": true,
"workflowId": "{{workflowId}}"
}
}
}

WebHook触发器会生成一个唯一的URL,您可以将其集成到第三方系统中。

数据库触发器

监控数据库变更并触发工作流:

{
"type": "database",
"config": {
"connection": "main_database",
"operation": "insert", // insert, update, delete
"table": "customers",
"condition": "status = 'new'"
}
}

文件触发器

监控文件系统变更:

{
"type": "file",
"config": {
"path": "/data/incoming",
"pattern": "*.csv",
"event": "created" // created, modified, deleted
}
}

邮件触发器

基于收到的电子邮件触发工作流:

{
"type": "email",
"config": {
"account": "support@example.com",
"subjectPattern": "订单确认",
"fromFilter": "*@customer.com",
"hasAttachment": true
}
}

API 触发器

REST API 触发器

通过 REST API 调用启动工作流:

{
"type": "api",
"config": {
"endpoint": "/api/v1/workflows/order-processor",
"method": "POST",
"authentication": {
"type": "oauth2"
},
"inputSchema": {
"type": "object",
"properties": {
"orderId": { "type": "string" },
"customer": { "type": "object" }
},
"required": ["orderId"]
}
}
}

消息队列触发器

从消息队列接收消息并触发工作流:

{
"type": "queue",
"config": {
"provider": "rabbitmq", // rabbitmq, kafka, sqs
"queue": "order-events",
"messageType": "order.created",
"batchSize": 10,
"connection": "mq_main"
}
}

条件触发器

数据条件触发器

当数据满足特定条件时触发工作流:

{
"type": "condition",
"config": {
"source": {
"type": "database",
"connection": "analytics_db",
"query": "SELECT COUNT(*) as count FROM errors WHERE severity = 'high' AND created_at > NOW() - INTERVAL '1 hour'"
},
"condition": "count > 10",
"checkInterval": 300 // 秒
}
}

复合条件触发器

组合多个条件的复杂触发器:

{
"type": "compositeCondition",
"config": {
"operator": "AND", // AND, OR
"conditions": [
{
"source": "database",
"query": "SELECT COUNT(*) as pending FROM orders WHERE status = 'pending'",
"condition": "pending > 50"
},
{
"source": "api",
"endpoint": "https://api.example.com/system-status",
"condition": "response.status === 'normal'"
}
],
"checkInterval": 600
}
}

手动触发器

用户界面触发器

允许用户通过UI手动启动工作流:

{
"type": "manual",
"config": {
"inputForm": {
"fields": [
{
"name": "reportType",
"label": "报表类型",
"type": "select",
"options": ["销售", "库存", "客户"]
},
{
"name": "dateRange",
"label": "日期范围",
"type": "daterange",
"required": true
}
]
},
"permissions": ["report_manager", "admin"]
}
}

批量操作触发器

处理批量选择的项目:

{
"type": "batchAction",
"config": {
"targetEntity": "orders",
"actionName": "批量处理订单",
"minItems": 1,
"maxItems": 100
}
}

高级配置

触发条件过滤

您可以为任何触发器添加额外的过滤条件:

{
"type": "webhook",
// ... 基本配置 ...
"filter": {
"conditions": [
{
"field": "payload.status",
"operator": "equals",
"value": "new"
},
{
"field": "payload.amount",
"operator": "greaterThan",
"value": 1000
}
],
"operator": "AND" // AND, OR
}
}

节流和速率限制

防止工作流执行过于频繁:

{
"type": "webhook",
// ... 基本配置 ...
"rateLimit": {
"maxExecutions": 10,
"perTimeWindow": 60, // 秒
"exceedingStrategy": "queue" // queue, discard, error
}
}

触发上下文和数据

每个触发器都会提供上下文数据,可在工作流中使用:

{
"trigger": {
"type": "webhook",
"id": "trig_12345",
"received_at": "2023-05-15T09:23:51Z"
},
"data": {
// 触发器特定数据
},
"metadata": {
"source_ip": "192.168.1.1",
"headers": {
"user-agent": "..."
}
}
}

触发器调试和监控

触发器日志

每个触发器事件都会生成日志条目,包括:

  • 接收时间
  • 匹配状态(是否通过过滤条件)
  • 工作流启动状态
  • 完整的输入数据

您可以在"工作流 > 触发器 > 日志"页面查看这些信息。

触发器测试

GeniSpace提供了测试工具来验证触发器配置:

  1. 配置触发器
  2. 点击"测试触发器"按钮
  3. 提供测试输入数据
  4. 查看触发器如何处理测试数据
  5. 检查是否会启动工作流(不会实际执行)

触发器监控

监控触发器的健康状态和性能:

  • 活动状态(活跃/禁用)
  • 最近触发时间
  • 触发频率
  • 错误率
  • 平均响应时间

安全性和访问控制

认证和授权

保护触发器端点的方法:

  1. API密钥 - 为WebHook和API触发器设置密钥
  2. IP白名单 - 限制可以调用触发器的IP地址
  3. OAuth2 - 使用OAuth2进行更高级的认证
  4. 共享密钥 - 在请求中包含签名进行验证

数据验证

验证输入数据以防止无效触发:

  1. JSON Schema - 定义输入数据的结构和类型
  2. 输入验证规则 - 设置自定义验证规则
  3. 内容安全策略 - 过滤潜在的恶意内容

常见问题

Details

触发器可以启动多个工作流吗? 是的,同一个触发事件可以启动多个不同的工作流。在触发器配置中,您可以链接多个工作流,或者创建多个监听相同事件的独立触发器。

Details

如何处理错过的触发事件? GeniSpace提供多种策略来处理错过的事件:

  1. 重放功能 - 对于某些触发器类型,可以在UI中重放历史事件
  2. 持久化队列 - 使用消息队列触发器确保消息不会丢失
  3. 补偿逻辑 - 在工作流中实现额外的数据检查以处理可能错过的事件
Details

如何处理重复触发? 为避免重复触发导致的问题,您可以:

  1. 启用去重 - 配置触发器在特定时间窗口内忽略重复事件
  2. 使用幂等操作 - 设计工作流使其可以安全地多次执行
  3. 实现事务ID - 使用唯一事务ID追踪已处理的事件

最佳实践

选择正确的触发器类型

  • 使用时间触发器进行定期任务,如报告生成和数据备份
  • 使用事件触发器响应实时事件,如新订单或用户注册
  • 使用条件触发器监控系统状态,如库存水平或错误率
  • 使用手动触发器进行需要人工决策的流程

优化触发频率

  • 避免过于频繁的检查,以减少系统负载
  • 为批处理任务设置合理的时间间隔
  • 使用队列合并多个相似事件
  • 考虑业务时间和用户活动模式

异常处理

  • 配置触发失败通知
  • 实现触发器健康检查
  • 设置备用触发机制
  • 记录和监控所有触发异常

下一步