Documentation ¶
Index ¶
- type AtomicStep
- type AtomicStepRegistry
- type AtomicWorkflow
- type Backward
- type Choreographer
- type Failure
- func NewFailedRollback(ctx context.Context, prevFailure *Failure, err error, report *StepReport) *Failure
- func NewFailedRun(ctx context.Context, prevSuccess *Success, err error, report *StepReport) *Failure
- func NewFailure(prevFailure *Failure, report *StepReport) *Failure
- func NewSkippedRollback(prevFailure *Failure, report *StepReport) *Failure
- type Forward
- type SagaRun
- type SagaUndo
- type Status
- type Step
- func (s *Step) FailedRollback(ctx context.Context, prevFailure *Failure, err error, report *StepReport) (WorkflowReport, error)
- func (s *Step) GetID() string
- func (s *Step) GetNext() Forward
- func (s *Step) GetPrev() Backward
- func (s *Step) RegisterSaga(run SagaRun, undo SagaUndo) *Step
- func (s *Step) Rollback(ctx context.Context, prevFailure *Failure) (WorkflowReport, error)
- func (s *Step) RollbackPrev(ctx context.Context, prevFailure *Failure, report *StepReport) (WorkflowReport, error)
- func (s *Step) Run(ctx context.Context, prevSuccess *Success) (WorkflowReport, error)
- func (s *Step) RunNext(ctx context.Context, prevSuccess *Success, report *StepReport) (WorkflowReport, error)
- func (s *Step) SetNext(next Forward)
- func (s *Step) SetPrev(prev Backward)
- func (s *Step) SkippedRollback(ctx context.Context, prevFailure *Failure, report *StepReport) (WorkflowReport, error)
- func (s *Step) SkippedRun(ctx context.Context, prevSuccess *Success, report *StepReport) (WorkflowReport, error)
- type StepActionType
- type StepIDs
- type StepRegistry
- type StepReport
- type Success
- type Workflow
- type WorkflowOption
- type WorkflowReport
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AtomicStep ¶
type AtomicStep interface { // GetID returns the step ID GetID() string Forward Backward Choreographer }
AtomicStep provides interface for an atomic state Note that an AtomicStep may skip rollback if that makes sense and in that case it is not Atomic in nature.
type AtomicStepRegistry ¶
type AtomicStepRegistry interface { // RegisterSteps registers a set of AtomicStep // steps argument is a map where key is the step identifier RegisterSteps(steps map[string]AtomicStep) AtomicStepRegistry // GetStep returns an AtomicStep from the registry given the id // If it doesn't exist, it returns nil. So the caller should handle the nil AtomicStep safely. GetStep(id string) AtomicStep // BuildWorkflow builds an AtomicWorkflow comprising the list of AtomicStep identified by ids BuildWorkflow(id string, stepIDs StepIDs) (AtomicWorkflow, error) }
AtomicStepRegistry is a registry of rollbackable steps
type AtomicWorkflow ¶
type AtomicWorkflow interface { // GetID returns the workflow ID GetID() string // Start starts the AtomicWorkflow execution Start(ctx context.Context) (WorkflowReport, error) // End performs cleanup after the AtomicWorkflow engine finish its execution End(ctx context.Context) }
AtomicWorkflow defines interface for a Workflow
type Backward ¶
type Backward interface { // Rollback defines the actions compensating the business logic executed in Run method // A step may skip rollback if that makes sense. In that case it would mean the AtomicStep is not Atomic in nature. Rollback(ctx context.Context, prevFailure *Failure) (WorkflowReport, error) }
Backward defines the methods to be executed to move the workflow backward on error
type Choreographer ¶
type Choreographer interface { SetNext(next Forward) SetPrev(prev Backward) GetNext() Forward GetPrev() Backward }
Choreographer interface defines the methods to support double link list of states This is needed to support Choreography execution of the Saga workflow
type Failure ¶
type Failure struct {
// contains filtered or unexported fields
}
Failure defines a failure event for a step
func NewFailedRollback ¶
func NewFailedRollback(ctx context.Context, prevFailure *Failure, err error, report *StepReport) *Failure
NewFailedRollback returns a Failure event when steps rollback action failed It sets the step's RollbackAction status as StatusFailed
func NewFailedRun ¶
func NewFailedRun(ctx context.Context, prevSuccess *Success, err error, report *StepReport) *Failure
NewFailedRun returns a Failure event to be used for first Rollback method It is used by a step to trigger its own rollback action It sets the step's RunAction status as StatusFailed
func NewFailure ¶
func NewFailure(prevFailure *Failure, report *StepReport) *Failure
NewFailure creates a Failure event for rollback action It is used by a step to trigger rollback action of the previous step when its own rollback succeeds. It sets the step's RollbackAction status as StatusSuccess.
func NewSkippedRollback ¶
func NewSkippedRollback(prevFailure *Failure, report *StepReport) *Failure
NewSkippedRollback creates a Failure event with StatusSkipped for RollbackAction This is a helper method to be used in rollback action when the rollback action is skipped.
type Forward ¶
type Forward interface { // Run runs the business logic to be performed in the AtomicStep Run(ctx context.Context, prevSuccess *Success) (WorkflowReport, error) }
Forward defines the methods to execute business logic of an AtomicStep and move the workflow forward
type SagaRun ¶
SagaRun is a func definition to contain the run logic
skipped return value denotes if the execution was skipped or not err return value denotes any error during execution (if any)
type SagaUndo ¶
SagaUndo is a func definition to contain the compensating logic
skipped return value denotes if the execution was skipped or not err return value denotes any error during execution (if any)
type Step ¶
Step is the kernel for AtomicStep implementation containing SagaRun and SagaUndo function It is to be used as inheritance by composition pattern by actual Step implementations If the saga methods are not registered, then Step will skip those operations during invocation of Run and Rollback Note that user may override the Run and Rollback methods in the actual implementation in order to change the control logic
func (*Step) FailedRollback ¶
func (s *Step) FailedRollback(ctx context.Context, prevFailure *Failure, err error, report *StepReport) (WorkflowReport, error)
FailedRollback is a helper method to report that current step's rollback has failed and trigger previous step's rollback It marks the current step RollbackAction as StatusFailed
func (*Step) RegisterSaga ¶
RegisterSaga register saga logic for run and undo in order to leverage the default controller logic for Run and Rollback This is just a helper function where user would like to use the default Run and Rollback logic. This method usage is optional and user is free to implement Run and Rollback method of AtomicStep as they wish.
func (*Step) Rollback ¶
Rollback implements Rollback controller logic for automa.AtomicStep interface This is a wrapper function to help simplify AtomicStep implementations Note that user may implement Rollback method in order to change the control logic as required.
func (*Step) RollbackPrev ¶
func (s *Step) RollbackPrev(ctx context.Context, prevFailure *Failure, report *StepReport) (WorkflowReport, error)
RollbackPrev is a helper method to report that current rollback step has been executed and trigger previous step's rollback It marks the current step as StatusFailed
func (*Step) Run ¶
Run implements Run controller logic for automa.AtomicStep interface This is a wrapper function to help simplify AtomicStep implementations Note that user may implement Run method in order to change the control logic as required.
func (*Step) RunNext ¶
func (s *Step) RunNext(ctx context.Context, prevSuccess *Success, report *StepReport) (WorkflowReport, error)
RunNext is a helper method to report that current step has been successful and trigger next step's execution It marks the current step as StatusSuccess
func (*Step) SetNext ¶
SetNext sets the next step of the Workflow to be able to move in the forward direction on success
func (*Step) SetPrev ¶
SetPrev sets the previous step of the Workflow to be able to move in the backward direction on error
func (*Step) SkippedRollback ¶
func (s *Step) SkippedRollback(ctx context.Context, prevFailure *Failure, report *StepReport) (WorkflowReport, error)
SkippedRollback is a helper method to report that current step's rollback has been skipped and trigger previous step's rollback It marks the current step as StatusSkipped
func (*Step) SkippedRun ¶
func (s *Step) SkippedRun(ctx context.Context, prevSuccess *Success, report *StepReport) (WorkflowReport, error)
SkippedRun is a helper method to report that current step has been skipped and trigger next step's execution It marks the current step as StatusSkipped
type StepActionType ¶
type StepActionType string
StepActionType defines the action taken by a step It is used as key for StepReport.Actions
const ( RunAction StepActionType = "run" RollbackAction StepActionType = "rollback" )
type StepRegistry ¶
type StepRegistry struct {
// contains filtered or unexported fields
}
StepRegistry is an implementation of AtomicStepRegistry interface
func NewStepRegistry ¶
func NewStepRegistry(logger *zap.Logger) *StepRegistry
NewStepRegistry returns an instance of StepRegistry that implements AtomicStepRegistry if logger is nil, it initializes itself with a NoOp logger
func (*StepRegistry) BuildWorkflow ¶
func (r *StepRegistry) BuildWorkflow(workflowID string, stepIDs StepIDs) (AtomicWorkflow, error)
BuildWorkflow is a helper method to build a Workflow from the given set of AtomicStep IDs
func (*StepRegistry) GetStep ¶
func (r *StepRegistry) GetStep(id string) AtomicStep
GetStep returns an AtomicStep by the id It returns error if a step cannot be found by the given ID
func (*StepRegistry) RegisterSteps ¶
func (r *StepRegistry) RegisterSteps(steps map[string]AtomicStep) AtomicStepRegistry
RegisterSteps is a helper method to register multiple AtomicSteps at a time
type StepReport ¶
type StepReport struct { StepID string `yaml:"step_id" json:"stepID"` Action StepActionType `yaml:"action" json:"action"` StartTime time.Time `yaml:"start_time" json:"startTime"` EndTime time.Time `yaml:"end_time" json:"endTime"` Status Status `yaml:"status" json:"status"` FailureReason errors.EncodedError `yaml:"reason" json:"reason"` Metadata map[string][]byte `yaml:"metadata" json:"metadata"` }
StepReport defines the report data model for each AtomicStep execution
func NewStepReport ¶
func NewStepReport(id string, action StepActionType) *StepReport
NewStepReport returns a new report with a given stepID
type Success ¶
type Success struct {
// contains filtered or unexported fields
}
Success defines a success event for a step
func NewSkippedRun ¶
func NewSkippedRun(prevSuccess *Success, report *StepReport) *Success
NewSkippedRun creates a Success event with StatusSkipped for RunAction This is a helper method to be used in run action when the run action is skipped.
func NewStartTrigger ¶
func NewStartTrigger(reports WorkflowReport) *Success
NewStartTrigger returns a Success event to be use for Run method It is used by the Workflow to trigger the execution of the first step
func NewSuccess ¶
func NewSuccess(prevSuccess *Success, report *StepReport) *Success
NewSuccess creates a Success event for run action It is used by a step to trigger run action of the nex step when its own run succeeds. It sets the step's RunAction status as StatusSuccess.
type Workflow ¶
type Workflow struct {
// contains filtered or unexported fields
}
Workflow implements AtomicWorkflow interface It implements a Saga workflow using Choreography execution pattern
In order to enable Choreography pattern it forms a double linked list of AtomicSteps to traverse 'Forward' on Success and 'Backward' on Failure
func NewWorkflow ¶
func NewWorkflow(id string, opts ...WorkflowOption) *Workflow
NewWorkflow returns an instance of WorkFlow that implements AtomicWorkflow interface
type WorkflowOption ¶
type WorkflowOption func(wf *Workflow)
WorkflowOption exposes "constructor with option" pattern for Workflow
func WithLogger ¶
func WithLogger(logger *zap.Logger) WorkflowOption
WithLogger allows Workflow to be initialized with a logger By default a Workflow is initialized with a NoOp logger
func WithSteps ¶
func WithSteps(steps ...AtomicStep) WorkflowOption
WithSteps allow Workflow to be initialized with the list of ordered steps
type WorkflowReport ¶
type WorkflowReport struct { WorkflowID string `yaml:"workflow_id" json:"workflowID"` StartTime time.Time `yaml:"start_time" json:"startTime"` EndTime time.Time `yaml:"end_time" json:"endTime"` Status Status `yaml:"status" json:"status"` StepSequence StepIDs `yaml:"step_sequence" json:"stepSequence"` StepReports []*StepReport `yaml:"step_reports" json:"stepReports"` }
WorkflowReport defines a map of StepReport with key as the step ID
func NewWorkflowReport ¶
func NewWorkflowReport(id string, steps StepIDs) *WorkflowReport
NewWorkflowReport returns an instance of WorkflowReport
func (*WorkflowReport) Append ¶
func (wfr *WorkflowReport) Append(stepReport *StepReport, action StepActionType, status Status)
Append appends the current report to the previous report It adds an end time and sets the status for the current report