statemachine

package
v0.52.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MPL-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package statemachine contains the state machine logic for a pipeline in the form of an acyclic graph

Index

Constants

View Source
const RootPipelineNodePath = "pipeline"

RootPipelineNodePath is the path of the root pipeline node

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionNode

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

ActionNode represents a pipeline action

func NewActionNode

func NewActionNode(path string, parent Node, status NodeStatus) *ActionNode

NewActionNode creates a new action node

func (*ActionNode) Accept

func (n *ActionNode) Accept(ctx context.Context, v Visitor) error

Accept a visitor

func (*ActionNode) Cancel

func (n *ActionNode) Cancel() error

Cancel cancels the node

func (*ActionNode) ChildrenNodes

func (n *ActionNode) ChildrenNodes() []Node

ChildrenNodes returns a list of child nodes

func (*ActionNode) Copy

func (n *ActionNode) Copy() *ActionNode

Copy returns a copy of the node

func (*ActionNode) GetDependencies

func (n *ActionNode) GetDependencies() []string

func (*ActionNode) GetInitialStatus

func (n *ActionNode) GetInitialStatus() NodeStatus

func (*ActionNode) GetStatusChanges

func (n *ActionNode) GetStatusChanges() []NodeStatusChange

func (*ActionNode) Name

func (n *ActionNode) Name() string

func (*ActionNode) Parent

func (n *ActionNode) Parent() Node

func (*ActionNode) Path

func (n *ActionNode) Path() string

func (*ActionNode) SetStatus

func (n *ActionNode) SetStatus(status NodeStatus) error

func (*ActionNode) Status

func (n *ActionNode) Status() NodeStatus

func (*ActionNode) Type

func (n *ActionNode) Type() NodeType

type GetNodesInput

type GetNodesInput struct {
	Status    *NodeStatus
	NodeTypes []NodeType
}

GetNodesInput defines the input for GetNodes

type NestedPipelineNode added in v0.46.0

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

NestedPipelineNode represents a pipeline node

func NewNestedPipelineNode added in v0.46.0

func NewNestedPipelineNode(input *NewNestedPipelineNodeInput) *NestedPipelineNode

NewNestedPipelineNode creates a new pipeline node

func (*NestedPipelineNode) Accept added in v0.46.0

func (n *NestedPipelineNode) Accept(ctx context.Context, v Visitor) error

Accept a visitor

func (*NestedPipelineNode) Cancel added in v0.46.0

func (n *NestedPipelineNode) Cancel() error

Cancel cancels the node

func (*NestedPipelineNode) ChildrenNodes added in v0.46.0

func (n *NestedPipelineNode) ChildrenNodes() []Node

ChildrenNodes returns a list of this node's children

func (*NestedPipelineNode) Copy added in v0.46.0

Copy returns a copy of the node

func (*NestedPipelineNode) GetDependencies added in v0.46.0

func (n *NestedPipelineNode) GetDependencies() []string

GetDependencies returns the dependencies for this node

func (*NestedPipelineNode) GetInitialStatus added in v0.46.0

func (n *NestedPipelineNode) GetInitialStatus() NodeStatus

GetInitialStatus returns the initial status of the node

func (*NestedPipelineNode) GetOnErrorStrategy added in v0.46.0

func (n *NestedPipelineNode) GetOnErrorStrategy() OnErrorStrategy

GetOnErrorStrategy returns the on error behavior.

func (*NestedPipelineNode) GetStatusChanges added in v0.46.0

func (n *NestedPipelineNode) GetStatusChanges() []NodeStatusChange

func (*NestedPipelineNode) GetWhenCondition added in v0.46.0

func (n *NestedPipelineNode) GetWhenCondition() StageChildWhenCondition

GetWhenCondition returns the when condition for this node.

func (*NestedPipelineNode) HasUnfinishedDependencies added in v0.46.0

func (n *NestedPipelineNode) HasUnfinishedDependencies() bool

HasUnfinishedDependencies returns true if the node has unfinished dependencies

func (*NestedPipelineNode) Name added in v0.46.0

func (n *NestedPipelineNode) Name() string

func (*NestedPipelineNode) Parent added in v0.46.0

func (n *NestedPipelineNode) Parent() Node

