~/bend-docscommunity

commands.bend relies on unsafe/foreign

raw source on the hub · import bend-kit-commands@0.3.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

3 imports
import Base
import bend-kit-bytes@0.3.3.0/bytes.bend as Bytes
import bend-kit-sqlite@0.1.0.1/sqlite.bend as Sqlite

Types

type Outcome source · line 8 · raw

@-R:Type -> Type

type Record source · line 13 · raw

@-P:Type -> @-R:Type -> Type

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

type Reply source · line 16 · raw

@-R:Type -> Type

type Decision source · line 22 · raw

@-S:Type -> @-R:Type -> @-E:Type -> Type

The domain's answer to a new command.

type Step source · line 27 · raw

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

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

type Store source · line 159 · raw

Data

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

type Pending source · line 364 · raw

Type

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

Definitions

def accepted source · line 32 · raw

@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @rr:Pair(R, R) -> @s2:S -> @p:P -> @at:U32 -> @es:List<&1, E> -> Step<S, P, R, E>

def refused source · line 36 · raw

@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @rr:Pair(R, R) -> @p:P -> @at:U32 -> Step<S, P, R, E>

def replayed source · line 54 · raw

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

def Run source · line 75 · raw

@-A:Type -> Type

def Run.pure source · line 78 · raw

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

def Run.fail source · line 81 · raw

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

def Run.next source · line 84 · raw

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

def Run.bind source · line 91 · raw

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

def first source · line 95 · 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 104 · raw

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

def exec source · line 114 · 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 120 · raw

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

def once source · line 127 · 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 132 · raw

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

def blob.of source · line 135 · raw

@lw:Pair(U32, Array<U32>) -> 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes

def column source · line 139 · raw

@+s:U32 -> @i:U32 -> Run(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes)

def bind source · line 144 · raw

@+s:U32 -> @i:U32 -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Unit)

def same.of source · line 148 · raw

@r:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, Bool)) -> Bool

def same source · line 153 · raw

@a:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Bool

The payload comparison that handle gives step: every byte, NUL included.

def init.run source · line 162 · raw

@+s:U32 -> @init:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Unit)

def open source · line 169 · raw

@path:String -> @init:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @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 188 · raw

@st:Store -> Run(Unit)

def outcome source · line 201 · raw

@accepted:Bool -> @r:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Outcome<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>

def seen.row source · line 208 · raw

@row:Bool -> @+find:U32 -> Run(Maybe<&1, Record<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>>)

def seen.get source · line 221 · raw

@+find:U32 -> @id:String -> Run(Maybe<&1, Record<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>>)

The record for id, if the store holds one.

def state.row source · line 229 · raw

@row:Bool -> @+read:U32 -> Run(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes)

def state.get source · line 236 · raw

@+read:U32 -> Run(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes)

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

@+keep:U32 -> @id:String -> @p:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @kind:U32 -> @r:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @at:U32 -> Run(Unit)

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

@o:Outcome<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes> -> @+keep:U32 -> @id:String -> @p:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @at:U32 -> Run(Unit)

def seen.put source · line 259 · raw

@rec:Record<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes> -> @+keep:U32 -> @id:String -> Run(Unit)

def outbox.put source · line 263 · raw

@es:List<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes> -> @+emit:U32 -> @+id:String -> Run(Unit)

def state.put source · line 274 · raw

@+save:U32 -> @s:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Unit)

def written source · line 280 · raw

@w:Step<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes> -> @+commit:U32 -> @+save:U32 -> @+keep:U32 -> @+emit:U32 -> @+id:String -> Run(Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)

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

def handle.end source · line 326 · raw

@r:Result<&1, &1, Pair(U32, String), Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>> -> @st:Store -> Run(Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)

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

def state source · line 349 · raw

@st:Store -> Run(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes)

The committed domain state.

def prune.run source · line 353 · raw

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

def prune source · line 359 · 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 367 · raw

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

def outbox.first source · line 378 · raw

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

def outbox.next source · line 384 · raw

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

The oldest undelivered effect.

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

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

def outbox.ack source · line 394 · raw

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

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

Templates

template decided source · line 40 · raw

@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @-dup_r:(@_:R -> Pair(R, R)) -> @d:Decision<S, R, E> -> @p:P -> @at:U32 -> Step<S, P, R, E>

template fresh source · line 47 · raw

@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @-dup_r:(@_:R -> Pair(R, R)) -> @-decide:(@_:S -> @_:P -> Decision<S, R, E>) -> @pp:Pair(P, P) -> @s:S -> @at:U32 -> Step<S, P, R, E>

template step source · line 63 · raw

@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @-eq:(@_:P -> @_:P -> Bool) -> @-dup_p:(@_:P -> Pair(P, P)) -> @-dup_r:(@_:R -> Pair(R, R)) -> @-decide:(@_:S -> @_:P -> Decision<S, R, E>) -> @found:Maybe<&1, 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. dup_p and dup_r copy a payload and a result: a new command's payload goes to decide and to the record.

template handle.found source · line 302 · raw

@-decide:(@_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Decision<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> @found:Maybe<&1, Record<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>> -> @+read:U32 -> @payload:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @at:U32 -> @+commit:U32 -> @+save:U32 -> @+keep:U32 -> @+emit:U32 -> @+id:String -> Run(Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)

A duplicate needs no state: step ignores s when found is Some, so it gets an empty one.

template handle.txn source · line 315 · raw

@-decide:(@_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Decision<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> @st:Store -> @+id:String -> @payload:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @at:U32 -> Run(Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)

template handle source · line 340 · raw

@-decide:(@_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Decision<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> @+st:Store -> @id:String -> @payload:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @at:U32 -> Run(Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)

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.