Flow 基础
Flow 是你向 Dex 注册的顶层组件。
它定义在后台运行的 Step、持久化状态的 Attribute 与 Channel,以及提供 durable 通信的 RPC。
实现一个 Flow
你通过实现 Flow interface 定义一个 Flow。
大多数 SDK 会从 struct 或 class 名推导 FlowType,除非你显式覆盖。
一个 Flow 实现主要包含:
- Steps — start Step 与 Flow 运行的其它 Step。
- Persistence schema — Flow 读写的 Attribute 与 Channel。
- RPC handlers — Flow 可能调用的方法。
status = Attribute("status", str)
notify = Channel("notify", None)
class ExampleFlow(Flow[int]):
def __init__(self) -> None:
self.finish = FinishStep()
self.example = ExampleStep(self.finish)
def get_steps(self) -> StepList[int]:
return StepList.start_step(self.example).other_steps(self.finish)
def get_persistence_schema(self) -> PersistenceSchema:
return PersistenceSchema.of(status, notify)
@rpc
def describe(self, context: Context) -> RPCResult[str]:
return RPCResult.of(status.get(context))
例子: examples/python/dex_examples/primitives/flow/example_flow.py
标识符
Dex 在其 API 和文档中使用这些标识符。
首先,说到 Flow 时,它通常依上下文指由 FlowType 标识的 Flow 定义,或由 Flow ID 标识的一次 Flow execution。
| 标识符 | 谁设置 | 含义 |
|---|---|---|
| FlowType | 应用(Flow 实现) | Dex 路由 Worker 调用与搜索结果使用的稳定类型名。 |
| Flow ID | 应用在 start 时 | 一个 Flow execution 的业务身份。ID reuse policy 控制是否可以使用同一个 Flow ID 启动另一个 Flow execution。 |
| Run ID | Dex Server | 一次 backend run attempt 的身份。Time travel 与 continue-as-new 会保留同一个 Flow execution 并分配新的 Run ID。 |
| StepType | 应用(Step 实现) | Flow 内 Step 定义的 stable 名称。 |
| StepExecutionID | Dex Server | 某个 Step type 的一次执行实例,格式为 StepType-Number。 |
Steps
GetSteps / get_steps / steps 注册一个 start Step,以及通过 StepDecision movement 可能到达的其它 Step type。start Step 的 input type 也是 Flow 的 start input type。
Step 列表为空,或没有 start Step,在 Flow 完全由 RPC 驱动、稍后再进入 Step 时是合法的。
WaitFor、Execute 与默认 Step options 见 Step 基础。
Persistence schema
Persistence schema 列出 Step 或 RPC 使用的每个 Attribute 与 Channel。未声明的名称会被 Dex 拒绝读写。
RPC handlers
Dex 按方法名(Rust 中可显式命名)注册 RPC 方法。客户端用 InvokeRPC 调用;Dex 随后将调用派发到 Worker。