FCQRS 发布 .NET 工作流库 FCQRS.Workflows,崩溃后可从断点恢复执行
来源标题:FCQRS.Workflows for .NET: durable code that resumes where it stopped
FCQRS 推出 FCQRS.Workflows 包,让 .NET 工作流在崩溃后从已完成的步骤继续执行,已保存结果的步骤不会重跑。该工作流会存储每一步的结果,与现有的 saga 并存而非替代它们。官方还提供了逐步构建工作流的教程。
以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。
工作流接收输入、调用外部系统、等待,然后返回结果。FCQRS 会把每一步的结果存下来。崩溃之后,工作流实例会从它已经走到的那一步继续,结果已经存下的步骤不会重跑。
工作流位于 FCQRS.Workflows 包中。它们和 saga 并存,并不取代 saga。工作流教程会一步步构建一个工作流;本页解释它背后的模型。
enrich 用外部服务给订单打分,等待五分钟,然后读取时间:
module Account =
open FCQRS.Common
// 调用方或转账 saga 可以要求账户执行的操作。
type AccountCommand =
| Open of owner: string
| Deposit of amount: decimal
| Withdraw of amount: decimal
| SendTransfer of transferId: string * target: string * amount: decimal
| ReceiveTransfer of transferId: string * source: string * amount: decimal
| RefundTransfer of transferId: string * target: string * amount: decimal
// 账户的回复。Rejected 只是一种回复:它永远不会被存储。
type AccountEvent =
| Opened of owner: string
| Deposited of amount: decimal
| Withdrawn of amount: decimal
| TransferSent of transferId: string * target: string * amount: decimal
| TransferReceived of transferId: string * source: string * amount: decimal
| TransferRefunded of transferId: string * target: string * amount: decimal
| Rejected of reason: string
// 账户目前知道的状态。这些集合保存它处理过的转账 ID,
// 因此重复的转账命令不会重复动钱。
type AccountState = {
Owner: string option
Balance: decimal
Sent: Set<string>
Received: Set<string>
Refunded: Set<string>
}
// 账户第一个事件之前的状态。
let initial = {
Owner = None
Balance = 0m
Sent = Set.empty
Received = Set.empty
Refunded = Set.empty
}
// 根据当前状态,决定如何处理一条命令。
let decide (command: Command<AccountCommand>) (state: AccountState) =
match command.CommandDetails, state.Owner with
| Open _, Some _ -> DeferEvent(Rejected "The account is already open")
| Open owner, None -> PersistEvent(Opened owner)
| _, None -> DeferEvent(Rejected "The account is not open")
| (Deposit amount | Withdraw amount | SendTransfer(_, _, amount)), _
when amount <= 0m -> DeferEvent(Rejected "The amount must be positive")
| Deposit amount, _ -> PersistEvent(Deposited amount)
| (Withdraw amount | SendTransfer(_, _, amount)), _
when amount > state.Balance ->
DeferEvent(Rejected $"Insufficient funds: {state.Balance} available")
| Withdraw amount, _ -> PersistEvent(Withdrawn amount)
| SendTransfer(id, _, _), _ when state.Sent.Contains id ->
DeferEvent(Rejected $"Transfer {id} was already sent")
| SendTransfer(id, target, amount), _ ->
PersistEvent(TransferSent(id, target, amount))
// 重复投递会得到第一次的答案;不会动钱。
| ReceiveTransfer(id, source, amount), _ when state.Received.Contains id ->
DeferEvent(TransferReceived(id, source, amount))
| ReceiveTransfer(id, source, amount), _ ->
PersistEvent(TransferReceived(id, source, amount))
| RefundTransfer(id, target, amount), _ when state.Refunded.Contains id ->
DeferEvent(TransferRefunded(id, target, amount))
| RefundTransfer(id, target, amount), _ ->
PersistEvent(TransferRefunded(id, target, amount))
// 应用一个事件。拒绝或重复的回复不会改变任何东西。
let fold (event: Event<AccountEvent>) (state: AccountState) =
match event.EventDetails with
| Opened owner -> { state with Owner = Some owner }
| Deposited amount -> { state with Balance = state.Balance + amount }
| Withdrawn amount -> { state with Balance = state.Balance - amount }
| TransferSent(id, _, amount) ->
{ state with
Balance = state.Balance - amount
Sent = state.Sent.Add id }
// FCQRS 也会折叠重复的回复;已知的 ID 不会改变任何东西。
| TransferReceived(id, _, _) when state.Received.Contains id -> state
| TransferReceived(id, _, amount) ->
{ state with
Balance = state.Balance + amount
Received = state.Received.Add id }
| TransferRefunded(id, _, _) when state.Refunded.Contains id -> state
| TransferRefunded(id, _, amount) ->
{ state with
Balance = state.Balance + amount
Refunded = state.Refunded.Add id }
| Rejected _ -> state
open System
open System.Threading.Tasks
open FCQRS.Common
open FCQRS.FSharp
open FCQRS.Workflows
open Account
let score (key: string) (order: string) : Task<int> = Task.FromResult 42
let charge (key: string) (amount: decimal) : Task<string> = Task.FromResult "paid"
let isShipped (key: string) (order: string) : Task<bool> = Task.FromResult true
let sendReminder (key: string) (text: string) : Task<string> = Task.FromResult "sent"
type ReservationCommand = Reserve of id: string
type ReservationEvent =
| Reserved of id: string
| Confirmed of id: string
FCQRS
FCQRS.Common 包含 Event 和 Command 这类通用类型
写侧(Write Side)的功能。
CLI 类型 的缩写。
基本类型
CLI 类型 的缩写。
基本类型
可选值的类型。在其他 CLI 语言中使用时,空选项就是 null 值。
使用构造函数 Some 和 None 来创建此类型的值。使用 Option 模块中的值来操作此类型的值,或直接对值进行模式匹配。对其他 CLI 语言来说,None 值会显示为 null 值。由于使用 null 作为值表示,此类型的实例方法对其他 CLI 语言会显示为静态方法。
Options
基于二叉树的不可变集合,其中元素按 F# 泛型比较排序。默认比较使用 F# 结构化比较函数,或使用元素值上的 IComparable 接口实现。
有关集合的更多操作,请参见相应模块。此类的所有成员都是线程安全的,可以从多个线程并发使用。
包含用于操作类型 的值的操作。
类型 'T 的空集合。
(原文较长,此处译出前 6000 字,完整内容请点「打开原文」。)
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。