effect

suspend fun <T> effect(key: String, serializer: StateSerializer<T>, block: suspend () -> T): T

Runs block, a side effect such as a payment or an email, once for the step of the node that calls it, and returns its result. When the step runs again, the call returns the result of the first time and block does not run.

val pay = node("pay") { order ->
val receipt = effect("charge", ReceiptSerializer) { payments.charge(order.id, order.amount) }
order.copy(receipt = receipt, status = "paid")
}

A step runs again after a failure: a node with a Retry is tried again, and CompiledGraph.resume runs the whole step of a run that failed, including the nodes that had already finished. It also runs again after interrupt. Each time, the effects that finished before are not repeated. With telar-serialization, effect(key) { ... } finds the serializer of a @Serializable result itself.

What to know:

  • The result is saved as soon as block returns, in the checkpoint the step continues from, so a crash or a failure of another node later in the step does not lose it. The checkpoint of the finished step no longer has it: the next time the node runs, in a later step, the effect runs again.

  • key names the effect within the node. Two nodes may use the same key. One run of a node may not: a second call with a key the node already used throws a GraphValidationException. An effect that runs once for each item of a list needs a key for each item, such as "email-${customer.id}".

  • A block that throws saves nothing, and runs again the next time.

  • What block did between its start and the save may still happen twice, when the process stops in that moment. For an effect that must never happen twice, use the effect with a reconcile, which gives block an idempotency key and asks what happened.

  • Without a checkpointer the results are kept in memory, for the tries of a Retry. A Checkpointer stores them with Checkpoint.effects.

  • CompiledGraph.fork runs them again. The results belong to the thread the checkpoint comes from, and a fork starts a thread of its own.

Call it from the action of a node, or from the work of a node with a work and an update. Outside a running node it runs block every time.

Parameters

key

names the effect, unique within one run of the node.

serializer

stores the result of block as a string and reads it back.

Throws

when the node already called effect with key in this run.

when serializer cannot read the result that was saved, for example because its type changed.


suspend fun <T> effect(key: String, serializer: StateSerializer<T>, reconcile: suspend (id: String) -> T?, block: suspend (id: String) -> T): T

Runs block, a side effect such as a payment, once for the step of the node that calls it, as the effect without reconcile does, and also closes the moment in which that one can repeat it: between the end of block and the save of its result.

Before block runs, the effect saves that it started, with a new id. block gets the id: pass it to the service as its idempotency key. When the step runs again and finds the effect started but without a result, the process stopped or block failed while the service may have done the work. Then reconcile is asked first, with the same id:

  • it returns the result when the work was done, for example after it found the payment with that id at the provider. The result is saved, and block does not run.

  • it returns null when the work was not done. block runs again, with the same id, so a service that takes idempotency keys still does the work only once.

  • it throws, or calls interrupt to let a person check, when it cannot tell.

val pay = node("pay") { order ->
val receipt = effect(
"charge",
ReceiptSerializer,
reconcile = { id -> payments.findByIdempotencyKey(id) },
) { id -> payments.charge(order.id, order.amount, idempotencyKey = id) }
order.copy(receipt = receipt)
}

For a service with idempotency keys, reconcile = { null } is enough: block runs again with the same key, and the service answers with what it did the first time.

What to know:

  • It saves twice: once before block and once after. The first save is what makes the id the same when the step runs again.

  • The id belongs to this effect of this run of the step. Another key, another step, another run of the thread or a CompiledGraph.fork gets a new one. Ids of tool calls and step numbers repeat across runs, so do not build a key from them.

  • A block that throws leaves the effect started: a timeout can come after the service did the work. The next call with key, in this run of the node or when the step runs again, asks reconcile.

  • effectId returns the id as well, to code that block or reconcile calls.

  • Outside a running node, block runs every time, with a new id, and reconcile is never asked.

Everything else is as for the effect without reconcile.

Parameters

key

names the effect, unique within one run of the node.

serializer

stores the result of block as a string and reads it back.

reconcile

tells, from the id, whether a block that started before did its work: its result if it did, null if it did not.

block

does the work, with the id as the idempotency key.

Throws

when the node already called effect with key in this run.

when serializer cannot read the result that was saved.