订单处理
查看 Markdown向买家扣款,等卖家批准发货,然后发货。发货一直失败时,给买家退款。
SDK: Python · Go · TypeScript · Java · Rust
展示:提醒、Execute 失败恢复和等待 Step 完成。
Flow 设计
- Charge 向买家扣款,并把 order-status Attribute 设为
charged。 - Ship 等待 seller-ok Channel 上的卖家批准,或一个 24 小时的 Timer。Timer 先触发时,Ship 发一封提醒,然后再等一轮。批准之后,它发货,把 order-status 设为
shipped,并以输出shipped:<订单 ID>完成 Flow。 - Refund 在 Ship 的重试用完后运行。它给买家退款,把 order-status 设为
refunded,并以refunded:<订单 ID>完成 Flow。图中 Step 上的 ⚠ 表示一条失败路由;选中这个 Step 可以看到它通向哪里。
approve RPC 把批准消息发到 seller-ok。describe RPC 返回 order-status。
核心实现
扣款、发货和退款调用都带上用订单 ID 构造的幂等键,订单 ID 也就是 Flow ID:charge-<订单 ID>、ship-<订单 ID> 和 refund-<订单 ID>。被中断的 Step 可能带着同一个键再次运行,所以支付和发货服务可以丢弃重复的调用。见 幂等键。真实的提醒邮件也需要幂等键,因为被中断的 Step 可能再次运行。示例没有传。
每个 Step 的 Execute 有 3 秒的重试预算,让演示很快结束;代码注释建议生产环境用一小时。Ship 的失败路由转到 Refund,Refund 有自己的短重试。
Flow 通过 constructor 拿到服务(Flow 包含什么)。order-status 建了索引,所以可以按状态搜索 Flow。dex:group 和 dex:explanation 注释标注上图中的各个 Step。
order_status = Attribute(
"order-status",
str,
AttributeIndex(IndexType.KEYWORD, "keyword1"),
)
seller_ok = Channel[str]("seller-ok", str)
# dex:group group-id:payment group-label:"Payment"
# dex:explanation text:"Charge the customer for the order."
class Charge(Step[OrderRequest]):
def __init__(self, service: MyDependencyService) -> None:
self.service = service
def get_step_options(self) -> StepOptions:
def execute(self, context: Context, input: OrderRequest) -> StepDecision:
self.service.charge_user(
input.email,
input.customer_id,
input.amount,
idempotency_key=f"charge-{input.order_id}",
)
order_status.set(context, "charged")
return go_to(Ship, input)
# dex:group group-id:shipping group-label:"Shipping"
# dex:explanation text:"Wait for seller approval, remind after 24 hours, then ship the order."
class Ship(Step[OrderRequest]):
def __init__(self, service: MyDependencyService) -> None:
self.service = service
def get_step_options(self) -> StepOptions:
def wait_for(self, context: Context, input: OrderRequest) -> Wait:
return Wait.any_of(
seller_ok.for_one(),
Timer.by_duration(timedelta(hours=24)),
)
def execute(self, context: Context, input: OrderRequest) -> StepDecision:
if context.has_timer_fired():
self.service.send_email(
input.email,
"Reminder: approve shipment",
"Please approve or provide a tracking number.",
)
return go_to(Ship, input)
self.service.ship_item(
input.order_id,
input.test_fail_at_shipping,
idempotency_key=f"ship-{input.order_id}",
)
order_status.set(context, "shipped")
return graceful_complete(f"shipped:{input.order_id}")
# dex:group group-id:refund group-label:"Refund"
# dex:explanation text:"Refund the order when shipping keeps failing."
class Refund(Step[OrderRequest]):
def __init__(self, service: MyDependencyService) -> None:
self.service = service
def execute(self, context: Context, input: OrderRequest) -> StepDecision:
self.service.update_external_system(
f"refund {input.order_id}", idempotency_key=f"refund-{input.order_id}"
)
order_status.set(context, "refunded")
return graceful_complete(f"refunded:{input.order_id}")
class OrderProcessingFlow(Flow[OrderRequest]):
def __init__(self, service: MyDependencyService) -> None:
self.service = service
self.refund = Refund(service)
self.ship = Ship(service)
self.charge = Charge(service)
def get_steps(self) -> StepList[OrderRequest]:
return StepList.start_step(self.charge).other_steps(self.ship, self.refund)
def get_persistence_schema(self) -> PersistenceSchema:
return PersistenceSchema.of(order_status, seller_ok)
@rpc
def approve(self, context: Context, _note: str) -> RPCResult[str]:
seller_ok.publish(context, "approved")
return RPCResult("ok")
@rpc
def describe(self, context: Context) -> RPCResult[str]:
return RPCResult(order_status.get(context))
例子: examples/python/dex_examples/products/order-processing/order_processing_flow.py
type OrderProcessingFlow struct {
dex.FlowDefaults
service service.MyService
}
func NewOrderProcessingFlow(applicationService service.MyService) *OrderProcessingFlow {
return &OrderProcessingFlow{service: applicationService}
}
func (flow *OrderProcessingFlow) GetSteps() []dex.StepDef {
return []dex.StepDef{
dex.DefineStartStep(chargeStep{service: flow.service}),
dex.DefineStep(shipStep{service: flow.service}),
dex.DefineStep(refundStep{service: flow.service}),
}
}
func (flow *OrderProcessingFlow) GetRPCs() []dex.RPCDef {
return []dex.RPCDef{
dex.DefineRPC(flow.Approve, nil),
dex.DefineRPC(flow.Describe, nil),
}
}
func (*OrderProcessingFlow) GetPersistenceSchema() dex.PersistenceSchema {
return dex.PersistenceSchema{
Attributes: []dex.AttributeDef{OrderStatus},
Channels: []dex.ChannelDef{SellerOK},
}
}
type chargeStep struct {
dex.StepDefaultsNoWaitFor[OrderRequest]
service service.MyService
}
func (chargeStep) GetStepType() string {
return ChargeStepType
}
func (chargeStep) GetStepOptions() *dex.StepOptions {
}
func (step chargeStep) Execute(
ctx dex.Context,
order OrderRequest,
) (*dex.StepDecision, error) {
step.service.ChargeUserWithIdempotencyKey(order.Email, order.CustomerID, order.Amount, "charge-"+order.OrderID)
if err := OrderStatus.Set(ctx, "charged"); err != nil {
return nil, err
}
return dex.GoTo(shipStep{}, order), nil
}
type shipStep struct {
dex.DefaultStepType
service service.MyService
}
func (shipStep) GetStepType() string {
return ShipStepType
}
func (step shipStep) GetStepOptions() *dex.StepOptions {
}
func (shipStep) WaitFor(
dex.Context,
OrderRequest,
) (*dex.Wait, error) {
return dex.AnyOf(
SellerOK.ForOne(),
dex.Timer(24*time.Hour),
), nil
}
func (step shipStep) Execute(
ctx dex.Context,
order OrderRequest,
) (*dex.StepDecision, error) {
if ctx.HasTimerFired() {
step.service.SendEmail(
order.Email,
"Reminder: approve shipment",
"Please approve or provide a tracking number.",
)
return dex.GoTo(shipStep{}, order), nil
}
if err := step.service.ShipItemWithIdempotencyKey(order.OrderID, order.TestFailAtShipping, "ship-"+order.OrderID); err != nil {
return nil, err
}
if err := OrderStatus.Set(ctx, "shipped"); err != nil {
return nil, err
}
return dex.GracefulComplete("shipped:" + order.OrderID), nil
}
type refundStep struct {
dex.StepDefaultsNoWaitFor[OrderRequest]
service service.MyService
}
func (refundStep) GetStepType() string {
return RefundStepType
}
func (step refundStep) Execute(
ctx dex.Context,
order OrderRequest,
) (*dex.StepDecision, error) {
step.service.UpdateExternalSystemWithIdempotencyKey("refund "+order.OrderID, "refund-"+order.OrderID)
if err := OrderStatus.Set(ctx, "refunded"); err != nil {
return nil, err
}
return dex.GracefulComplete("refunded:" + order.OrderID), nil
}
var (
_ dex.Flow = (*OrderProcessingFlow)(nil)
_ dex.RPC[string, string] = (*OrderProcessingFlow)(nil).Approve
_ dex.RPC[dex.None, string] = (*OrderProcessingFlow)(nil).Describe
)
public class OrderProcessingFlow implements Flow<OrderRequest> {
public static final Attribute<String> orderStatus = Attribute.define(
"order-status",
String.class,
new AttributeIndex(AttributeIndex.Type.KEYWORD, "keyword1"));
public static final Channel<String> sellerOk = Channel.define("seller-ok", String.class);
private final MyDependencyService service;
private final Charge charge;
private final Ship ship;
private final Refund refund;
public OrderProcessingFlow(final MyDependencyService service) {
this.service = service;
this.refund = new Refund(service);
this.ship = new Ship(service);
this.charge = new Charge(service);
}
@Override
public StepList<OrderRequest> getSteps() {
return StepList.startStep(charge).otherSteps(ship, refund);
}
@Override
public PersistenceSchema getPersistenceSchema() {
return PersistenceSchema.of(orderStatus, sellerOk);
}
@RPC
public RPCResult<String> approve(final Context context, final String note) {
sellerOk.publish(context, "approved");
return RPCResult.of("ok");
}
@RPC
public RPCResult<String> describe(final Context context) {
return RPCResult.of(orderStatus.get(context));
}
static final class Charge implements Step<OrderRequest> {
private final MyDependencyService service;
Charge(final MyDependencyService service) {
this.service = service;
}
@Override
public String getStepType() {
return "Charge";
}
@Override
public Class<OrderRequest> getInputType() {
return OrderRequest.class;
}
@Override
public StepOptions getStepOptions() {
}
@Override
public StepDecision execute(final Context context, final OrderRequest order) {
service.chargeUser(order.email, order.customerId, order.amount, "charge-" + order.orderId);
orderStatus.set(context, "charged");
return StepDecision.goTo(Ship.class, order);
}
}
static final class Ship implements Step<OrderRequest> {
private final MyDependencyService service;
Ship(final MyDependencyService service) {
this.service = service;
}
@Override
public String getStepType() {
return "Ship";
}
@Override
public Class<OrderRequest> getInputType() {
return OrderRequest.class;
}
@Override
public StepOptions getStepOptions() {
}
@Override
public Wait waitFor(final Context context, final OrderRequest order) {
return Wait.anyOf(
sellerOk.forOne(),
Timer.byDuration(Duration.ofHours(24)));
}
@Override
public StepDecision execute(final Context context, final OrderRequest order) {
if (context.hasTimerFired()) {
service.sendEmail(
order.email,
"Reminder: approve shipment",
"Please approve or provide a tracking number.");
return StepDecision.goTo(Ship.class, order);
}
service.shipItem(order.orderId, order.testFailAtShipping, "ship-" + order.orderId);
orderStatus.set(context, "shipped");
return StepDecision.gracefulComplete("shipped:" + order.orderId);
}
}
static final class Refund implements Step<OrderRequest> {
private final MyDependencyService service;
Refund(final MyDependencyService service) {
this.service = service;
}
@Override
public String getStepType() {
return "Refund";
}
@Override
public Class<OrderRequest> getInputType() {
return OrderRequest.class;
}
@Override
public StepDecision execute(final Context context, final OrderRequest order) {
service.updateExternalSystem("refund " + order.orderId, "refund-" + order.orderId);
orderStatus.set(context, "refunded");
return StepDecision.gracefulComplete("refunded:" + order.orderId);
}
}
}
例子: examples/java/src/main/java/io/superdurable/dex/products/orderprocessing/OrderProcessingFlow.java
const orderStatus = new Attribute("order-status", stringCodec, {
type: IndexType.KEYWORD,
indexKey: "keyword1",
});
const sellerOk = new Channel("seller-ok", stringCodec);
export class OrderProcessingFlow implements Flow<OrderRequest> {
public readonly charge: Charge;
public readonly ship: Ship;
public readonly refund: Refund;
public constructor(public readonly service: MyDependencyService) {
this.refund = new Refund(service);
this.ship = new Ship(service);
this.charge = new Charge(service);
}
public getFlowType(): string {
return "OrderProcessingFlow";
}
public getSteps() {
return StepList.startStep(this.charge).otherSteps(this.ship, this.refund);
}
public getPersistenceSchema(): PersistenceSchema {
return {
attributes: [orderStatus],
channels: [sellerOk],
};
}
@rpc({ inputCodec: stringCodec, outputCodec: stringCodec })
public approve(context: Context, _note: string): RPCResult<string> {
sellerOk.publish(context, "approved");
return { output: "ok" };
}
@rpc({ outputCodec: stringCodec })
public describe(context: Context): RPCResult<string> {
return { output: orderStatus.get(context) };
}
}
class Charge implements Step<OrderRequest> {
public readonly inputCodec = orderRequestCodec;
public constructor(private readonly service: MyDependencyService) {}
public getStepType(): string {
return "Charge";
}
public getStepOptions() {
}
public execute(context: Context, input: OrderRequest): StepDecision {
this.service.chargeUser(input.email, input.customerId, input.amount, `charge-${input.orderId}`);
orderStatus.set(context, "charged");
return goTo(Ship, input);
}
}
class Ship implements Step<OrderRequest> {
public readonly inputCodec = orderRequestCodec;
public constructor(private readonly service: MyDependencyService) {}
public getStepType(): string {
return "Ship";
}
public getStepOptions() {
}
public waitFor(_context: Context, _input: OrderRequest): Wait {
return Wait.anyOf(sellerOk.forOne(), Timer.byDuration(DAY_MS));
}
public execute(context: Context, input: OrderRequest): StepDecision {
if (context.hasTimerFired()) {
this.service.sendEmail(
input.email,
"Reminder: approve shipment",
"Please approve or provide a tracking number.",
);
return goTo(Ship, input);
}
this.service.shipItem(input.orderId, input.testFailAtShipping, `ship-${input.orderId}`);
orderStatus.set(context, "shipped");
return gracefulComplete(`shipped:${input.orderId}`);
}
}
class Refund implements Step<OrderRequest> {
public readonly inputCodec = orderRequestCodec;
public constructor(private readonly service: MyDependencyService) {}
public getStepType(): string {
return "Refund";
}
public execute(context: Context, input: OrderRequest): StepDecision {
this.service.updateExternalSystem(`refund ${input.orderId}`, `refund-${input.orderId}`);
orderStatus.set(context, "refunded");
return gracefulComplete(`refunded:${input.orderId}`);
}
}
例子: examples/typescript/src/products/order-processing/order-processing-flow.ts
#[derive(Clone)]
pub struct OrderProcessingFlow {
charge: Charge,
ship: Ship,
refund: Refund,
}
impl OrderProcessingFlow {
pub fn new(service: MyDependencyService) -> Self {
Self {
charge: Charge {
service: service.clone(),
},
ship: Ship {
service: service.clone(),
},
refund: Refund { service },
}
}
fn approve(&self, context: &mut Context, _note: String) -> HandlerResult<RpcResult<String>> {
SELLER_OK.publish(context, "approved".to_string())?;
Ok(RpcResult::new("ok".to_string()))
}
fn describe(&self, context: &mut Context) -> HandlerResult<RpcResult<String>> {
Ok(RpcResult::new(ORDER_STATUS.get_required(context)?))
}
}
impl Default for OrderProcessingFlow {
fn default() -> Self {
Self::new(MyDependencyService::default())
}
}
impl Flow for OrderProcessingFlow {
type StartInput = OrderRequest;
fn flow_type(&self) -> &'static str {
"OrderProcessingFlow"
}
fn steps(&self) -> StepList<'_, Self::StartInput> {
StepList::start(&self.charge)
.and(&self.ship)
.and(&self.refund)
}
fn persistence(&self) -> PersistenceSchema {
PersistenceSchema::new()
.attribute(&ORDER_STATUS)
.channel(&SELLER_OK)
}
fn rpcs(&self) -> RpcList<Self> {
RpcList::new()
.function(APPROVE_ORDER, Self::approve)
.function_without_input(DESCRIBE_ORDER, Self::describe)
}
}
#[derive(Clone, Default)]
pub struct Charge {
service: MyDependencyService,
}
impl Step for Charge {
type Input = OrderRequest;
fn step_type(&self) -> &'static str {
"ChargeStep"
}
fn options(&self) -> StepOptions<Self::Input> {
}
fn execute(&self, context: &mut Context, input: Self::Input) -> HandlerResult<StepDecision> {
self.service.charge_user_with_idempotency_key(
&input.email,
&input.customer_id,
input.amount,
&format!("charge-{}", input.order_id),
);
context.record_event("charge", input.order_id.clone())?;
ORDER_STATUS.set(context, "charged".to_string())?;
Ok(StepDecision::go_to(&Ship::default(), input))
}
}
#[derive(Clone, Default)]
pub struct Ship {
service: MyDependencyService,
}
impl Step for Ship {
type Input = OrderRequest;
fn step_type(&self) -> &'static str {
"ShipStep"
}
fn options(&self) -> StepOptions<Self::Input> {
}
fn wait_for(&self, _context: &mut Context, _input: Self::Input) -> HandlerResult<Wait> {
Ok(Wait::any_of([
SELLER_OK.for_one(),
Timer::by_duration(Duration::from_secs(24 * 60 * 60)),
]))
}
fn execute(&self, context: &mut Context, input: Self::Input) -> HandlerResult<StepDecision> {
if context.has_any_timer_fired() {
self.service.send_email(
&input.email,
"Reminder: approve shipment",
"Please approve or provide a tracking number.",
);
context.record_event("shipment-reminder", input.order_id.clone())?;
return Ok(StepDecision::go_to(&Ship::default(), input));
}
self.service.ship_item_with_idempotency_key(
&input.order_id,
input.test_fail_at_shipping,
&format!("ship-{}", input.order_id),
)?;
context.record_event("ship", input.order_id.clone())?;
ORDER_STATUS.set(context, "shipped".to_string())?;
Ok(StepDecision::graceful_complete(format!(
"shipped:{}",
input.order_id
)))
}
}
#[derive(Clone, Default)]
pub struct Refund {
service: MyDependencyService,
}
impl Step for Refund {
type Input = OrderRequest;
fn step_type(&self) -> &'static str {
"RefundStep"
}
fn options(&self) -> StepOptions<Self::Input> {
}
fn execute(&self, context: &mut Context, input: Self::Input) -> HandlerResult<StepDecision> {
self.service.update_external_system_with_idempotency_key(
&format!("refund {}", input.order_id),
&format!("refund-{}", input.order_id),
);
context.record_event("refund", input.order_id.clone())?;
ORDER_STATUS.set(context, "refunded".to_string())?;
Ok(StepDecision::graceful_complete(format!(
"refunded:{}",
input.order_id
)))
}
}
static ORDER_STATUS: LazyLock<Attribute<String>> = LazyLock::new(|| {
Attribute::new("order-status").indexed(AttributeIndex::keyword().with_key("keyword1"))
});
static SELLER_OK: LazyLock<Channel<String>> = LazyLock::new(|| Channel::new("seller-ok"));
HTTP 处理器
示例应用在同一个进程里运行 Worker 和这些 handler。start 启动 Flow,用 Client 的 wait-for-Step-completion API 等 Charge 完成后才返回。approve 和 describe 调用同名的 RPC。
@blueprint.get("/start")
async def start() -> Response:
flow_id = new_flow_id("order-processing")
request = OrderRequest(
flow_id,
"buyer@example.com",
"customer-1",
42,
optional_query("testFailAtShipping", "") == "true",
)
run_id = await app_state.client.start_flow(
app_state.order_processing,
flow_id,
request,
start_options(),
)
await app_state.client.wait_for_step_completion(
flow_id,
CHARGE_STEP,
WaitForStepCompletionOptions(),
)
return started_flow(flow_id, run_id)
@blueprint.get("/approve")
async def approve() -> Response:
output = await app_state.client.invoke_rpc(
app_state.order_processing.approve,
_required_flow_id(),
optional_query("notes", ""),
)
return jsonify(output)
@blueprint.get("/describe")
async def describe() -> Response:
flow_id = _required_flow_id()
status = await app_state.client.invoke_rpc(
app_state.order_processing.describe,
flow_id,
)
return jsonify({"flowID": flow_id, "status": status})
return blueprint
def _required_flow_id() -> str:
"""Reads the Flow ID from flowId, or from its alias workflowId."""
return optional_query("workflowId", "") or required_query("flowId")
例子: examples/python/dex_examples/products/order-processing/controller.py
func RegisterRoutes(router gin.IRouter, client *sdk.Client, flow *OrderProcessingFlow) {
controller := &controller{client: client, flow: flow}
group := router.Group("/products/order-processing")
group.GET("/start", controller.start)
group.GET("/approve", controller.approve)
group.GET("/describe", controller.describe)
}
func (controller *controller) start(request *gin.Context) {
flowID := httputil.NewFlowID("order-processing")
input := OrderRequest{
OrderID: flowID,
Email: "buyer@example.com",
CustomerID: "customer-1",
Amount: 42,
TestFailAtShipping: request.Query("testFailAtShipping") == "true",
}
runID, err := controller.client.StartFlow(
request.Request.Context(),
controller.flow,
flowID,
input,
sdk.StartFlowOptions{},
)
if err != nil {
httputil.Respond(request, nil, err)
return
}
waitContext, cancelWait := context.WithTimeout(request.Request.Context(), 5*time.Minute)
defer cancelWait()
@RestController
@RequestMapping("/products/order-processing")
public class OrderProcessingController {
private final Client client;
private final OrderProcessingFlow flow;
public OrderProcessingController(
final Client client,
final OrderProcessingFlow flow) {
this.client = client;
this.flow = flow;
}
@GetMapping("/start")
public ResponseEntity<Map<String, String>> start(
@RequestParam(defaultValue = "false") final boolean testFailAtShipping) {
final String flowId = "order-processing-" + System.nanoTime();
final OrderRequest request = new OrderRequest(
flowId,
"buyer@example.com",
"customer-1",
42,
testFailAtShipping);
final String runId = client.startFlow(flow, flowId, request, ExampleFlows.startOptions());
client.waitForStepCompletion(
flowId,
StepExecutionId.of("Charge"),
WaitForStepCompletionOptions.newBuilder().build());
final Map<String, String> response = new LinkedHashMap<String, String>();
response.put("flowID", flowId);
export function createOrderProcessingRouter(
client: Client,
flow: OrderProcessingFlow,
): Router {
const router = Router();
router.get("/start", async (request, response) => {
const flowId = `order-processing-${process.hrtime.bigint()}`;
const input: OrderRequest = {
orderId: flowId,
email: "buyer@example.com",
customerId: "customer-1",
amount: 42,
testFailAtShipping: String(request.query.testFailAtShipping ?? "") === "true",
};
const runId = await client.startFlow(flow, flowId, input, startOptions());
await client.waitForStepCompletion(
flowId,
StepExecutionId.of("Charge"),
{},
);
response.json({ flowID: flowId, runID: runId });
});
router.get("/approve", async (request, response) => {
const flowId = flowIdFromQuery(request.query);
const notes = String(request.query.notes ?? "");
const output = await client.invokeRPC(flow.approve, flowId, notes);
response.json(output);
});
router.get("/describe", async (request, response) => {
const flowId = flowIdFromQuery(request.query);
const status = await client.invokeRPC(flow.describe, flowId);
response.json({ flowID: flowId, status });
});
return router;
}
/** Reads the Flow ID from flowId, or from its alias workflowId. */
function flowIdFromQuery(query: Record<string, unknown>): string {
return String(query.workflowId ?? query.flowId ?? "");
}
例子: examples/typescript/src/products/order-processing/controller.ts
pub fn mount(client: SharedClient, flow: OrderProcessingFlow) -> Router {
Router::new()
.route("/products/order-processing/start", get(start))
.route("/products/order-processing/approve", get(approve))
.route("/products/order-processing/describe", get(describe))
.with_state(OrderProcessingState { client, flow })
}
async fn start(
State(state): State<OrderProcessingState>,
Query(query): Query<StartQuery>,
) -> impl IntoResponse {
let OrderProcessingState { client, flow } = state;
match run_blocking(move || {
let flow_id = new_flow_id("order-processing");
let input = OrderRequest {
order_id: flow_id.clone(),
email: "buyer@example.com".into(),
customer_id: "customer-1".into(),
amount: 42,
test_fail_at_shipping: query.test_fail_at_shipping,
};
let run_id = client.start_flow(&flow, &flow_id, input)?;
client.wait_for_step_completion(
&flow_id,
StepExecutionId::of(&Charge::default()),
WaitForStepCompletionOptions::new(),
)?;
Ok(StartResponse { flow_id, run_id })
}) {
例子: examples/rust/src/products/order_processing/controller.rs
运行
按在 playground 里运行一个示例启动一种语言的示例。然后创建一个订单:
curl -s 'http://127.0.0.1:8080/products/order-processing/start'
从响应里复制 flowID。在 Dex Web(dexcli dev 打印的地址,例如 http://127.0.0.1:8802)里,这条 Flow 停在 Ship 等待。把 FLOW_ID 换成它,批准发货:
curl -s "http://127.0.0.1:8080/products/order-processing/approve?flowId=FLOW_ID"
这个调用把批准消息发到 seller-ok。随后 Ship 发货,Flow 完成;可以在 Dex Web 里看到这个过程。也可以在 examples playground 里选择 Order processing,做同样的调用。
试试退款路径
创建一个发货总会失败的订单:
curl -s 'http://127.0.0.1:8080/products/order-processing/start?testFailAtShipping=true'
复制新的 flowID,像上面一样批准它。随后 Ship 一直失败,直到 3 秒的重试预算用完,然后 Refund 运行。在 Dex Web 里等到 Refund 完成,或者重复下面的调用,直到状态变成 refunded:
curl -s "http://127.0.0.1:8080/products/order-processing/describe?flowId=FLOW_ID"
在 Dex Web 的 Events 下,Graph 显示 Charge、Ship,然后是 Refund。
在 Dex Web 中查看运行
打开一次已批准的运行,选择 Events。Timeline 按顺序显示扣款、批准和发货;选中一个事件可以读取它的输入和输出。Graph 显示各 Step 如何衔接。

