Worker lifecycle state machine.
Manages worker state transitions following the same pattern as the TypeScript/Deno pgflow WorkerState. Validates that only allowed transitions occur.
States
:created- Worker has been created but not yet started:starting- Worker is starting but not yet processing messages:running- Worker is actively processing messages:stopping- Worker stopped processing, releasing resources:stopped- Worker has stopped and released resources (terminal state)
State Transitions
created -> starting -> running -> stopping -> stoppedUsage
lifecycle = Lifecycle.new()
{:ok, lifecycle} = Lifecycle.transition(lifecycle, :starting)
{:ok, lifecycle} = Lifecycle.transition(lifecycle, :running)
Lifecycle.running?(lifecycle)
#=> true
Summary
Functions
Returns true if the worker can accept new work.
Returns the current state.
Creates a new lifecycle in the :created state.
Returns true if in :running state.
Returns true if in :stopped state.
Transitions to a new state if the transition is valid.
Transitions to a new state, raising on invalid transition.
Types
@type state() :: :created | :starting | :running | :stopping | :stopped
@type t() :: %PgFlow.Worker.Lifecycle{state: state()}
Functions
Returns true if the worker can accept new work.
A worker can accept work only when in the :running state.
Returns the current state.
Examples
Lifecycle.current(lifecycle)
#=> :running
@spec new() :: t()
Creates a new lifecycle in the :created state.
Examples
lifecycle = Lifecycle.new()
lifecycle.state
#=> :created
Returns true if in :running state.
Returns true if in :stopped state.
Transitions to a new state if the transition is valid.
Returns {:ok, lifecycle} if the transition is valid,
or {:error, reason} if the transition is not allowed.
Examples
{:ok, lifecycle} = Lifecycle.transition(lifecycle, :starting)
{:error, {:invalid_transition, :created, :running}} = Lifecycle.transition(lifecycle, :running)
Transitions to a new state, raising on invalid transition.
Examples
lifecycle = Lifecycle.transition!(lifecycle, :starting)