2021-04-27 08:32:47 +03:00
|
|
|
// Package flow is an interface used for saga pattern microservice workflow
|
2021-10-02 19:55:07 +03:00
|
|
|
package flow // import "go.unistack.org/micro/v3/flow"
|
2021-02-14 23:47:11 +03:00
|
|
|
|
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
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2021-06-30 17:50:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-03-22 14:54:43 +03:00
|
|
|
// ErrStepNotExists returns when step not found
|
2021-06-30 17:50:58 +03:00
|
|
|
ErrStepNotExists = errors.New("step not exists")
|
2022-03-22 14:54:43 +03:00
|
|
|
// ErrMissingClient returns when client.Client is missing
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// Message used to transfer data between steps
|
2021-07-15 22:56:26 +03:00
|
|
|
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
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// Status contains step current status
|
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 (
|
2022-03-22 14:54:43 +03:00
|
|
|
// StatusPending step waiting to start
|
2021-07-15 22:56:26 +03:00
|
|
|
StatusPending Status = iota
|
2022-03-22 14:54:43 +03:00
|
|
|
// StatusRunning step is running
|
2021-07-15 22:56:26 +03:00
|
|
|
StatusRunning
|
2022-03-22 14:54:43 +03:00
|
|
|
// StatusFailure step competed with error
|
2021-07-15 22:56:26 +03:00
|
|
|
StatusFailure
|
2022-03-22 14:54:43 +03:00
|
|
|
// StatusSuccess step completed without error
|
2021-07-15 22:56:26 +03:00
|
|
|
StatusSuccess
|
2022-03-22 14:54:43 +03:00
|
|
|
// StatusAborted step aborted while it running
|
2021-07-15 22:56:26 +03:00
|
|
|
StatusAborted
|
2022-03-22 14:54:43 +03:00
|
|
|
// StatusSuspend step suspended
|
2021-07-15 22:56:26 +03:00
|
|
|
StatusSuspend
|
|
|
|
)
|
|
|
|
|
2021-07-16 00:17:16 +03:00
|
|
|
var (
|
2022-03-22 14:54:43 +03:00
|
|
|
// StatusString contains map status => string
|
2021-07-16 00:17:16 +03:00
|
|
|
StatusString = map[Status]string{
|
|
|
|
StatusPending: "StatusPending",
|
|
|
|
StatusRunning: "StatusRunning",
|
|
|
|
StatusFailure: "StatusFailure",
|
|
|
|
StatusSuccess: "StatusSuccess",
|
|
|
|
StatusAborted: "StatusAborted",
|
|
|
|
StatusSuspend: "StatusSuspend",
|
|
|
|
}
|
2022-03-22 14:54:43 +03:00
|
|
|
// StringStatus contains map string => status
|
2021-07-16 00:17:16 +03:00
|
|
|
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
|
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
|
|
|
|
)
|
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// RegisterStep register own step with workflow
|
2021-07-16 00:17:16 +03:00
|
|
|
func RegisterStep(step Step) {
|
|
|
|
flowMu.Lock()
|
|
|
|
steps, _ := atomicSteps.Load().([]Step)
|
|
|
|
atomicSteps.Store(append(steps, step))
|
|
|
|
flowMu.Unlock()
|
|
|
|
}
|