跳到主要内容

工作流 DSL

查看 Markdown

在运行时把工作流定义为数据,通常用 JSON 表示。保存这些定义,再用同一个解释器 Flow 运行它们。 当所有工作流都由同一组动作和外部条件组成时,使用这个模式。

操作人员、客户、合作伙伴或其他系统都可以定义工作流。定义遵循可序列化的 schema,由应用自己的定义库保存并管理版本。用户可以编辑、复制、导入和导出这些文档。新增工作流只需添加一份 JSON 文档,无需改代码或发布新版本。

例如,卖家可以定义两种购买流程。一种在买家确认后扣款,另一种先寄样品。两种定义都由同一个解释器运行。

工作方式

  1. 保存并校验定义。 在应用边界校验定义,包括保存时和启动 Flow 前。检查引用的状态、动作和条件名称是否存在,并确保每个条件名称只属于一个等待。发现无效定义时,在启动 Flow 前拒绝。
  2. 启动购买流程。 起始 Step 把完整定义保存到一个 Attribute,并初始化共享状态数据映射。每次运行只在一个位置推进。启动之后,每个 Step 的输入只携带当前位置,例如状态名称和动作索引。
  3. 等待买家。 如果状态有前置条件,就先等条件满足,再进入该状态。RPC 根据定义校验条件名称,然后将消息发布到一个以条件名称为键的 ChannelMap。持久化 schema 在代码中声明这个映射,定义在运行时提供键。每次等待取一条消息。解释器把消息合并到共享状态数据映射中。
  4. 执行一个动作。 将扣款或交付的处理函数或脚本放在一个 Step 的 Execute 中运行。脚本和其他外部调用一样,需要重试和幂等键。脚本可能阻塞且观察不到取消时,还要给它设置超时。把动作结果合并到共享映射。每个动作有自己的检查点,重试时已经完成的动作保持原样。
  5. 选择下一步。 转换 Step 可以等待另一个条件,并将收到的消息合并到同一个映射。它读取映射来选择下一个状态。解释器用 GoTo 在这四种 Step 之间推进,直到购买流程到达没有后续转换的状态。
  6. 记录进度。 定义决定进度如何表示,例如当前状态、当前步骤或自定义阶段。将进度写入 Attribute,供 Client 读取。需要搜索进度时,使用 Attribute 同步。Flow 图展示解释器的 Step。Attribute 展示某笔购买在其工作流中的位置。

示例

等待 Step 以条件名称为键,从 ChannelMap 取一条消息,并将消息合并到共享状态数据。动作 Step 每次只执行一个动作,然后进入下一个动作或状态转换。每个标签页都链接到完整的可运行示例。

deal_dsl_flow.pyPython
class WaitForDealCondition(Step[StateStepInput]):
def __init__(self, flow: "DealDSLFlow") -> None:
self.flow = flow

def wait_for(self, context: Context, input: StateStepInput) -> Wait:
state = self.flow.definition.get(context).state(input.state_name)
if state.pre_condition is None:
return Wait.skip_immediately()
self.flow.pending_condition.set(context, state.pre_condition.name)
return Wait.until(self.flow.condition_messages.for_one(state.pre_condition.name))

def execute(self, context: Context, input: StateStepInput) -> StepDecision:
state = self.flow.definition.get(context).state(input.state_name)
if state.pre_condition is not None:
self.flow.merge_condition(context, state.pre_condition.name)
self.flow.pending_condition.delete(context)
self.flow.current_state.set(context, state.name)
if state.actions:
return go_to(ExecuteDealAction, ActionStepInput(state.name, 0))
return go_to(EvaluateDealTransition, input)


# dex:group group-id:actions group-label:"Actions"
# dex:explanation text:"Run one action of the current state, then the next action or the transition."
class ExecuteDealAction(Step[ActionStepInput]):
def __init__(self, flow: "DealDSLFlow") -> None:
self.flow = flow

def execute(self, context: Context, input: ActionStepInput) -> StepDecision:
state = self.flow.definition.get(context).state(input.state_name)
if input.action_index < 0 or input.action_index >= len(state.actions):
raise ValueError(f"invalid action index {input.action_index}")
# The execution ID stays stable on retries and changes when the same action runs again.
idempotency_key = f"{context.flow_id}/{context.step_execution_id}"
self.flow.execute_action(
context, state.actions[input.action_index], idempotency_key
)
next_index = input.action_index + 1
if next_index < len(state.actions):
return go_to(ExecuteDealAction, ActionStepInput(state.name, next_index))
return go_to(EvaluateDealTransition, StateStepInput(state.name))

例子: examples/python/dex_examples/products/deal_dsl/deal_dsl_flow.py

定义版本

方式每次运行使用的定义编辑的影响
启动时保存快照起始 Step 将完整定义存入一个 Attribute。后续 Step 读取这份副本。Deal DSL 采用这种方式。新版本适用于新启动的 Flow。运行中的 Flow 保留原来的定义。
读取最新定义每次进入状态或计算转换时,在 Execute 中从定义库读取。读取属于外部调用,可能失败并重试。编辑会影响运行中的 Flow。状态可能改名或被删除,正在等待的条件也可能消失。为不同版本制定兼容规则,或在版本间迁移已保存的位置。

并行分支

如果一个状态可以开启多个分支,用 GoToMany 启动它们。每个分支在自己的 Step 输入中保存位置,最后在 Join 汇合。并发写入共享状态时使用 Attribute 锁。也可以让各分支分别写入自己的数据,在 Join 之后合并。参见 并行 Step和临界区。

下一步