Documentation
¶
Overview ¶
Observer delivery is bounded and best-effort. Observers must return promptly and should honor context cancellation; a blocked or failed observer never changes durable execution correctness or prevents runtime shutdown.
External callers record execution-scoped events with Event.Emit. Event.Deliver provides deliberately detached ingress to a known execution, including from an active worker; passing Runtime.InTx joins it to caller-owned application writes. Same-execution worker events should normally use staged Emit instead.
Index ¶
- Constants
- Variables
- func CancelCommand(ctx context.Context, c Client, id CommandID, reason string) error
- func CancelExecution(ctx context.Context, c Client, id ExecutionID, reason string) error
- func Emit[W, T any](work *Work[W], event Event[T], key string, payload T) error
- func GetEventValue[W, T any](work *Work[W], event Event[T], key string) (T, error)
- func Migrate(ctx context.Context, db *pgkit.DB, opts ...MigrateOption) error
- func MigrationFS(opts ...MigrateOption) (fs.FS, error)
- func Permanent(err error) error
- func ResultOf[A, R any](trace ExecutionTrace, key string, cmd Command[A, R]) (R, error)
- func RetryAfter(delay time.Duration, err error) error
- func WithSchema(schema string) schemaOption
- type AttemptID
- type Client
- type Command
- type CommandFailure
- type CommandID
- type CommandInfo
- type CommandOption
- type CommandStatus
- type Commit
- type Error
- type Event
- type EventID
- type EventRef
- type Execution
- type ExecutionFilter
- type ExecutionID
- type ExecutionOption
- func WaitFor(event EventRef, key string) ExecutionOption
- func WithExecutionDeadline(deadline time.Duration) ExecutionOption
- func WithFailFast(enabled bool) ExecutionOption
- func WithLiveKey() ExecutionOption
- func WithMetadata(metadata map[string]string) ExecutionOption
- func WithStartDelay(delay time.Duration) ExecutionOption
- func Within(duration time.Duration) ExecutionOption
- func WithoutExecutionDeadline() ExecutionOption
- type ExecutionPage
- type ExecutionStatus
- type ExecutionTrace
- type Failure
- type HistoryEntry
- type HistoryKind
- type HistoryOption
- type JournalEntryID
- type JournalPosition
- type KeyScope
- type KeyedHistoryEntry
- type KeyedHistoryFilter
- type KeyedHistoryPage
- type LiveWork
- type LiveWorkFilter
- type LiveWorkPage
- type MigrateOption
- type Node
- type None
- type NopObserver
- type Observation
- type ObservationKind
- type Observer
- type Option
- func WithMaxCommandsPerExecution(max int) Option
- func WithNotifications(enabled bool) Option
- func WithObserver(observer Observer) Option
- func WithPollInterval(interval time.Duration) Option
- func WithQueueConcurrency(queue string, concurrency int) Option
- func WithShutdownGrace(grace time.Duration) Option
- func WithWorkerConcurrency(concurrency int) Option
- type QueueDepth
- type QueueState
- type Registration
- type RetryPolicy
- type Runtime
- type SchemaStatus
- type TerminalStatus
- type TraceAttempt
- type TraceCommand
- type TraceEvent
- type TraceEventWait
- type TraceOption
- type Tx
- type Work
- type WorkerOption
Constants ¶
const ( // MaxReadKeys bounds every by-keys batch read before duplicate removal. MaxReadKeys = store.MaxReadKeys // DefaultReadPageSize is used when a by-key read filter has PageSize zero. DefaultReadPageSize = 100 // MaxReadPageSize is the largest public by-key read page. MaxReadPageSize = 1000 )
const ( ExecutionStatusRunning ExecutionStatus = "running" ExecutionStatusFailing ExecutionStatus = "failing" ExecutionStatusSucceeded ExecutionStatus = "succeeded" ExecutionStatusFailed ExecutionStatus = "failed" ExecutionStatusCancelled ExecutionStatus = "cancelled" ExecutionStatusExpired ExecutionStatus = "expired" CommandStatusPending CommandStatus = "pending" CommandStatusReady CommandStatus = "ready" CommandStatusRunning CommandStatus = "running" CommandStatusRetryWait CommandStatus = "retry_wait" CommandStatusSucceeded CommandStatus = "succeeded" CommandStatusFailed CommandStatus = "failed" CommandStatusCancelled CommandStatus = "cancelled" CommandStatusExpired CommandStatus = "expired" QueueStateReady QueueState = "ready" QueueStateRetryWait QueueState = "retry_wait" QueueStateRunning QueueState = "running" KeyScopePermanent KeyScope = "permanent" KeyScopeLive KeyScope = "live" TerminalStatusSucceeded TerminalStatus = "succeeded" TerminalStatusFailed TerminalStatus = "failed" TerminalStatusCancelled TerminalStatus = "cancelled" TerminalStatusExpired TerminalStatus = "expired" // Short names remain source-compatible aliases for terminal command states. StatusSucceeded = CommandStatusSucceeded StatusFailed = CommandStatusFailed StatusCancelled = CommandStatusCancelled StatusExpired = CommandStatusExpired )
Variables ¶
var ( ErrNotFound = flowerr.ErrNotFound ErrConflict = flowerr.ErrConflict ErrInvalid = flowerr.ErrInvalid ErrInvalidState = flowerr.ErrInvalidState ErrTerminal = flowerr.ErrTerminal ErrLeaseLost = flowerr.ErrLeaseLost ErrPayloadTooLarge = flowerr.ErrPayloadTooLarge ErrClosed = flowerr.ErrClosed ErrSchema = flowerr.ErrSchema )
Functions ¶
func CancelCommand ¶
func CancelExecution ¶
func Emit ¶
Emit stages an application event in a worker decision. It performs no database work and becomes durable only when the enclosing decision settles successfully.
func GetEventValue ¶
GetEventValue returns the typed value attached to an exact event gate declared by the current command. The value is already in memory when the worker starts; this function does not wait or query the database.
func Migrate ¶
Migrate applies every unapplied embedded Flow migration in its own execution-serialized transaction and verifies all previously recorded checksums before writing.
func MigrationFS ¶
func MigrationFS(opts ...MigrateOption) (fs.FS, error)
MigrationFS returns schema-rendered SQL files for an external transactional migration runner. Each file records the same checksum and compatibility row as Migrate, so CheckSchema accepts either application path.
func Permanent ¶
Permanent classifies an application error as terminal for the current command delivery.
func ResultOf ¶
func ResultOf[A, R any](trace ExecutionTrace, key string, cmd Command[A, R]) (R, error)
func RetryAfter ¶
RetryAfter classifies an error as retryable after a requested delay. The command's immutable retry bounds still apply.
func WithSchema ¶
func WithSchema(schema string) schemaOption
WithSchema places Flow's fixed flow_ tables in a validated PostgreSQL schema. The default is public. Runtime options adopt the same value in the runtime phase; table names always retain their flow_ prefix.
Types ¶
type Client ¶
type Client interface {
// contains filtered or unexported methods
}
Client is a sealed capability implemented by Runtime and transaction-scoped clients. Applications pass it or bind it with Definition.With; they cannot construct another implementation that bypasses Flow's durable operations.
type Command ¶
type Command[A, R any] struct { // contains filtered or unexported fields }
func DefineCommand ¶
func DefineCommand[A, R any](name string, version int, opts ...CommandOption) Command[A, R]
type CommandFailure ¶
type CommandFailure = Failure
type CommandInfo ¶
type CommandOption ¶
type CommandOption interface {
// contains filtered or unexported methods
}
func WithQueue ¶
func WithQueue(queue string) CommandOption
func WithRetry ¶
func WithRetry(policy RetryPolicy) CommandOption
func WithTimeout ¶
func WithTimeout(timeout time.Duration) CommandOption
type CommandStatus ¶
type CommandStatus string
type Commit ¶
type Commit[A, R any] struct { Args A Result R Info CommandInfo }
type Error ¶
Error adds safe structured context to a sentinel category. Its fields must contain identifiers and bounded reasons only, never payloads, SQL, secrets, or lease tokens.
type Event ¶
type Event[T any] struct { // contains filtered or unexported fields }
func DefineEvent ¶
func (Event[T]) Deliver ¶
func (event Event[T]) Deliver(ctx context.Context, client Client, target ExecutionID, key string, payload T) error
Deliver records an event in a known execution, including from inside an active worker attempt. Delivery is detached from that attempt: pass a Runtime.InTx client to join a caller-owned transaction. Once committed, the event is not retracted if the source attempt fails or retries; equivalent repeats retain ordinary event idempotency. Use Emit(work, ...) for same-execution events that must settle atomically with the worker decision.
type Execution ¶
type Execution struct {
ID ExecutionID
Type string
Version int
Key string
RootCommandID CommandID
Status ExecutionStatus
FailFast bool
MaxCommands int
CommandCount int
OpenCommands int
DeadlineAt *time.Time
Failure *Failure
CreatedAt time.Time
UpdatedAt time.Time
StatusAt time.Time
FinishedAt *time.Time
Metadata json.RawMessage
Created bool
}
Execution is a durable execution state snapshot. Execute returns the snapshot as of durable acceptance; GetExecution, AwaitExecution, and other inspection reads return the current or final state. Created reports whether the producing Execute call created the execution; it is false for an idempotent rediscovery and always false on inspection reads.
func AwaitExecution ¶
AwaitExecution polls the durable execution row until it reaches a terminal state or ctx ends. It consumes no command worker, connection while waiting, or durable lease.
func GetExecution ¶
func LookupLiveExecution ¶
LookupLiveExecution finds the one non-terminal execution currently holding a live-scoped key for the definition, if any. Live keys admit many settled executions per key over time but at most one live holder; this is the lookup that matches that invariant. found=false means no live holder — settled executions with the key may still exist.
type ExecutionFilter ¶
type ExecutionFilter struct {
Type string
KeyPrefix string
Statuses []ExecutionStatus
CreatedAfter *time.Time
CreatedBefore *time.Time
Metadata map[string]string
PageSize int
Cursor string
}
ExecutionFilter is the bounded, indexed filter supported by ListExecutions. CreatedBefore is exclusive and CreatedAfter is inclusive.
type ExecutionID ¶
type ExecutionID string
type ExecutionOption ¶
type ExecutionOption interface {
// contains filtered or unexported methods
}
ExecutionOption is a sealed command execution option.
func WaitFor ¶
func WaitFor(event EventRef, key string) ExecutionOption
WaitFor gates a root command on one exact application event inside the execution it creates. Multiple waits are AND conditions. Worker decisions use the matching Node.WaitFor method.
func WithExecutionDeadline ¶
func WithExecutionDeadline(deadline time.Duration) ExecutionOption
func WithFailFast ¶
func WithFailFast(enabled bool) ExecutionOption
func WithLiveKey ¶
func WithLiveKey() ExecutionOption
WithLiveKey scopes the execution key to live executions: while a running or failing execution holds the key, Execute rediscovers it without comparing start identity — a silent, queue-style dedupe no-op — and once that execution reaches a terminal status the key is released for a new start. Live-keyed starts therefore give at-most-one live execution per key, not at-most-one execution ever. Requires a non-empty key.
func WithMetadata ¶
func WithMetadata(metadata map[string]string) ExecutionOption
func WithStartDelay ¶
func WithStartDelay(delay time.Duration) ExecutionOption
WithStartDelay schedules an execution's root command to become deliverable after the delay instead of immediately, mirroring Delay for sub-commands.
func Within ¶
func Within(duration time.Duration) ExecutionOption
Within bounds how long a direct root command waits for its exact events. It is valid only when the same start declares at least one WaitFor option.
func WithoutExecutionDeadline ¶
func WithoutExecutionDeadline() ExecutionOption
type ExecutionPage ¶
func ListExecutions ¶
func ListExecutions(ctx context.Context, c Client, filter ExecutionFilter) (ExecutionPage, error)
type ExecutionStatus ¶
type ExecutionStatus string
type ExecutionTrace ¶
type ExecutionTrace struct {
Execution Execution
Commands []TraceCommand
Events []TraceEvent
History []HistoryEntry
}
func Trace ¶
func Trace(ctx context.Context, c Client, id ExecutionID, opts ...TraceOption) (ExecutionTrace, error)
Trace reconstructs one execution and overlays its current operational projections. A Runtime client gets one Flow-owned Repeatable Read snapshot. A transaction-scoped client inherits the caller's isolation; callers needing a coherent multi-statement snapshot must use Repeatable Read or Serializable.
type HistoryEntry ¶
type HistoryEntry struct {
ExecutionID ExecutionID
Position JournalPosition
EntryID JournalEntryID
Kind HistoryKind
RecordedAt time.Time
CausationPosition *JournalPosition
CommandID CommandID
AttemptID AttemptID
EventID EventID
EventNamespace string
EventName string
EventKey string
EventClass string
TerminalStatus TerminalStatus
Body json.RawMessage
BodyHash string
}
func History ¶
func History(ctx context.Context, c Client, id ExecutionID, opts ...HistoryOption) ([]HistoryEntry, error)
type HistoryKind ¶
type HistoryKind string
const ( HistoryExecutionStarted HistoryKind = "execution_started" HistoryExecutionFailing HistoryKind = "execution_failing" HistoryCommandCreated HistoryKind = "command_created" HistoryAttemptStarted HistoryKind = "attempt_started" HistoryAttemptConcluded HistoryKind = "attempt_concluded" HistoryEventRecorded HistoryKind = "event_recorded" )
type HistoryOption ¶
type HistoryOption interface {
// contains filtered or unexported methods
}
func HistoryAfter ¶
func HistoryAfter(position JournalPosition) HistoryOption
func HistoryLimit ¶
func HistoryLimit(limit int) HistoryOption
type JournalEntryID ¶
type JournalEntryID string
type JournalPosition ¶
type JournalPosition uint64
type KeyedHistoryEntry ¶
type KeyedHistoryEntry struct {
DefinitionName string
ExecutionKey string
KeyScope KeyScope
HistoryEntry
}
KeyedHistoryEntry is a history entry carrying its execution's identity.
type KeyedHistoryFilter ¶
KeyedHistoryFilter selects bounded retained history for exact execution keys. Cursor values are opaque and may be reused only with the same Keys filter.
type KeyedHistoryPage ¶
type KeyedHistoryPage struct {
Entries []KeyedHistoryEntry
NextCursor string
}
KeyedHistoryPage contains one bounded page and an opaque cursor for the next page. NextCursor is empty when no later row was observed.
func ListHistoryByKeys ¶
func ListHistoryByKeys(ctx context.Context, c Client, filter KeyedHistoryFilter) (KeyedHistoryPage, error)
ListHistoryByKeys returns one bounded retained-history page for every execution that ever held one of filter.Keys. Rows are ordered by key, definition, execution creation, execution ID, and journal position. Journal order is preserved within each execution. Transaction-scoped clients use the caller's transaction and observe their uncommitted writes.
type LiveWork ¶
type LiveWork struct {
ExecutionID ExecutionID
DefinitionName string
ExecutionKey string
KeyScope KeyScope
ExecutionStatus ExecutionStatus
CommandID CommandID
CommandKey string
CommandName string
Queue string
QueueState QueueState
NextRunAt time.Time
LeaseOwner string
LeaseExpiresAt *time.Time
AttemptOrdinal int
CommandCreatedAt time.Time
}
LiveWork is one queued (or leased) command of a non-terminal execution, carrying the execution's identity. It is the batch, key-addressed read for consumers that decorate their own domain rows with dispatch state, without touching Flow's tables.
type LiveWorkFilter ¶
LiveWorkFilter selects bounded queued work for exact execution keys. Cursor values are opaque and may be reused only with the same Keys filter.
type LiveWorkPage ¶
LiveWorkPage contains one bounded page and an opaque cursor for the next page. NextCursor is empty when no later row was observed.
func ListLiveWork ¶
func ListLiveWork(ctx context.Context, c Client, filter LiveWorkFilter) (LiveWorkPage, error)
ListLiveWork returns one bounded page of queued commands for non-terminal executions whose execution key is in filter.Keys. Rows are ordered by key, definition, execution creation, execution ID, and command ID. An ordinary client does not provide a cross-page snapshot; a transaction-scoped client uses the caller's transaction and observes its uncommitted writes.
type MigrateOption ¶
type MigrateOption interface {
// contains filtered or unexported methods
}
MigrateOption is a sealed migration and schema-inspection option.
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node is an ephemeral builder for a command staged by a worker decision. It is valid only for the duration of that decision.
type NopObserver ¶
type NopObserver struct{}
func (NopObserver) Observe ¶
func (NopObserver) Observe(context.Context, Observation)
type Observation ¶
type Observation struct {
Kind ObservationKind
Operation string
Outcome string
ExecutionID ExecutionID
CommandID CommandID
CommandKey string
Name string
Version int
Queue string
Worker string
Count int64
Duration time.Duration
OccurredAt time.Time
}
Observation contains only bounded operational metadata. It intentionally has no payload, result, raw SQL, connection, or lease token field.
type ObservationKind ¶
type ObservationKind string
const ( ObservationExecution ObservationKind = "execution" ObservationCommand ObservationKind = "command" ObservationEvent ObservationKind = "event" ObservationAttempt ObservationKind = "attempt" ObservationClaim ObservationKind = "claim" ObservationLease ObservationKind = "lease" ObservationWait ObservationKind = "wait" ObservationRuntime ObservationKind = "runtime" )
type Observer ¶
type Observer interface {
// Observe receives best-effort operational metadata. Implementations must
// return promptly and should stop work when ctx is cancelled. Flow never
// waits indefinitely for an observer during runtime shutdown.
Observe(context.Context, Observation)
}
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is a sealed runtime configuration option.
func WithMaxCommandsPerExecution ¶
WithMaxCommandsPerExecution sets the command ceiling copied into each newly created execution. Zero explicitly disables the ceiling.
func WithNotifications ¶
WithNotifications enables or disables transactional PostgreSQL wake hints. It defaults to enabled. Polling always remains active and is the correctness path, so disabling notifications is suitable for transaction-pooling proxies and deliberately poll-only deployments.
func WithObserver ¶
WithObserver installs the optional no-op-by-default operational observer.
func WithPollInterval ¶
WithPollInterval configures the fallback scheduler and maintenance poll.
func WithQueueConcurrency ¶
WithQueueConcurrency optionally gives one queue lane a smaller process-local handler limit. The lane still shares the runtime's global worker capacity.
func WithShutdownGrace ¶
WithShutdownGrace configures how long Run waits before interrupting handlers.
func WithWorkerConcurrency ¶
WithWorkerConcurrency bounds command handlers running in this process.
type QueueDepth ¶
type QueueDepth struct {
Queue string
Ready int64
Delayed int64
Running int64
OldestReadyFor time.Duration
}
QueueDepth is a point-in-time operational snapshot of one queue lane. Ready commands are deliverable now, Delayed commands wait out a retry backoff or start delay, and Running commands hold an attempt lease. OldestReadyFor is how long the oldest deliverable command has been ready; a growing value with stable Ready means no compatible worker is claiming the lane.
func GetQueueDepth ¶
GetQueueDepth reports the lane's current deliverable, scheduled, and leased command counts. It reads operational delivery state, not application events: the counts change as attempts are claimed and settled.
type QueueState ¶
type QueueState string
type Registration ¶
type Registration interface {
// contains filtered or unexported methods
}
func Handle ¶
func Handle[A, R any]( cmd Command[A, R], worker func(context.Context, *Work[A]) (R, error), opts ...WorkerOption[A, R], ) Registration
type RetryPolicy ¶
type RetryPolicy = retrypolicy.PublicPolicy
RetryPolicy is immutable declarative data. Its fields are sealed so durable behavior can only be constructed through validated builders.
func Attempts ¶
func Attempts(max int) RetryPolicy
func RetryFor ¶
func RetryFor(maxElapsed time.Duration) RetryPolicy
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
Runtime is a configured PostgreSQL-backed Flow client. New starts no goroutines; execution operations are usable before background processing is started.
func New ¶
New validates configuration and schema compatibility without migrating or starting background work.
func (*Runtime) InTx ¶
InTx returns a client whose writes participate in the supplied caller-owned transaction. Flow never commits or rolls back that transaction.
func (*Runtime) Register ¶
func (r *Runtime) Register(definitions ...Registration) error
type SchemaStatus ¶
type SchemaStatus struct {
Schema string
CurrentVersion int
MinReaderVersion int
MinWriterVersion int
Compatible bool
AppliedAt time.Time
}
SchemaStatus describes the verified migration and compatibility state.
func CheckSchema ¶
func CheckSchema(ctx context.Context, db *pgkit.DB, opts ...MigrateOption) (SchemaStatus, error)
CheckSchema verifies migration checksums, reader/writer compatibility, and the fixed Flow table inventory without changing the database.
type TerminalStatus ¶
type TerminalStatus string
type TraceAttempt ¶
type TraceCommand ¶
type TraceCommand struct {
ID CommandID
Key string
Name string
Version int
ParentCommandID CommandID
Required bool
State CommandStatus
Args json.RawMessage
Result json.RawMessage
Queue string
InitialDelay time.Duration
BudgetStartedAt *time.Time
NextAttemptAt *time.Time
Within time.Duration
Waits []TraceEventWait
CreatedPosition JournalPosition
TerminalPosition *JournalPosition
Failure *Failure
LastError *Failure
UnsatisfiedWaits int
AttemptOrdinal int
ConsumedAttempts int
WaitStartedAt *time.Time
WaitDeadlineAt *time.Time
DeliveryState QueueState
LeaseOwner string
LeaseStartedAt *time.Time
LeaseExpiresAt *time.Time
CreatedAt time.Time
UpdatedAt time.Time
StatusAt time.Time
FinishedAt *time.Time
Attempts []TraceAttempt
}
type TraceEvent ¶
type TraceEvent struct {
ID EventID
Position JournalPosition
Namespace string
Name string
Key string
Class string
TerminalStatus TerminalStatus
CommandID CommandID
RecordedAt time.Time
CausationPosition *JournalPosition
Body json.RawMessage
}
type TraceEventWait ¶
type TraceEventWait struct {
Name string
Key string
SatisfiedPosition *JournalPosition
}
type TraceOption ¶
type TraceOption interface {
// contains filtered or unexported methods
}
type Work ¶
type Work[A any] struct { Args A // contains filtered or unexported fields }
func (*Work[A]) Info ¶
func (w *Work[A]) Info() CommandInfo
type WorkerOption ¶
type WorkerOption[A, R any] interface { // contains filtered or unexported methods }
func WithCommit ¶
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
agent
command
|
|
|
direct
command
|
|
|
fanout
command
|
|
|
monitor
command
|
|
|
Package flowtest provides database-free helpers backed by Flow's production codecs and deterministic engine primitives.
|
Package flowtest provides database-free helpers backed by Flow's production codecs and deterministic engine primitives. |
|
replaytest
Package replaytest provides PostgreSQL-backed assertions for verifying that Flow's journal replay agrees with its live projections.
|
Package replaytest provides PostgreSQL-backed assertions for verifying that Flow's journal replay agrees with its live projections. |
|
internal
|
|
|
canonical
Package canonical provides the deterministic JSON representation used by Flow's durable identities.
|
Package canonical provides the deterministic JSON representation used by Flow's durable identities. |
|
durable
Package durable contains validation shared by public construction and the PostgreSQL store for values whose durable representation is narrower than their Go representation.
|
Package durable contains validation shared by public construction and the PostgreSQL store for values whose durable representation is narrower than their Go representation. |
|
fault
Package fault defines named internal fault points used by integration and crash-recovery tests.
|
Package fault defines named internal fault points used by integration and crash-recovery tests. |
|
replay
Package replay folds retained journal entries into settled orchestration projections.
|
Package replay folds retained journal entries into settled orchestration projections. |
|
store/journalcodec
Package journalcodec owns version validation for internal journal bodies.
|
Package journalcodec owns version validation for internal journal bodies. |
|
testengine
Package testengine is the private bridge between flow's production deterministic recorders and the public database-free flowtest package.
|
Package testengine is the private bridge between flow's production deterministic recorders and the public database-free flowtest package. |
|
testpg
Package testpg creates isolated PostgreSQL schemas for integration tests.
|
Package testpg creates isolated PostgreSQL schemas for integration tests. |
|
tools
|
|
|
db
command
Command db manages the disposable PostgreSQL database used by Flow's tests.
|
Command db manages the disposable PostgreSQL database used by Flow's tests. |