func (*NestedPipelineNode) Path added in v0.46.0

func (n *NestedPipelineNode) Path() string

func (*NestedPipelineNode) SetStatus added in v0.46.0

func (n *NestedPipelineNode) SetStatus(status NodeStatus) error

SetStatus sets the node status

func (*NestedPipelineNode) Status added in v0.46.0

func (n *NestedPipelineNode) Status() NodeStatus

func (*NestedPipelineNode) Type added in v0.46.0

func (n *NestedPipelineNode) Type() NodeType

type NewNestedPipelineNodeInput added in v0.46.0

type NewNestedPipelineNodeInput struct {
	Parent       Node
	OnError      OnErrorStrategy
	Status       NodeStatus
	Path         string
	Dependencies []string
	When         StageChildWhenCondition
}

NewNestedPipelineNodeInput is the input for creating a new pipeline node

type NewPipelineNodeInput

type NewPipelineNodeInput struct {
	Status NodeStatus
	Path   string
}

NewPipelineNodeInput is the input for creating a new pipeline node

type NewTaskNodeInput

type NewTaskNodeInput struct {
	Parent       Node
	Status       *NodeStatus
	OnError      OnErrorStrategy
	Path         string
	Dependencies []string
	When         StageChildWhenCondition
}

NewTaskNodeInput defines the input for creating a new task node

type Node

type Node interface {
	Name() string
	Path() string
	Type() NodeType
	Status() NodeStatus
	Cancel() error
	GetStatusChanges() []NodeStatusChange
	SetStatus(status NodeStatus) error
	Parent() Node
	ChildrenNodes() []Node
	GetInitialStatus() NodeStatus
	Accept(ctx context.Context, v Visitor) error
	// contains filtered or unexported methods
}

Node represents a node in the pipeline graph

type NodeStatus

type NodeStatus string

NodeStatus constant defines the possible states for a node in a pipeline

const (
	CreatedNodeStatus         NodeStatus = "CREATED"
	QueuedNodeStatus          NodeStatus = "QUEUED"
	ApprovalPendingNodeStatus NodeStatus = "APPROVAL_PENDING"
	WaitingNodeStatus         NodeStatus = "WAITING"
	ReadyNodeStatus           NodeStatus = "READY"
	PendingNodeStatus         NodeStatus = "PENDING"
	RunningNodeStatus         NodeStatus = "RUNNING"
	FailedNodeStatus          NodeStatus = "FAILED"
	SkippedNodeStatus         NodeStatus = "SKIPPED"
	SucceededNodeStatus       NodeStatus = "SUCCEEDED"
	CanceledNodeStatus        NodeStatus = "CANCELED"
	CancelingNodeStatus       NodeStatus = "CANCELING"
	InitializingNodeStatus    NodeStatus = "INITIALIZING"
	FinalizingNodeStatus      NodeStatus = "FINALIZING"
	DeferredNodeStatus        NodeStatus = "DEFERRED"
)

NodeStatusConstants

func (NodeStatus) Equals

func (n NodeStatus) Equals(s NodeStatus) bool

Equals returns true if the status is equal to the given status

func (NodeStatus) IsDeferrableStatus added in v0.10.0

func (n NodeStatus) IsDeferrableStatus() bool

IsDeferrableStatus returns true if the status is deferrable.

func (NodeStatus) IsFinalStatus

func (n NodeStatus) IsFinalStatus() bool

IsFinalStatus returns true if the status is a final status

func (NodeStatus) IsRetryableStatus

func (n NodeStatus) IsRetryableStatus() bool

IsRetryableStatus returns true if status allows a node to be retried

func (NodeStatus) IsSkippableStatus added in v0.3.0

func (n NodeStatus) IsSkippableStatus() bool

IsSkippableStatus returns true is the status is skippable.

type NodeStatusChange

type NodeStatusChange struct {
	OldStatus NodeStatus
	NewStatus NodeStatus
	NodePath  string
	NodeType  NodeType
}

NodeStatusChange represents a change in the status of a node

func (NodeStatusChange) IsNestedPipelineChange added in v0.42.0

func (n NodeStatusChange) IsNestedPipelineChange() bool

IsNestedPipelineChange returns true if this is a nested pipeline status change

func (NodeStatusChange) IsRootPipelineChange added in v0.42.0

func (n NodeStatusChange) IsRootPipelineChange() bool

IsRootPipelineChange returns true if this is a root pipeline status change

type NodeType

type NodeType string

NodeType constant defines the types of pipeline graph nodes

const (
	PipelineNodeType NodeType = "PIPELINE"
	StageNodeType    NodeType = "STAGE"
	TaskNodeType     NodeType = "TASK"
	ActionNodeType   NodeType = "ACTION"
)

NodeType constants

func (NodeType) String added in v0.10.0

func (n NodeType) String() string

String returns the string representation of the node type

type OnErrorStrategy added in v0.7.0

type OnErrorStrategy string

OnErrorStrategy represents the behavior of a node when an error occurs

const (
	ContinueOnError OnErrorStrategy = "CONTINUE"
	FailOnError     OnErrorStrategy = "FAIL"
)

NodeOnError constants

func (OnErrorStrategy) Equals added in v0.7.0

func (n OnErrorStrategy) Equals(s OnErrorStrategy) bool

Equals returns true if the error behavior is equal to the given error behavior

type PartialVisitor

type PartialVisitor struct{}

PartialVisitor provides a noop implementation of the required visitor functions for visitors that only want to implement a subset of the functions

func (*PartialVisitor) VisitForAction

func (v *PartialVisitor) VisitForAction(_ context.Context, _ *ActionNode) error

VisitForAction visits an action node

func (*PartialVisitor) VisitForAny

func (v *PartialVisitor) VisitForAny(_ context.Context, _ Node) error

VisitForAny visits all nodes

func (*PartialVisitor) VisitForNestedPipeline added in v0.46.0

func (v *PartialVisitor) VisitForNestedPipeline(_ context.Context, _ *NestedPipelineNode) error

VisitForNestedPipeline visits a nested pipeline node

func (*PartialVisitor) VisitForPipeline

func (v *PartialVisitor) VisitForPipeline(_ context.Context, _ *PipelineNode) error

VisitForPipeline visits a pipeline node

func (*PartialVisitor) VisitForStage

func (v *PartialVisitor) VisitForStage(_ context.Context, _ *StageNode) error

VisitForStage visits a stage node

func (*PartialVisitor) VisitForTask

func (v *PartialVisitor) VisitForTask(_ context.Context, _ *TaskNode) error

VisitForTask visits a task node

type PipelineNode

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

PipelineNode represents a pipeline node

func NewPipelineNode

func NewPipelineNode(input *NewPipelineNodeInput) *PipelineNode

NewPipelineNode creates a new pipeline node

func (*PipelineNode) Accept

func (n *PipelineNode) Accept(ctx context.Context, v Visitor) error

Accept a visitor

func (*PipelineNode) AddStageNode

func (n *PipelineNode) AddStageNode(s *StageNode)

AddStageNode adds a new stage node to the pipeline

func (*PipelineNode) Cancel

func (n *PipelineNode) Cancel() error

Cancel cancels the node

func (*PipelineNode) ChildrenNodes

func (n *PipelineNode) ChildrenNodes() []Node

ChildrenNodes returns a list of this node's children

func (*PipelineNode) Copy

func (n *PipelineNode) Copy() *PipelineNode

Copy returns a copy of the node

func (*PipelineNode) GetDependencies

func (n *PipelineNode) GetDependencies() []string

func (*PipelineNode) GetInitialStatus

func (n *PipelineNode) GetInitialStatus() NodeStatus

func (*PipelineNode) GetStatusChanges

func (n *PipelineNode) GetStatusChanges() []NodeStatusChange

func (*PipelineNode) Name added in v0.10.0

func (n *PipelineNode) Name() string

func (*PipelineNode) Parent

func (n *PipelineNode) Parent() Node

func (*PipelineNode) Path

func (n *PipelineNode) Path() string

func (*PipelineNode) SetStatus

func (n *PipelineNode) SetStatus(status NodeStatus) error

SetStatus sets the node status

func (*PipelineNode) Status

func (n *PipelineNode) Status() NodeStatus

func (*PipelineNode) Type

func (n *PipelineNode) Type() NodeType

type SimpleTraverser

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

SimpleTraverser will visit each node in the graph

func NewSimpleTraverser added in v0.51.0

func NewSimpleTraverser(stateMachine *StateMachine) *SimpleTraverser

NewSimpleTraverser creates a new simple traverser for the given state machine.

func (*SimpleTraverser) Accept

func (t *SimpleTraverser) Accept(ctx context.Context, v Visitor) error

Accept accepts a visitor

type StageChildNode added in v0.7.0

type StageChildNode interface {
	Node
	HasUnfinishedDependencies() bool
	GetDependencies() []string
	GetOnErrorStrategy() OnErrorStrategy
	GetWhenCondition() StageChildWhenCondition
}

StageChildNode represents a child of a stage node, such as a task or nested pipeline.

type StageChildWhenCondition added in v0.46.0

type StageChildWhenCondition string

StageChildWhenCondition represents the when condition of a pipeline node

const (
	StageChildWhenConditionAuto         StageChildWhenCondition = "auto"
	StageChildWhenConditionManual       StageChildWhenCondition = "manual"
	StageChildWhenConditionStageFailure StageChildWhenCondition = "stage_failure"
)

StageChildWhenCondition constants

func (StageChildWhenCondition) IsValid added in v0.46.0

func (p StageChildWhenCondition) IsValid() bool

IsValid returns true if the when condition is valid

type StageNode

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

StageNode represents a stage in a pipeline

func NewStageNode

func NewStageNode(path string, parent Node, status NodeStatus, when StageWhenCondition) *StageNode

NewStageNode creates a new stage node

func (*StageNode) Accept

func (n *StageNode) Accept(ctx context.Context, v Visitor) error

Accept a visitor

func (*StageNode) AddNestedPipelineNode

func (n *StageNode) AddNestedPipelineNode(p *NestedPipelineNode)

AddNestedPipelineNode adds a nested pipeline

func (*StageNode) AddTaskNode

func (n *StageNode) AddTaskNode(t *TaskNode)

AddTaskNode adds a task node

func (*StageNode) Cancel

func (n *StageNode) Cancel() error

Cancel cancels the node

func (*StageNode) ChildrenNodes

func (n *StageNode) ChildrenNodes() []Node

ChildrenNodes returns a list of this stage node's children. Each child returned by this function is a StageChildNode, not just a Node, so it has a GetOnErrorStrategy method.

func (*StageNode) Copy

func (n *StageNode) Copy() *StageNode

Copy returns a copy of the node

func (*StageNode) GetDependencies

func (n *StageNode) GetDependencies() []string

func (*StageNode) GetInitialStatus

func (n *StageNode) GetInitialStatus() NodeStatus

func (*StageNode) GetStatusChanges

func (n *StageNode) GetStatusChanges() []NodeStatusChange

func (*StageNode) Name

func (n *StageNode) Name() string

func (*StageNode) Parent

func (n *StageNode) Parent() Node

func (*StageNode) Path

func (n *StageNode) Path() string

func (*StageNode) SetStatus

func (n *StageNode) SetStatus(status NodeStatus) error

SetStatus sets the node status

func (*StageNode) Status

func (n *StageNode) Status() NodeStatus

func (*StageNode) Type

func (n *StageNode) Type() NodeType

type StageWhenCondition added in v0.46.0

type StageWhenCondition string

StageWhenCondition represents the condition when a stage should run

const (
	StageWhenConditionAuto            StageWhenCondition = "auto"
	StageWhenConditionPipelineFailure StageWhenCondition = "pipeline_failure"
)

StageWhenCondition constants

func (StageWhenCondition) IsValid added in v0.46.0

func (p StageWhenCondition) IsValid() bool

IsValid returns true if the when condition is valid

type StateMachine

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

StateMachine encapsulates pipeline state updates

func New

func New(pipeline *PipelineNode) *StateMachine

New creates a new StateMachine instance

func (*StateMachine) Copy

func (s *StateMachine) Copy() *StateMachine

Copy creates a copy of the state machine

func (*StateMachine) GetNode

func (s *StateMachine) GetNode(path string) (Node, bool)

GetNode returns a node by path

func (*StateMachine) GetNodes

func (s *StateMachine) GetNodes(input *GetNodesInput) ([]Node, error)

GetNodes returns a list of nodes based on the provided node type and state

func (*StateMachine) GetPipelineNode

func (s *StateMachine) GetPipelineNode() *PipelineNode

GetPipelineNode returns the pipeline node

func (*StateMachine) GetStatusChanges

func (s *StateMachine) GetStatusChanges() ([]NodeStatusChange, error)

GetStatusChanges traverses the state machine and returns all status changes

func (*StateMachine) GetTraversalForNode added in v0.51.0

func (s *StateMachine) GetTraversalForNode(nodePath string) (*Traversal, error)

GetTraversalForNode returns the traversal context for a given node

type TaskNode

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

TaskNode represents a task in a pipeline

func NewTaskNode

func NewTaskNode(input *NewTaskNodeInput) *TaskNode

NewTaskNode creates a new task node

func (*TaskNode) Accept

func (n *TaskNode) Accept(ctx context.Context, v Visitor) error

Accept a visitor

func (*TaskNode) Actions

func (n *TaskNode) Actions() []*ActionNode

Actions returns a list of actions for this task

func (*TaskNode) AddActionNode

func (n *TaskNode) AddActionNode(a *ActionNode)

AddActionNode adds a new action node

func (*TaskNode) Cancel

func (n *TaskNode) Cancel() error

Cancel cancels the node

func (*TaskNode) ChildrenNodes

func (n *TaskNode) ChildrenNodes() []Node

ChildrenNodes returns a list of this nodes children

func (*TaskNode) Copy

func (n *TaskNode) Copy() *TaskNode

Copy returns a copy of the node

func (*TaskNode) GetDependencies

func (n *TaskNode) GetDependencies() []string

GetDependencies returns the dependencies for this node.

func (*TaskNode) GetInitialStatus

func (n *TaskNode) GetInitialStatus() NodeStatus

GetInitialStatus returns the initial status of the node

func (*TaskNode) GetOnErrorStrategy added in v0.7.0

func (n *TaskNode) GetOnErrorStrategy() OnErrorStrategy

GetOnErrorStrategy returns the on error behavior.

func (*TaskNode) GetStatusChanges

func (n *TaskNode) GetStatusChanges() []NodeStatusChange

func (*TaskNode) GetWhenCondition added in v0.46.0

func (n *TaskNode) GetWhenCondition() StageChildWhenCondition

GetWhenCondition returns the when condition for this node.

func (*TaskNode) HasUnfinishedDependencies added in v0.10.0

func (n *TaskNode) HasUnfinishedDependencies() bool

HasUnfinishedDependencies returns true if the node has unfinished dependencies

func (*TaskNode) Name

func (n *TaskNode) Name() string

func (*TaskNode) Parent

func (n *TaskNode) Parent() Node

func (*TaskNode) Path

func (n *TaskNode) Path() string

func (*TaskNode) SetStatus

func (n *TaskNode) SetStatus(status NodeStatus) error

SetStatus sets the node status

func (*TaskNode) Status

func (n *TaskNode) Status() NodeStatus

func (*TaskNode) Type

func (n *TaskNode) Type() NodeType

type Traversal added in v0.51.0

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

Traversal tracks state as a visitor traverses the pipeline graph

func (*Traversal) Empty added in v0.51.0

func (t *Traversal) Empty() bool

Empty returns true if no nodes have been visited

func (*Traversal) VisitedActions added in v0.51.0

func (t *Traversal) VisitedActions() []*ActionNode

VisitedActions returns a list of visited actions

func (*Traversal) VisitedNodes added in v0.51.0

func (t *Traversal) VisitedNodes(nodeTypes ...NodeType) []Node

VisitedNodes returns nodes that have been visited, optionally filtered by node type.

type Visitor

type Visitor interface {
	VisitForPipeline(ctx context.Context, pipeline *PipelineNode) error
	VisitForNestedPipeline(ctx context.Context, pipeline *NestedPipelineNode) error
	VisitForStage(ctx context.Context, stage *StageNode) error
	VisitForTask(ctx context.Context, task *TaskNode) error
	VisitForAction(ctx context.Context, action *ActionNode) error
	VisitForAny(ctx context.Context, node Node) error
}

Visitor is used to visit each node in the pipeline graph

Jump to

Keyboard shortcuts

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