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 ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Action ¶
type Action struct { // 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 // 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 ¶
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 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 ¶
NewPipeline creates a new pipeline instance with the given list of actions.
func (*Pipeline) Execute ¶
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 call fail, 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.