state

package
v0.1.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TransitionState

func TransitionState(ctx context.Context, m *Machine, u StateUpdater, versionID string, from, to model.ObjectState) error

TransitionState validates a state change via the FSM, then atomically updates the database. This is the single entry point for all object state changes, providing dual protection:

  1. FSM validation (catches programming errors early)
  2. DB compare-and-set (prevents races at the data level)

When transitioning to ObjectStateFailed, use TransitionToFailed instead to record the phase that failed and the error message.

func TransitionToFailed

func TransitionToFailed(ctx context.Context, m *Machine, u StateUpdater, versionID string, from model.ObjectState, lastError string) error

TransitionToFailed validates the transition to ObjectStateFailed via the FSM, then atomically updates the database, recording the source state (FailedAtState) and the error message. Workers use FailedAtState to determine the correct retry target.

Types

type Machine

type Machine struct {
	// contains filtered or unexported fields
}

Machine is a generic, extensible finite state machine. States and transitions can be registered dynamically, supporting future additions without modifying existing logic.

func New

func New() *Machine

New creates a new empty state machine.

func NewObjectStateMachine

func NewObjectStateMachine() *Machine

NewObjectStateMachine returns a state machine pre-configured with the standard object lifecycle transitions.

func (*Machine) CanTransition

func (m *Machine) CanTransition(from, to string) bool

CanTransition reports whether moving from → to is allowed.

func (*Machine) NextStates

func (m *Machine) NextStates(from string) []string

NextStates returns all states reachable from the given state.

func (*Machine) Register

func (m *Machine) Register(transitions ...Transition)

Register adds one or more valid transitions.

func (*Machine) Validate

func (m *Machine) Validate(from, to string) error

Validate checks the transition and returns an error if it is not allowed.

type StateUpdater

type StateUpdater interface {
	UpdateVersionState(ctx context.Context, versionID string, from, to model.ObjectState) error
	UpdateVersionStateToFailed(ctx context.Context, versionID string, from model.ObjectState, lastError string) error
}

StateUpdater abstracts the DB compare-and-set for state transitions.

type Transition

type Transition struct {
	From string
	To   string
}

Transition defines a valid state change.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL