statemachine

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionNode

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

ActionNode represents a pipeline action

func NewActionNode

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

NewActionNode creates a new action node

func (*ActionNode) Accept

func (n *ActionNode) Accept(v Visitor, traversalCtx *TraversalContext) 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) 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 NewPipelineNodeInput

type NewPipelineNodeInput struct {
	Parent          Node
	Status          NodeStatus
	Path            string
	Dependencies    []string
	ApprovalPending bool
}

NewPipelineNodeInput is the input for creating a new pipeline node

type NewTaskNodeInput

type NewTaskNodeInput struct {
	Parent          Node
	Status          *NodeStatus
	Path            string
	Name            string
	Dependencies    []string
	ApprovalPending bool
}

NewTaskNodeInput defines the input for creating a new task node

type Node

type Node interface {
	Path() string
	Type() NodeType
	Status() NodeStatus
	Cancel() error
	GetStatusChanges() []NodeStatusChange
	GetDependencies() []string
	SetStatus(status NodeStatus) error
	Parent() Node
	ChildrenNodes() []Node
	GetInitialStatus() NodeStatus
	Accept(v Visitor, traversalCtx *TraversalContext) 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"
	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"
)

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) 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

type NodeStatusChange

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

NodeStatusChange represents a change in the status of a node

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

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(_ *ActionNode, _ *TraversalContext) error

VisitForAction visits an action node

func (*PartialVisitor) VisitForAny

func (v *PartialVisitor) VisitForAny(_ Node, _ *TraversalContext) error

VisitForAny visits all nodes

func (*PartialVisitor) VisitForPipeline

func (v *PartialVisitor) VisitForPipeline(_ *PipelineNode) error

VisitForPipeline visits a pipeline node

func (*PartialVisitor) VisitForStage

func (v *PartialVisitor) VisitForStage(_ *StageNode) error

VisitForStage visits a stage node

func (*PartialVisitor) VisitForTask

func (v *PartialVisitor) VisitForTask(_ *TaskNode, _ *TraversalContext) 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(v Visitor, traversalCtx *TraversalContext) 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 nodes children

func (*PipelineNode) Copy

func (n *PipelineNode) Copy() *PipelineNode

Copy returns a copy of the node

func (*PipelineNode) GetDependencies

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

GetDependencies returns the dependencies for this node

func (*PipelineNode) GetInitialStatus

func (n *PipelineNode) GetInitialStatus() NodeStatus

GetInitialStatus returns the initial status of the node

func (*PipelineNode) GetStatusChanges

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

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 PrettyPrintVisitor

type PrettyPrintVisitor struct {
	PartialVisitor
	// contains filtered or unexported fields
}

PrettyPrintVisitor prints the state of each node in the pipeline graph

func (*PrettyPrintVisitor) VisitForAction

func (v *PrettyPrintVisitor) VisitForAction(action *ActionNode, traversalCtx *TraversalContext) error

VisitForAction visits an action node

func (*PrettyPrintVisitor) VisitForPipeline

func (v *PrettyPrintVisitor) VisitForPipeline(pipeline *PipelineNode) error

VisitForPipeline visits a pipeline node

func (*PrettyPrintVisitor) VisitForStage

func (v *PrettyPrintVisitor) VisitForStage(stage *StageNode) error

VisitForStage visits a stage node

func (*PrettyPrintVisitor) VisitForTask

func (v *PrettyPrintVisitor) VisitForTask(task *TaskNode, _ *TraversalContext) error

VisitForTask visits a task node

type SimpleTraverser

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

SimpleTraverser will visit each node in the graph

func (*SimpleTraverser) Accept

func (t *SimpleTraverser) Accept(v Visitor) error

Accept accepts a visitor

type SimulatedTraverser

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

SimulatedTraverser is a traverser that simulates a traversal of the pipeline graph in the order that the nodes would be executed in a real pipeline run

func NewSimulatedTraverser

func NewSimulatedTraverser(stateMachine *StateMachine) *SimulatedTraverser

NewSimulatedTraverser creates a new simulated traverser

func (*SimulatedTraverser) Accept

func (t *SimulatedTraverser) Accept(v Visitor) error

Accept accepts a visitor and visits each node in the graph

type StageNode

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

StageNode represents a stage in a pipeline

func NewStageNode

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

NewStageNode creates a new stage node

func (*StageNode) Accept

func (n *StageNode) Accept(v Visitor, traversalCtx *TraversalContext) error

Accept a visitor

func (*StageNode) AddNestedPipelineNode

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

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 node's children

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) 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 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) GetTraversalContextForNode

func (s *StateMachine) GetTraversalContextForNode(nodePath string) (*TraversalContext, error)

GetTraversalContextForNode returns the traversal context for a given node

func (*StateMachine) Reset

func (s *StateMachine) Reset() error

Reset resets the status of each node in the state machine to CreatedNodeStatus

type TaskNode

type TaskNode struct {
	Name string
	// 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(v Visitor, traversalCtx *TraversalContext) 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) GetStatusChanges

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

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 TraversalContext

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

TraversalContext tracks state as a visitor traverses the pipeline graph

func (*TraversalContext) Copy

Copy creates a copy of the traversal context

func (*TraversalContext) VisitedActions

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

VisitedActions returns a list of visited actions

type Visitor

type Visitor interface {
	VisitForPipeline(pipeline *PipelineNode) error
	VisitForStage(stage *StageNode) error
	VisitForTask(task *TaskNode, traversalCtx *TraversalContext) error
	VisitForAction(action *ActionNode, traversalCtx *TraversalContext) error
	VisitForAny(node Node, traversalCtx *TraversalContext) 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