action

package
v0.0.0-...-9451de5 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2019 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package action implements atomic pipeline execution of actions. A pipeline is a set of actions that must be executed atomically: either all occur, or nothing occur.

The pipeline executor has two possible phases: forward and backward. Whenever a caller invoke the Execute method, the pipeline starts the forward phase. If any action fail, then the executor enter in the backward phase and rolls back all completed actions in that pipeline.

Each action contains two functions, matching the two executor phases: Forward and Backward.

A pipeline is composed of a list of actions. For each action execution, the executor will provide two possible contexts, based on the current phase of the executor:

  • in forward phase, the Forward function will receive a FWContext, that contains a list of parameters passed to executor in the Execute() call and the the result of the previous action (which will be nil for the first action in the pipeline);

  • in backward phase, the Backward function will receive a BWContext, that also contains the list of parameters given to the executor, but instead of the previous result, it receives the result of the forward phase of the current action.

Besides the Context, the Backward function will also receive the result of the Forward call.

For more details, check the documentation of the Execute method in the Pipeline type.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPipelineNoActions      = errors.New("No actions to execute.")
	ErrPipelineForwardMissing = errors.New("All actions must define the forward function.")
	ErrPipelineFewParameters  = errors.New("Not enough parameters to call Action.Forward.")
)

Functions

This section is empty.

Types

type Action

type Action struct {
	// Name is the action name. Used by the log.
	Name string

	// Function that will be invoked in the forward phase. This value
	// cannot be nil.
	Forward Forward

	// Function that will be invoked in the backward phase. For actions
	// that are not undoable, this attribute should be nil.
	Backward Backward

	// Minimum number of parameters that this action requires to run.
	MinParams int

	// Function that will be invoked after some failure occurured in the
	// Forward phase of this same action.
	OnError OnErrorFunc
	// contains filtered or unexported fields
}

Action defines actions that should be . It is composed of two functions: Forward and Backward.

Each action should do only one thing, and do it well. All information that is needed to undo the action should be returned by the Forward function.

type BWContext

type BWContext struct {
	// Result of the forward phase (for the current action).
	FWResult Result

	// List of parameters given to the executor.
	Params []interface{}
}

BWContext is the context used in calls to Backward functions (backward phase).

type Backward

type Backward func(context BWContext)

Backward is the function called by the pipeline executor when in the backward phase. It receives the context instance, that contains the list of parameters given to the pipeline executor and the result of the forward phase.

type FWContext

type FWContext struct {
	// Result of the previous action.
	Previous Result

	// List of parameters given to the executor.
	Params []interface{}
}

FWContext is the context used in calls to Forward functions (forward phase).

type Forward

type Forward func(context FWContext) (Result, error)

Forward is the function called by the pipeline executor in the forward phase. It receives a FWContext instance, that contains the list of parameters given to the pipeline executor and the result of the previous action in the pipeline (which will be nil for the first action in the pipeline).

type OnErrorFunc

type OnErrorFunc func(FWContext, error)

type Pipeline

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

Pipeline is a list of actions. Each pipeline is atomic: either all actions are successfully executed, or none of them are. For that, it's fundamental that all actions are really small and atomic.

func NewPipeline

func NewPipeline(actions ...*Action) *Pipeline

NewPipeline creates a new pipeline instance with the given list of actions.

func (*Pipeline) Execute

func (p *Pipeline) Execute(params ...interface{}) (err error)

Execute executes the pipeline.

The execution starts in the forward phase, calling the Forward function of all actions. If none of the Forward calls return error, the pipeline execution ends in the forward phase and is "committed".

If any of the Forward calls fails, the executor switches to the backward phase (roll back) and call the Backward function for each action completed. It does not call the Backward function of the action that has failed.

After rolling back all completed actions, it returns the original error returned by the action that failed.

func (*Pipeline) Result

func (p *Pipeline) Result() Result

Result returns the result of the last action.

type Result

type Result interface{}

Result is the value returned by Forward. It is used in the call of the next action, and also when rolling back the actions.

Jump to

Keyboard shortcuts

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