Skip to main content

Module process

Module process 

Source
Expand description

Business processes (design/processes.md): a flow across commands, events and queries, defined in code next to the domain, built into a model the kernel runs.

crate::Domain::process returns a Process; its methods add the steps in the order written, each returning the process, so they chain:

use kernable::{command, domain, event, query};
use kernable::process::{Ask, Decide, Send, Start, Wait, value};

#[event("shop")]
pub struct OrderSubmitted { pub total: i64 }
#[event("shop")]
pub struct OrderApproved {}
#[event("payments")]
pub struct PaymentReceived { pub order_id: String, pub at: i64 }
#[command("shop", aggregate = "order")]
pub struct Confirm {}
#[command("shop", aggregate = "order")]
pub struct Ship {}
#[command("shop", aggregate = "order")]
pub struct NotifyDelay {}
#[command("shop", aggregate = "order")]
pub struct Cancel { pub reason: String }
#[query("shop", answer = bool)]
pub struct NeedsApproval { pub order_id: String }

let shop = domain("shop");
let fulfilment = shop
    .process("fulfilment")
    .start::<OrderSubmitted>(Start::key("aggregate_id").data([("order_id", "aggregate_id"), ("total", "data.total")]))
    .command::<Confirm>("confirm", Send::aggregate("case.order_id"))
    .decide("large", Decide::when("case.total > 1000", "approval").otherwise("paid"))
    .query::<NeedsApproval>("approval", Ask::data([("order_id", "case.order_id")]).path(true, "approved").path(false, "paid"))
    .wait::<OrderApproved>("approved", Wait::key("aggregate_id"))
    .wait::<PaymentReceived>("paid", Wait::key("data.order_id").data([("paid_at", "data.at")]).timeout("3d", "cancel"))
    .command::<Ship>("ship", Send::aggregate("case.order_id").rejected("out_of_stock", "notify").next("done"))
    .command::<NotifyDelay>("notify", Send::aggregate("case.order_id").next("done"))
    .command::<Cancel>("cancel", Send::aggregate("case.order_id").data([("reason", value("unpaid"))]))
    .end("done");
assert_eq!(fulfilment.model().unwrap()["name"], "shop.fulfilment");
  • Steps run in the order written, unless a step names next; a decide without otherwise goes on with the next step written. Step names are required (letters, digits, _, -): they are the stable IDs of versions, the UI and the statistics.
  • References are strings: aggregate_id and data.<field> (the event being handled, in start and wait), case.<field> (the case’s data) and, in a query step, answer or answer.<field>. Field names are written in Rust’s style (case.order_id); the model has the wire names (case.orderId), as the log does. A literal is value.
  • Conditions (decide) are small expressions: references to case.<field>, literals, == != < <= > >=, and, or, not. The kernel parses them when the process registers.
  • Durations are 30s, 15m, 12h, 3d.
  • Messages are types (command::<Confirm>), or names (command_named("confirm", "order.Confirm", ...), unqualified: of the process’s domain).

An app registers its domains’ processes (crate::Kernable::app); crate::Kernable::register_process registers one alone. Import the option types from this module: Send and Wait share their names with std::marker::Send and crate::Wait.

Structs§

Ask
A query step: asks a read model; the answer picks the path. Ask::data([("order_id", "case.order_id")]).path(true, "approved").path(false, "paid").
Decide
A decision on the case’s data: the first condition that holds picks the path. Decide::when("case.total > 1000", "approval").or_when("case.total > 100", "check").otherwise("paid").
Process
A business process of a domain, made with crate::Domain::process: its start and steps, added in the order written (see the module for an example). Cloning gives another handle to the same process. Process::model builds the model the kernel registers.
Registered
Registered by crate::Kernable::register_process: the process’s version.
Send
A command step: the aggregate it goes to, its data, the paths for rejections, and the next step. Send::aggregate("case.order_id").rejected("out_of_stock", "notify").next("done").
Source
What a mapping’s field gets: a reference (a string, e.g. "case.order_id") or a literal (value).
Start
How a case starts: where its key is in the event, and the case’s data from the event. Start::key("aggregate_id").data([("order_id", "aggregate_id"), ("total", "data.total")]).
Timer
A timer step: waits a while (Timer::after("12h")) or until a time in the case’s data, in ms since epoch (Timer::at("case.due_at")).
Wait
A wait step: where this case’s key is in the awaited event, what the event adds to the case, how long to wait. Wait::key("data.order_id").timeout("3d", "cancel").

Functions§

value
A literal in a mapping, where a string would be a reference: Send::aggregate("case.order_id").data([("reason", value("unpaid"))]).