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-30 17:50:58 +03:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-07-16 00:17:16 +03:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2021-07-15 22:56:26 +03:00
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2021-06-30 17:50:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrStepNotExists = errors.New("step not exists")
|
2021-07-15 22:56:26 +03:00
|
|
|
ErrMissingClient = errors.New("client not set")
|
2021-06-30 17:50:58 +03:00
|
|
|
)
|
|
|
|
|
2021-07-15 22:56:26 +03:00
|
|
|
// RawMessage is a raw encoded JSON value.
|
|
|
|
// It implements Marshaler and Unmarshaler and can be used to delay decoding or precompute a encoding.
|
|
|
|
type RawMessage []byte
|
|
|
|
|
|
|
|
// MarshalJSON returns m as the JSON encoding of m.
|
|
|
|
func (m *RawMessage) MarshalJSON() ([]byte, error) {
|
|
|
|
if m == nil {
|
|
|
|
return []byte("null"), nil
|
|
|
|
}
|
|
|
|
return *m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON sets *m to a copy of data.
|
|
|
|
func (m *RawMessage) UnmarshalJSON(data []byte) error {
|
|
|
|
if m == nil {
|
|
|
|
return errors.New("RawMessage UnmarshalJSON on nil pointer")
|
|
|
|
}
|
|
|
|
*m = append((*m)[0:0], data...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
Header metadata.Metadata
|
|
|
|
Body RawMessage
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:50:58 +03:00
|
|
|
// Step represents dedicated workflow step
|
2021-02-14 23:47:11 +03:00
|
|
|
type Step interface {
|
2021-06-30 17:50:58 +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-30 17:50:58 +03:00
|
|
|
// Execute step run
|
2021-07-15 22:56:26 +03:00
|
|
|
Execute(ctx context.Context, req *Message, opts ...ExecuteOption) (*Message, error)
|
2021-06-30 17:50:58 +03:00
|
|
|
// Requires returns dependent steps
|
|
|
|
Requires() []string
|
|
|
|
// Options returns step options
|
|
|
|
Options() StepOptions
|
|
|
|
// Require add required steps
|
|
|
|
Require(steps ...Step) error
|
|
|
|
// String
|
|
|
|
String() string
|
2021-07-15 22:56:26 +03:00
|
|
|
// GetStatus returns step status
|
|
|
|
GetStatus() Status
|
|
|
|
// SetStatus sets the step status
|
|
|
|
SetStatus(Status)
|
|
|
|
// Request returns step request message
|
|
|
|
Request() *Message
|
|
|
|
// Response returns step response message
|
|
|
|
Response() *Message
|
2021-02-14 23:47:11 +03:00
|
|
|
}
|
2021-04-27 08:32:47 +03:00
|
|
|
|
2021-07-15 22:56:26 +03:00
|
|
|
type Status int
|
|
|
|
|
2021-07-16 00:17:16 +03:00
|
|
|
func (status Status) String() string {
|
|
|
|
return StatusString[status]
|
|
|
|
}
|
|
|
|
|
2021-07-15 22:56:26 +03:00
|
|
|
const (
|
|
|
|
StatusPending Status = iota
|
|
|
|
StatusRunning
|
|
|
|
StatusFailure
|
|
|
|
StatusSuccess
|
|
|
|
StatusAborted
|
|
|
|
StatusSuspend
|
|
|
|
)
|
|
|
|
|
2021-07-16 00:17:16 +03:00
|
|
|
var (
|
|
|
|
StatusString = map[Status]string{
|
|
|
|
StatusPending: "StatusPending",
|
|
|
|
StatusRunning: "StatusRunning",
|
|
|
|
StatusFailure: "StatusFailure",
|
|
|
|
StatusSuccess: "StatusSuccess",
|
|
|
|
StatusAborted: "StatusAborted",
|
|
|
|
StatusSuspend: "StatusSuspend",
|
|
|
|
}
|
|
|
|
StringStatus = map[string]Status{
|
|
|
|
"StatusPending": StatusPending,
|
|
|
|
"StatusRunning": StatusRunning,
|
|
|
|
"StatusFailure": StatusFailure,
|
|
|
|
"StatusSuccess": StatusSuccess,
|
|
|
|
"StatusAborted": StatusAborted,
|
|
|
|
"StatusSuspend": StatusSuspend,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-06-30 17:50:58 +03:00
|
|
|
// Workflow contains all steps to execute
|
2021-04-27 08:32:47 +03:00
|
|
|
type Workflow interface {
|
2021-06-30 17:50:58 +03:00
|
|
|
// ID returns id of the workflow
|
|
|
|
ID() string
|
|
|
|
// Execute workflow with args, return execution id and error
|
2021-07-15 22:56:26 +03:00
|
|
|
Execute(ctx context.Context, req *Message, opts ...ExecuteOption) (string, error)
|
2021-06-30 17:50:58 +03:00
|
|
|
// RemoveSteps remove steps from workflow
|
2021-07-15 22:56:26 +03:00
|
|
|
RemoveSteps(steps ...Step) error
|
2021-06-30 17:50:58 +03:00
|
|
|
// AppendSteps append steps to workflow
|
2021-07-15 22:56:26 +03:00
|
|
|
AppendSteps(steps ...Step) error
|
|
|
|
// Status returns workflow status
|
|
|
|
Status() Status
|
|
|
|
// Steps returns steps slice where parallel steps returned on the same level
|
|
|
|
Steps() ([][]Step, error)
|
2021-07-16 00:17:16 +03:00
|
|
|
// Suspend suspends execution
|
2021-08-20 22:40:48 +03:00
|
|
|
Suspend(ctx context.Context, id string) error
|
2021-07-16 00:17:16 +03:00
|
|
|
// Resume resumes execution
|
2021-08-20 22:40:48 +03:00
|
|
|
Resume(ctx context.Context, id string) error
|
2021-07-16 00:17:16 +03:00
|
|
|
// Abort abort execution
|
2021-08-20 22:40:48 +03:00
|
|
|
Abort(ctx context.Context, id string) error
|
2021-04-27 08:32:47 +03:00
|
|
|
}
|
|
|
|
|
2021-06-30 17:50:58 +03:00
|
|
|
// Flow the base interface to interact with workflows
|
2021-04-27 08:32:47 +03:00
|
|
|
type Flow interface {
|
2021-06-30 17:50:58 +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
|
|
|
}
|
2021-07-16 00:17:16 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
flowMu sync.Mutex
|
|
|
|
atomicSteps atomic.Value
|
|
|
|
)
|
|
|
|
|
|
|
|
func RegisterStep(step Step) {
|
|
|
|
flowMu.Lock()
|
|
|
|
steps, _ := atomicSteps.Load().([]Step)
|
|
|
|
atomicSteps.Store(append(steps, step))
|
|
|
|
flowMu.Unlock()
|
|
|
|
}
|