state

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanTransition

func CanTransition(from, to State) bool

CanTransition checks if a transition from one state to another is valid

Types

type Event

type Event string

Event represents events that can trigger state transitions

const (
	// EventStart triggers initial startup
	EventStart Event = "start"

	// EventCoreStarted indicates core subprocess started successfully
	EventCoreStarted Event = "core_started"

	// EventCoreReady indicates core is ready to serve requests
	EventCoreReady Event = "core_ready"

	// EventAPIConnected indicates successful API/SSE connection
	EventAPIConnected Event = "api_connected"

	// EventConnectionLost indicates lost connection to core
	EventConnectionLost Event = "connection_lost"

	// EventCoreExited indicates core subprocess exited
	EventCoreExited Event = "core_exited"

	// EventPortConflict indicates core failed due to port conflict
	EventPortConflict Event = "port_conflict"

	// EventDBLocked indicates core failed due to database lock
	EventDBLocked Event = "db_locked"

	// EventConfigError indicates core failed due to configuration error
	EventConfigError Event = "config_error"

	// EventPermissionError indicates core failed due to permission error
	EventPermissionError Event = "permission_error"

	// EventDockerUnavailable indicates Docker engine is unavailable or paused
	EventDockerUnavailable Event = "docker_unavailable"

	// EventDockerRecovered indicates Docker engine became available again
	EventDockerRecovered Event = "docker_recovered"

	// EventGeneralError indicates core failed with general error
	EventGeneralError Event = "general_error"

	// EventRetry triggers retry attempt
	EventRetry Event = "retry"

	// EventShutdown triggers shutdown
	EventShutdown Event = "shutdown"

	// EventTimeout indicates a timeout occurred
	EventTimeout Event = "timeout"

	// EventSkipCore indicates core launch should be skipped
	EventSkipCore Event = "skip_core"
)

type Info

type Info struct {
	Name        State
	Description string
	IsError     bool
	CanRetry    bool
	UserMessage string
	Timeout     *time.Duration
}

Info provides metadata about each state

func GetInfo

func GetInfo(state State) Info

GetInfo returns metadata for a given state

type Machine

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

Machine manages state transitions for the tray application

func NewMachine

func NewMachine(logger *zap.SugaredLogger) *Machine

NewMachine creates a new state machine

func (*Machine) GetCurrentState

func (m *Machine) GetCurrentState() State

GetCurrentState returns the current state

func (*Machine) GetLastError

func (m *Machine) GetLastError() error

GetLastError returns the last error that occurred

func (*Machine) GetRetryCount

func (m *Machine) GetRetryCount(state State) int

GetRetryCount returns the current retry count for the given state

func (*Machine) GetRetryDelay

func (m *Machine) GetRetryDelay(state State) time.Duration

GetRetryDelay returns the retry delay for the given state

func (*Machine) SendEvent

func (m *Machine) SendEvent(event Event)

SendEvent sends an event to the state machine

func (*Machine) SetError

func (m *Machine) SetError(err error)

SetError sets an error on the state machine

func (*Machine) ShouldRetry

func (m *Machine) ShouldRetry(state State) bool

ShouldRetry checks if we should retry for the given state (exported for error handlers)

func (*Machine) Shutdown

func (m *Machine) Shutdown()

Shutdown gracefully shuts down the state machine

func (*Machine) Start

func (m *Machine) Start()

Start starts the state machine

func (*Machine) Subscribe

func (m *Machine) Subscribe() <-chan Transition

Subscribe returns a channel for receiving state transitions

type State

type State string

State represents the current state of the tray application

const (
	// StateInitializing represents the initial startup state
	StateInitializing State = "initializing"

	// StateLaunchingCore represents launching the core subprocess
	StateLaunchingCore State = "launching_core"

	// StateWaitingForCore represents waiting for core to become ready
	StateWaitingForCore State = "waiting_for_core"

	// StateConnectingAPI represents establishing API/SSE connection
	StateConnectingAPI State = "connecting_api"

	// StateConnected represents fully connected and operational
	StateConnected State = "connected"

	// StateReconnecting represents attempting to reconnect after disconnection
	StateReconnecting State = "reconnecting"

	// StateCoreErrorPortConflict represents core failed due to port conflict
	StateCoreErrorPortConflict State = "core_error_port_conflict"

	// StateCoreErrorDBLocked represents core failed due to database lock
	StateCoreErrorDBLocked State = "core_error_db_locked"

	// StateCoreErrorDocker represents core failed due to Docker being unavailable
	StateCoreErrorDocker State = "core_error_docker"

	// StateCoreRecoveringDocker represents Docker recovery in progress
	StateCoreRecoveringDocker State = "core_recovering_docker"

	// StateCoreErrorConfig represents core failed due to configuration error
	StateCoreErrorConfig State = "core_error_config"

	// StateCoreErrorPermission represents core failed due to permission error
	StateCoreErrorPermission State = "core_error_permission"

	// StateCoreErrorGeneral represents core failed with general error
	StateCoreErrorGeneral State = "core_error_general"

	// StateShuttingDown represents clean shutdown in progress
	StateShuttingDown State = "shutting_down"

	// StateFailed represents unrecoverable failure
	StateFailed State = "failed"
)

type Transition

type Transition struct {
	From      State
	To        State
	Event     Event
	Timestamp time.Time
	Data      map[string]interface{}
	Error     error
}

Transition represents a state change with metadata

Jump to

Keyboard shortcuts

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