2021-04-27 08:32:47 +03:00
|
|
|
// Package flow is an interface used for saga pattern microservice workflow
|
2021-02-14 23:47:11 +03:00
|
|
|
package flow
|
|
|
|
|
2021-06-28 22:39:50 +03:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Step represents dedicated workflow step
|
2021-02-14 23:47:11 +03:00
|
|
|
type Step interface {
|
2021-06-28 22:39:50 +03:00
|
|
|
// ID returns step id
|
|
|
|
ID() string
|
2021-04-27 08:32:47 +03:00
|
|
|
// Endpoint returns rpc endpoint service_name.service_method or broker topic
|
2021-02-14 23:47:11 +03:00
|
|
|
Endpoint() string
|
2021-06-28 22:39:50 +03:00
|
|
|
// Execute step run
|
|
|
|
Execute(ctx context.Context, req interface{}, opts ...ExecuteOption) error
|
|
|
|
// Requires returns dependent steps
|
|
|
|
Requires() []Step
|
|
|
|
// Options returns step options
|
|
|
|
Options() StepOptions
|
|
|
|
// Require add required steps
|
|
|
|
Require(steps ...Step) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStepOptions(opts ...StepOption) StepOptions {
|
|
|
|
options := StepOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if options.ID == "" {
|
|
|
|
options.ID = uuid.New().String()
|
|
|
|
}
|
|
|
|
return options
|
2021-02-14 23:47:11 +03:00
|
|
|
}
|
2021-04-27 08:32:47 +03:00
|
|
|
|
2021-06-28 22:39:50 +03:00
|
|
|
// Workflow contains all steps to execute
|
2021-04-27 08:32:47 +03:00
|
|
|
type Workflow interface {
|
2021-06-28 22:39:50 +03:00
|
|
|
// ID returns id of the workflow
|
|
|
|
ID() string
|
|
|
|
// Steps returns steps slice where parallel steps returned on the same level
|
2021-04-27 08:32:47 +03:00
|
|
|
Steps() [][]Step
|
2021-06-28 22:39:50 +03:00
|
|
|
// Execute workflow with args, return execution id and error
|
|
|
|
Execute(ctx context.Context, req interface{}, opts ...ExecuteOption) (string, error)
|
|
|
|
// RemoveSteps remove steps from workflow
|
|
|
|
RemoveSteps(ctx context.Context, steps ...Step) error
|
|
|
|
// AppendSteps append steps to workflow
|
|
|
|
AppendSteps(ctx context.Context, steps ...Step) error
|
2021-04-27 08:32:47 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 22:39:50 +03:00
|
|
|
// Flow the base interface to interact with workflows
|
2021-04-27 08:32:47 +03:00
|
|
|
type Flow interface {
|
2021-06-28 22:39:50 +03:00
|
|
|
// Options returns options
|
|
|
|
Options() Options
|
|
|
|
// Init initialize
|
|
|
|
Init(...Option) error
|
|
|
|
// WorkflowCreate creates new workflow with specific id and steps
|
|
|
|
WorkflowCreate(ctx context.Context, id string, steps ...Step) (Workflow, error)
|
|
|
|
// WorkflowSave saves workflow
|
|
|
|
WorkflowSave(ctx context.Context, w Workflow) error
|
|
|
|
// WorkflowLoad loads workflow with specific id
|
|
|
|
WorkflowLoad(ctx context.Context, id string) (Workflow, error)
|
|
|
|
// WorkflowList lists all workflows
|
|
|
|
WorkflowList(ctx context.Context) ([]Workflow, error)
|
2021-04-27 08:32:47 +03:00
|
|
|
}
|