~/bend-docscommunity

commands.bend relies on unsafe/foreign

raw source on the hub · import bend-kit-commands@0.1.0.0/commands.bend as Commands

Retry-safe commands: deduplicate by command id, replay recorded results, and commit state, record, and outbox in one SQLite transaction. Source: https://github.com/paymog/bend-kit/tree/main/commands

2 imports
import Base
import bend-kit-sqlite@0.1.0.1/sqlite.bend as Sqlite

Types

type Outcome source · line 7 · raw

@-R:Data -> Data

type Record source · line 12 · raw

@-P:Data -> @-R:Data -> Data

What the store keeps for one command id: its payload, its outcome, and when it was recorded.

type Reply source · line 15 · raw

@-R:Data -> Data

type Decision source · line 21 · raw

@-S:Data -> @-R:Data -> @-E:Data -> Data

The domain's answer to a new command.

type Step source · line 26 · raw

@-S:Data -> @-P:Data -> @-R:Data -> @-E:Data -> Data

Write: commit the state, the record, and the effects together. Keep: write nothing.

type Store source · line 120 · raw

Data

A database and its prepared statements. One Store serves one task at a time.

type Pending source · line 295 · raw

Data

An undelivered effect: its sequence number, its command id, and the effect.

Definitions

def decided source · line 30 · raw

@-S:Data -> @-P:Data -> @-R:Data -> @-E:Data -> @d:Decision<S, R, E> -> @s:S -> @p:P -> @at:U32 -> Step<S, P, R, E>

def replayed source · line 37 · raw

@-R:Data -> @same:Bool -> @o:Outcome<R> -> Reply<R>

def Run source · line 57 · raw

@-A:Type -> Type

def Run.pure source · line 60 · raw

@-A:Type -> @x:A -> Run(A)

def Run.fail source · line 63 · raw

@-A:Type -> @e:Pair(U32, String) -> Run(A)

def Run.next source · line 66 · raw

@-A:Type -> @-B:Type -> @r:Result<&1, &1, Pair(U32, String), A> -> @f:(@_:A -> Run(B)) -> Run(B)

def Run.bind source · line 73 · raw

@-A:Type -> @-B:Type -> @m:Run(A) -> @f:(@_:A -> Run(B)) -> Run(B)

def first source · line 77 · raw

@-A:Type -> @r:Result<&1, &1, Pair(U32, String), A> -> @z:Result<&1, &1, Pair(U32, String), Unit> -> Result<&1, &1, Pair(U32, String), A>

r's value, unless r or then z failed.

def unit source · line 86 · raw

@-A:Type -> @r:Result<&1, &1, Pair(U32, String), A> -> Result<&1, &1, Pair(U32, String), Unit>

def exec source · line 96 · raw

@+stmt:U32 -> Run(Unit)

Steps a statement and ignores any row, then resets it, also after a failed step.

def once.run source · line 102 · raw

@-A:Type -> @+s:U32 -> @body:(@_:U32 -> Run(A)) -> Run(A)

def once source · line 109 · raw

@-A:Type -> @+db:U32 -> @text:String -> @body:(@_:U32 -> Run(A)) -> Run(A)

Prepares one statement, runs body on it, and finalizes it, also after body fails.

def sql source · line 114 · raw

@+db:U32 -> @text:String -> Run(Unit)

def init.run source · line 123 · raw

@+s:U32 -> @init:String -> Run(Unit)

def open source · line 130 · raw

@path:String -> @init:String -> @wait:U32 -> Run(Store)

Opens or creates the tables at path. A new database starts with domain state init. wait is how many ms a write waits for another connection's transaction before it fails with SQLITE_BUSY (5).

def close source · line 149 · raw

@st:Store -> Run(Unit)

def outcome source · line 162 · raw

@+kind:U32 -> @+r:String -> Outcome<String>

def seen.row source · line 165 · raw

@row:Bool -> @+find:U32 -> Run(Maybe<&2, Record<String, String>>)

def seen.get source · line 178 · raw

@+find:U32 -> @id:String -> Run(Maybe<&2, Record<String, String>>)

The record for id, if the store holds one.

def state.row source · line 186 · raw

@row:Bool -> @+read:U32 -> Run(String)

def state.get source · line 193 · raw

@+read:U32 -> Run(String)

def seen.put.row source · line 200 · raw

@+keep:U32 -> @id:String -> @p:String -> @kind:U32 -> @r:String -> @at:U32 -> Run(Unit)

def seen.put.of source · line 209 · raw

@o:Outcome<String> -> @+keep:U32 -> @id:String -> @p:String -> @at:U32 -> Run(Unit)

def seen.put source · line 216 · raw

@rec:Record<String, String> -> @+keep:U32 -> @id:String -> Run(Unit)

def outbox.put source · line 220 · raw

@es:List<&2, String> -> @+emit:U32 -> @+id:String -> Run(Unit)

def state.put source · line 231 · raw

@+save:U32 -> @s:String -> Run(Unit)

def written source · line 237 · raw

@w:Step<String, String, String, String> -> @+commit:U32 -> @+save:U32 -> @+keep:U32 -> @+emit:U32 -> @+id:String -> Run(Reply<String>)

Applies a step inside the open transaction, then commits it.

def handle.end source · line 260 · raw

@r:Result<&1, &1, Pair(U32, String), Reply<String>> -> @st:Store -> Run(Reply<String>)

On failure, resets the read statements and rolls back. Rollback errors are ignored: the transaction may not have started.

def state source · line 280 · raw

@st:Store -> Run(String)

The committed domain state.

def prune.run source · line 284 · raw

@+s:U32 -> @before:U32 -> Run(Unit)

def prune source · line 290 · raw

@st:Store -> @before:U32 -> Run(Unit)

Forgets every record made before the time before. A later retry of a forgotten id runs as a new command.

def outbox.row source · line 298 · raw

@row:Bool -> @+s:U32 -> Run(Maybe<&2, Pending>)

def outbox.first source · line 309 · raw

@+s:U32 -> Run(Maybe<&2, Pending>)

def outbox.next source · line 315 · raw

@st:Store -> Run(Maybe<&2, Pending>)

The oldest undelivered effect.

def outbox.ack.run source · line 319 · raw

@+s:U32 -> @seq:U32 -> Run(Unit)

def outbox.ack source · line 325 · raw

@st:Store -> @seq:U32 -> Run(Unit)

Removes a delivered effect. Deliver before ack: a crash between them delivers the effect again.

Templates

template step source · line 45 · raw

@-S:Data -> @-P:Data -> @-R:Data -> @-E:Data -> @-eq:(@_:P -> @_:P -> Bool) -> @-decide:(@_:S -> @_:P -> Decision<S, R, E>) -> @found:Maybe<&2, Record<P, R>> -> @+s:S -> @+p:P -> @at:U32 -> Step<S, P, R, E>

One command, given the record that the store holds for its id and the domain state s.

template handle.txn source · line 251 · raw

@-decide:(@_:String -> @_:String -> Decision<String, String, String>) -> @st:Store -> @+id:String -> @payload:String -> @at:U32 -> Run(Reply<String>)

template handle source · line 274 · raw

@-decide:(@_:String -> @_:String -> Decision<String, String, String>) -> @+st:Store -> @id:String -> @payload:String -> @at:U32 -> Run(Reply<String>)

Runs one command in one transaction: find its record, decide, and commit state, record, and effects. at is the record's time for prune, in seconds. decide sees the committed state and the payload.