action

package
v0.0.0-...-101454b Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2017 License: Apache-2.0 Imports: 5 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

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

	CauseOf error
	// 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{}) 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

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