lifecycle

package
v0.1.66 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package lifecycle provides generic event-driven lifecycle primitives.

It is intentionally separate from policycontroller, which reconciles a desired state synchronously, and eventstream, which carries bounded live command output.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoWork       = errors.New("no lifecycle work available")
	ErrInvalidEvent = errors.New("invalid lifecycle event")
	ErrLeaseLost    = errors.New("lifecycle event lease lost")
)

Functions

func SortRecordsByID

func SortRecordsByID(records map[string]Record) []string

SortRecordsByID provides deterministic ordering for store implementations.

Types

type Claim

type Claim struct {
	Event      Event
	LeaseToken string
	LeaseUntil time.Time
}

Claim identifies a leased event and prevents a stale worker from acknowledging or rescheduling a lease it no longer owns.

type Dispatcher

type Dispatcher struct {
	Events        EventStore
	Handler       Handler
	Clock         func() time.Time
	LeaseDuration time.Duration
	RetryDelay    time.Duration
	CompactAfter  time.Duration
}

Dispatcher publishes and delivers lifecycle events with at-least-once semantics. Handlers must be idempotent because a process can exit after an action succeeds and before its acknowledgement is persisted.

func NewDispatcher

func NewDispatcher(events EventStore, handler Handler) *Dispatcher

NewDispatcher constructs a dispatcher with conservative operational defaults. The queue remains usable without a background goroutine through RunOnce, which keeps integration tests and one-shot tools deterministic.

func (*Dispatcher) Publish

func (d *Dispatcher) Publish(ctx context.Context, event Event) error

Publish appends an event. An existing event with the same ID is treated as an idempotent duplicate when its envelope matches.

func (*Dispatcher) Run

func (d *Dispatcher) Run(ctx context.Context) error

Run delivers events until ctx is cancelled.

func (*Dispatcher) RunOnce

func (d *Dispatcher) RunOnce(ctx context.Context) (bool, error)

RunOnce claims and processes one available event. It returns false without error when the queue is empty.

type Event

type Event struct {
	ID          string          `json:"id"`
	Type        string          `json:"type"`
	Subject     string          `json:"subject"`
	Payload     json.RawMessage `json:"payload,omitempty"`
	RecordedAt  time.Time       `json:"recorded_at"`
	AvailableAt time.Time       `json:"available_at"`
	Attempt     int             `json:"attempt"`
}

Event is the non-secret envelope persisted in the operational event queue. Payload must contain identifiers and safe state only; callers must not put credentials or other secret material in it.

func NewEvent

func NewEvent(eventType, subject string, payload json.RawMessage, now time.Time) Event

NewEvent creates an event with a generated ID and server receipt time.

type EventStore

EventStore is the narrow durable queue contract used by Dispatcher.

type Handler

type Handler interface {
	Handle(context.Context, Event) (Outcome, error)
}

Handler evaluates an event and performs the policy-selected action.

type HandlerFunc

type HandlerFunc func(context.Context, Event) (Outcome, error)

HandlerFunc adapts a function to Handler.

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(ctx context.Context, event Event) (Outcome, error)

type Outcome

type Outcome string

Outcome tells Dispatcher how to record a handler result.

const (
	OutcomeAck      Outcome = "ack"
	OutcomeRetry    Outcome = "retry"
	OutcomeTerminal Outcome = "terminal"
)

type Record

type Record struct {
	Event      Event     `json:"event"`
	Status     Status    `json:"status"`
	LeaseToken string    `json:"lease_token,omitempty"`
	LeaseUntil time.Time `json:"lease_until,omitempty"`
	LastError  string    `json:"last_error,omitempty"`
	Completed  time.Time `json:"completed,omitempty"`
}

Record is the persisted queue representation. EventStore implementations may persist this directly in their native format.

type Status

type Status string

Status is the durable queue state of an event.

const (
	StatusPending Status = "pending"
	StatusLeased  Status = "leased"
	StatusAcked   Status = "acked"
	StatusFailed  Status = "failed"
)

Jump to

Keyboard shortcuts

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