2021-06-30 17:50:58 +03:00
|
|
|
package flow
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/silas/dag"
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/client"
|
|
|
|
"go.unistack.org/micro/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
|
|
|
"go.unistack.org/micro/v4/metadata"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
|
|
|
moptions "go.unistack.org/micro/v4/options"
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/store"
|
|
|
|
"go.unistack.org/micro/v4/util/id"
|
2021-06-30 17:50:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type microFlow struct {
|
|
|
|
opts Options
|
|
|
|
}
|
|
|
|
|
|
|
|
type microWorkflow struct {
|
2021-07-15 22:56:26 +03:00
|
|
|
opts Options
|
2021-09-30 01:24:16 +03:00
|
|
|
g *dag.AcyclicGraph
|
2021-07-15 22:56:26 +03:00
|
|
|
steps map[string]Step
|
2021-09-30 01:24:16 +03:00
|
|
|
id string
|
2021-07-15 22:56:26 +03:00
|
|
|
status Status
|
2021-09-30 01:24:16 +03:00
|
|
|
sync.RWMutex
|
|
|
|
init bool
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *microWorkflow) ID() string {
|
|
|
|
return w.id
|
|
|
|
}
|
|
|
|
|
2021-07-14 17:12:54 +03:00
|
|
|
func (w *microWorkflow) Steps() ([][]Step, error) {
|
|
|
|
return w.getSteps("", false)
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
2021-07-15 22:56:26 +03:00
|
|
|
func (w *microWorkflow) Status() Status {
|
|
|
|
return w.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *microWorkflow) AppendSteps(steps ...Step) error {
|
2021-07-14 17:12:54 +03:00
|
|
|
w.Lock()
|
|
|
|
|
|
|
|
for _, s := range steps {
|
|
|
|
w.steps[s.String()] = s
|
|
|
|
w.g.Add(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dst := range steps {
|
|
|
|
for _, req := range dst.Requires() {
|
|
|
|
src, ok := w.steps[req]
|
|
|
|
if !ok {
|
|
|
|
return ErrStepNotExists
|
|
|
|
}
|
|
|
|
w.g.Connect(dag.BasicEdge(src, dst))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.g.Validate(); err != nil {
|
|
|
|
w.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.g.TransitiveReduction()
|
|
|
|
|
|
|
|
w.Unlock()
|
|
|
|
|
2021-06-30 17:50:58 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-15 22:56:26 +03:00
|
|
|
func (w *microWorkflow) RemoveSteps(steps ...Step) error {
|
2021-07-14 17:12:54 +03:00
|
|
|
// TODO: handle case when some step requires or required by removed step
|
2021-06-30 17:50:58 +03:00
|
|
|
|
|
|
|
w.Lock()
|
2021-07-14 17:12:54 +03:00
|
|
|
|
|
|
|
for _, s := range steps {
|
|
|
|
delete(w.steps, s.String())
|
|
|
|
w.g.Remove(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dst := range steps {
|
|
|
|
for _, req := range dst.Requires() {
|
|
|
|
src, ok := w.steps[req]
|
|
|
|
if !ok {
|
|
|
|
return ErrStepNotExists
|
|
|
|
}
|
|
|
|
w.g.Connect(dag.BasicEdge(src, dst))
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 17:12:54 +03:00
|
|
|
if err := w.g.Validate(); err != nil {
|
|
|
|
w.Unlock()
|
|
|
|
return err
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
2021-07-14 17:12:54 +03:00
|
|
|
w.g.TransitiveReduction()
|
|
|
|
|
|
|
|
w.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *microWorkflow) getSteps(start string, reverse bool) ([][]Step, error) {
|
2021-06-30 17:50:58 +03:00
|
|
|
var steps [][]Step
|
2021-07-14 17:12:54 +03:00
|
|
|
var root dag.Vertex
|
|
|
|
var err error
|
|
|
|
|
2021-06-30 17:50:58 +03:00
|
|
|
fn := func(n dag.Vertex, idx int) error {
|
|
|
|
if idx == 0 {
|
|
|
|
steps = make([][]Step, 1)
|
|
|
|
steps[0] = make([]Step, 0, 1)
|
|
|
|
} else if idx >= len(steps) {
|
|
|
|
tsteps := make([][]Step, idx+1)
|
|
|
|
copy(tsteps, steps)
|
|
|
|
steps = tsteps
|
|
|
|
steps[idx] = make([]Step, 0, 1)
|
|
|
|
}
|
|
|
|
steps[idx] = append(steps[idx], n.(Step))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-14 17:12:54 +03:00
|
|
|
if start != "" {
|
2021-06-30 17:50:58 +03:00
|
|
|
var ok bool
|
|
|
|
w.RLock()
|
2021-07-14 17:12:54 +03:00
|
|
|
root, ok = w.steps[start]
|
2021-06-30 17:50:58 +03:00
|
|
|
w.RUnlock()
|
|
|
|
if !ok {
|
2021-07-14 17:12:54 +03:00
|
|
|
return nil, ErrStepNotExists
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
root, err = w.g.Root()
|
|
|
|
if err != nil {
|
2021-07-14 17:12:54 +03:00
|
|
|
return nil, err
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
}
|
2021-07-14 17:12:54 +03:00
|
|
|
|
|
|
|
if reverse {
|
2021-06-30 17:50:58 +03:00
|
|
|
err = w.g.SortedReverseDepthFirstWalk([]dag.Vertex{root}, fn)
|
|
|
|
} else {
|
|
|
|
err = w.g.SortedDepthFirstWalk([]dag.Vertex{root}, fn)
|
|
|
|
}
|
2021-07-14 17:12:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return steps, nil
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:40:48 +03:00
|
|
|
func (w *microWorkflow) Abort(ctx context.Context, id string) error {
|
2022-11-07 14:53:58 +03:00
|
|
|
workflowStore := store.NewNamespaceStore(w.opts.Store, "workflows"+w.opts.Store.Options().Separator+id)
|
2021-07-16 00:17:16 +03:00
|
|
|
return workflowStore.Write(ctx, "status", &codec.Frame{Data: []byte(StatusAborted.String())})
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:40:48 +03:00
|
|
|
func (w *microWorkflow) Suspend(ctx context.Context, id string) error {
|
2022-11-07 14:53:58 +03:00
|
|
|
workflowStore := store.NewNamespaceStore(w.opts.Store, "workflows"+w.opts.Store.Options().Separator+id)
|
2021-07-16 00:17:16 +03:00
|
|
|
return workflowStore.Write(ctx, "status", &codec.Frame{Data: []byte(StatusSuspend.String())})
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:40:48 +03:00
|
|
|
func (w *microWorkflow) Resume(ctx context.Context, id string) error {
|
2022-11-07 14:53:58 +03:00
|
|
|
workflowStore := store.NewNamespaceStore(w.opts.Store, "workflows"+w.opts.Store.Options().Separator+id)
|
2021-07-16 00:17:16 +03:00
|
|
|
return workflowStore.Write(ctx, "status", &codec.Frame{Data: []byte(StatusRunning.String())})
|
|
|
|
}
|
2021-07-15 22:56:26 +03:00
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...options.Option) (string, error) {
|
2021-07-14 17:12:54 +03:00
|
|
|
w.Lock()
|
|
|
|
if !w.init {
|
|
|
|
if err := w.g.Validate(); err != nil {
|
|
|
|
w.Unlock()
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
w.g.TransitiveReduction()
|
|
|
|
w.init = true
|
|
|
|
}
|
|
|
|
w.Unlock()
|
|
|
|
|
2021-08-20 22:44:17 +03:00
|
|
|
eid, err := id.New()
|
2021-07-14 17:12:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-07-15 22:56:26 +03:00
|
|
|
|
2022-11-07 14:53:58 +03:00
|
|
|
stepStore := store.NewNamespaceStore(w.opts.Store, "steps"+w.opts.Store.Options().Separator+eid)
|
|
|
|
workflowStore := store.NewNamespaceStore(w.opts.Store, "workflows"+w.opts.Store.Options().Separator+eid)
|
2021-07-14 17:12:54 +03:00
|
|
|
|
|
|
|
options := NewExecuteOptions(opts...)
|
2021-07-15 22:56:26 +03:00
|
|
|
|
2021-07-14 17:12:54 +03:00
|
|
|
steps, err := w.getSteps(options.Start, options.Reverse)
|
2021-06-30 17:50:58 +03:00
|
|
|
if err != nil {
|
2021-07-16 00:17:16 +03:00
|
|
|
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusPending.String())}); werr != nil {
|
2021-07-15 22:56:26 +03:00
|
|
|
w.opts.Logger.Errorf(w.opts.Context, "store error: %v", werr)
|
|
|
|
}
|
2021-06-30 17:50:58 +03:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
cherr := make(chan error, 1)
|
2021-07-16 00:17:16 +03:00
|
|
|
chstatus := make(chan Status, 1)
|
2021-06-30 17:50:58 +03:00
|
|
|
|
|
|
|
nctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2021-07-16 00:17:16 +03:00
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
nopts := make([]moptions.Option, 0, len(opts)+5)
|
2021-07-15 22:56:26 +03:00
|
|
|
|
2021-07-14 17:12:54 +03:00
|
|
|
nopts = append(nopts,
|
2023-07-29 00:40:58 +03:00
|
|
|
moptions.Client(w.opts.Client),
|
|
|
|
moptions.Tracer(w.opts.Tracer),
|
|
|
|
moptions.Logger(w.opts.Logger),
|
|
|
|
moptions.Meter(w.opts.Meter),
|
2021-07-14 17:12:54 +03:00
|
|
|
)
|
|
|
|
nopts = append(nopts, opts...)
|
|
|
|
done := make(chan struct{})
|
2021-07-15 22:56:26 +03:00
|
|
|
|
2021-07-16 00:17:16 +03:00
|
|
|
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusRunning.String())}); werr != nil {
|
2021-07-15 22:56:26 +03:00
|
|
|
w.opts.Logger.Errorf(w.opts.Context, "store error: %v", werr)
|
2021-08-20 22:44:17 +03:00
|
|
|
return eid, werr
|
2021-07-15 22:56:26 +03:00
|
|
|
}
|
|
|
|
for idx := range steps {
|
|
|
|
for nidx := range steps[idx] {
|
|
|
|
cstep := steps[idx][nidx]
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, cstep.ID()+w.opts.Store.Options().Separator+"status", &codec.Frame{Data: []byte(StatusPending.String())}); werr != nil {
|
2021-08-20 22:44:17 +03:00
|
|
|
return eid, werr
|
2021-07-15 22:56:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:50:58 +03:00
|
|
|
go func() {
|
|
|
|
for idx := range steps {
|
|
|
|
for nidx := range steps[idx] {
|
2021-07-16 00:17:16 +03:00
|
|
|
wStatus := &codec.Frame{}
|
|
|
|
if werr := workflowStore.Read(w.opts.Context, "status", wStatus); werr != nil {
|
|
|
|
cherr <- werr
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if status := StringStatus[string(wStatus.Data)]; status != StatusRunning {
|
|
|
|
chstatus <- status
|
|
|
|
return
|
|
|
|
}
|
2021-07-14 17:12:54 +03:00
|
|
|
if w.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
w.opts.Logger.Tracef(nctx, "will be executed %v", steps[idx][nidx])
|
|
|
|
}
|
2021-07-15 22:56:26 +03:00
|
|
|
cstep := steps[idx][nidx]
|
2021-09-30 21:13:13 +03:00
|
|
|
// nolint: nestif
|
2021-07-15 22:56:26 +03:00
|
|
|
if len(cstep.Requires()) == 0 {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(step Step) {
|
|
|
|
defer wg.Done()
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, step.ID()+w.opts.Store.Options().Separator+"req", req); werr != nil {
|
2021-07-15 22:56:26 +03:00
|
|
|
cherr <- werr
|
|
|
|
return
|
|
|
|
}
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, step.ID()+w.opts.Store.Options().Separator+"status", &codec.Frame{Data: []byte(StatusRunning.String())}); werr != nil {
|
2021-07-15 22:56:26 +03:00
|
|
|
cherr <- werr
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rsp, serr := step.Execute(nctx, req, nopts...)
|
|
|
|
if serr != nil {
|
|
|
|
step.SetStatus(StatusFailure)
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, step.ID()+w.opts.Store.Options().Separator+"rsp", serr); werr != nil && w.opts.Logger.V(logger.ErrorLevel) {
|
2021-07-15 22:56:26 +03:00
|
|
|
w.opts.Logger.Errorf(ctx, "store write error: %v", werr)
|
|
|
|
}
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, step.ID()+w.opts.Store.Options().Separator+"status", &codec.Frame{Data: []byte(StatusFailure.String())}); werr != nil && w.opts.Logger.V(logger.ErrorLevel) {
|
2021-07-15 22:56:26 +03:00
|
|
|
w.opts.Logger.Errorf(ctx, "store write error: %v", werr)
|
|
|
|
}
|
|
|
|
cherr <- serr
|
|
|
|
return
|
2021-09-28 23:43:43 +03:00
|
|
|
}
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, step.ID()+w.opts.Store.Options().Separator+"rsp", rsp); werr != nil {
|
2021-09-28 23:43:43 +03:00
|
|
|
w.opts.Logger.Errorf(ctx, "store write error: %v", werr)
|
|
|
|
cherr <- werr
|
|
|
|
return
|
|
|
|
}
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, step.ID()+w.opts.Store.Options().Separator+"status", &codec.Frame{Data: []byte(StatusSuccess.String())}); werr != nil {
|
2021-09-28 23:43:43 +03:00
|
|
|
w.opts.Logger.Errorf(ctx, "store write error: %v", werr)
|
|
|
|
cherr <- werr
|
|
|
|
return
|
2021-07-15 22:56:26 +03:00
|
|
|
}
|
|
|
|
}(cstep)
|
|
|
|
wg.Wait()
|
|
|
|
} else {
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, cstep.ID()+w.opts.Store.Options().Separator+"req", req); werr != nil {
|
2021-07-15 22:56:26 +03:00
|
|
|
cherr <- werr
|
|
|
|
return
|
|
|
|
}
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, cstep.ID()+w.opts.Store.Options().Separator+"status", &codec.Frame{Data: []byte(StatusRunning.String())}); werr != nil {
|
2021-07-15 22:56:26 +03:00
|
|
|
cherr <- werr
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rsp, serr := cstep.Execute(nctx, req, nopts...)
|
|
|
|
if serr != nil {
|
|
|
|
cstep.SetStatus(StatusFailure)
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, cstep.ID()+w.opts.Store.Options().Separator+"rsp", serr); werr != nil && w.opts.Logger.V(logger.ErrorLevel) {
|
2021-07-15 22:56:26 +03:00
|
|
|
w.opts.Logger.Errorf(ctx, "store write error: %v", werr)
|
|
|
|
}
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, cstep.ID()+w.opts.Store.Options().Separator+"status", &codec.Frame{Data: []byte(StatusFailure.String())}); werr != nil && w.opts.Logger.V(logger.ErrorLevel) {
|
2021-07-15 22:56:26 +03:00
|
|
|
w.opts.Logger.Errorf(ctx, "store write error: %v", werr)
|
|
|
|
}
|
2021-07-14 17:12:54 +03:00
|
|
|
cherr <- serr
|
2021-07-15 22:56:26 +03:00
|
|
|
return
|
2021-09-28 23:43:43 +03:00
|
|
|
}
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, cstep.ID()+w.opts.Store.Options().Separator+"rsp", rsp); werr != nil {
|
2021-09-28 23:43:43 +03:00
|
|
|
w.opts.Logger.Errorf(ctx, "store write error: %v", werr)
|
|
|
|
cherr <- werr
|
|
|
|
return
|
|
|
|
}
|
2022-11-07 14:53:58 +03:00
|
|
|
if werr := stepStore.Write(ctx, cstep.ID()+w.opts.Store.Options().Separator+"status", &codec.Frame{Data: []byte(StatusSuccess.String())}); werr != nil {
|
2021-09-28 23:43:43 +03:00
|
|
|
cherr <- werr
|
|
|
|
return
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
2021-07-15 22:56:26 +03:00
|
|
|
}
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
}
|
2021-07-14 17:12:54 +03:00
|
|
|
close(done)
|
2021-06-30 17:50:58 +03:00
|
|
|
}()
|
|
|
|
|
2021-07-16 00:17:16 +03:00
|
|
|
if options.Async {
|
2021-08-20 22:44:17 +03:00
|
|
|
return eid, nil
|
2021-07-16 00:17:16 +03:00
|
|
|
}
|
|
|
|
|
2021-07-14 17:12:54 +03:00
|
|
|
logger.Tracef(ctx, "wait for finish or error")
|
|
|
|
select {
|
|
|
|
case <-nctx.Done():
|
|
|
|
err = nctx.Err()
|
|
|
|
case cerr := <-cherr:
|
|
|
|
err = cerr
|
|
|
|
case <-done:
|
|
|
|
close(cherr)
|
2021-07-16 00:17:16 +03:00
|
|
|
case <-chstatus:
|
|
|
|
close(chstatus)
|
2021-08-20 22:44:17 +03:00
|
|
|
return eid, nil
|
2021-07-14 17:12:54 +03:00
|
|
|
}
|
2021-06-30 17:50:58 +03:00
|
|
|
|
2021-07-15 22:56:26 +03:00
|
|
|
switch {
|
|
|
|
case nctx.Err() != nil:
|
2021-07-16 00:17:16 +03:00
|
|
|
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusAborted.String())}); werr != nil {
|
2021-07-15 22:56:26 +03:00
|
|
|
w.opts.Logger.Errorf(w.opts.Context, "store error: %v", werr)
|
|
|
|
}
|
|
|
|
case err == nil:
|
2021-07-16 00:17:16 +03:00
|
|
|
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusSuccess.String())}); werr != nil {
|
2021-07-15 22:56:26 +03:00
|
|
|
w.opts.Logger.Errorf(w.opts.Context, "store error: %v", werr)
|
|
|
|
}
|
|
|
|
case err != nil:
|
2021-07-16 00:17:16 +03:00
|
|
|
if werr := workflowStore.Write(w.opts.Context, "status", &codec.Frame{Data: []byte(StatusFailure.String())}); werr != nil {
|
2021-07-15 22:56:26 +03:00
|
|
|
w.opts.Logger.Errorf(w.opts.Context, "store error: %v", werr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:44:17 +03:00
|
|
|
return eid, err
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// NewFlow create new flow
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewFlow(opts ...options.Option) Flow {
|
2021-06-30 17:50:58 +03:00
|
|
|
options := NewOptions(opts...)
|
|
|
|
return µFlow{opts: options}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *microFlow) Options() Options {
|
|
|
|
return f.opts
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (f *microFlow) Init(opts ...options.Option) error {
|
2021-06-30 17:50:58 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&f.opts)
|
|
|
|
}
|
|
|
|
if err := f.opts.Client.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := f.opts.Tracer.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := f.opts.Logger.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := f.opts.Meter.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := f.opts.Store.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *microFlow) WorkflowList(ctx context.Context) ([]Workflow, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *microFlow) WorkflowCreate(ctx context.Context, id string, steps ...Step) (Workflow, error) {
|
|
|
|
w := µWorkflow{opts: f.opts, id: id, g: &dag.AcyclicGraph{}, steps: make(map[string]Step, len(steps))}
|
|
|
|
|
|
|
|
for _, s := range steps {
|
|
|
|
w.steps[s.String()] = s
|
|
|
|
w.g.Add(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dst := range steps {
|
|
|
|
for _, req := range dst.Requires() {
|
|
|
|
src, ok := w.steps[req]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrStepNotExists
|
|
|
|
}
|
|
|
|
w.g.Connect(dag.BasicEdge(src, dst))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.g.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
w.g.TransitiveReduction()
|
|
|
|
|
|
|
|
w.init = true
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *microFlow) WorkflowRemove(ctx context.Context, id string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *microFlow) WorkflowSave(ctx context.Context, w Workflow) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *microFlow) WorkflowLoad(ctx context.Context, id string) (Workflow, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type microCallStep struct {
|
2021-09-30 01:24:16 +03:00
|
|
|
rsp *Message
|
|
|
|
req *Message
|
2021-06-30 17:50:58 +03:00
|
|
|
service string
|
|
|
|
method string
|
2021-09-30 21:00:02 +03:00
|
|
|
opts StepOptions
|
2021-07-15 22:56:26 +03:00
|
|
|
status Status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microCallStep) Request() *Message {
|
|
|
|
return s.req
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microCallStep) Response() *Message {
|
|
|
|
return s.rsp
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microCallStep) ID() string {
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microCallStep) Options() StepOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microCallStep) Endpoint() string {
|
|
|
|
return s.method
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microCallStep) Requires() []string {
|
|
|
|
return s.opts.Requires
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microCallStep) Require(steps ...Step) error {
|
|
|
|
for _, step := range steps {
|
|
|
|
s.opts.Requires = append(s.opts.Requires, step.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microCallStep) String() string {
|
|
|
|
if s.opts.ID != "" {
|
|
|
|
return s.opts.ID
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s.%s", s.service, s.method)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microCallStep) Name() string {
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microCallStep) Hashcode() interface{} {
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
2021-07-15 22:56:26 +03:00
|
|
|
func (s *microCallStep) GetStatus() Status {
|
|
|
|
return s.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microCallStep) SetStatus(status Status) {
|
|
|
|
s.status = status
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (s *microCallStep) Execute(ctx context.Context, req *Message, opts ...options.Option) (*Message, error) {
|
2021-06-30 17:50:58 +03:00
|
|
|
options := NewExecuteOptions(opts...)
|
|
|
|
if options.Client == nil {
|
2021-07-15 22:56:26 +03:00
|
|
|
return nil, ErrMissingClient
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
rsp := &codec.Frame{}
|
2023-07-29 00:40:58 +03:00
|
|
|
copts := []moptions.Option{client.Retries(0)}
|
2021-06-30 17:50:58 +03:00
|
|
|
if options.Timeout > 0 {
|
2021-09-28 23:43:43 +03:00
|
|
|
copts = append(copts,
|
2023-07-29 00:40:58 +03:00
|
|
|
client.RequestTimeout(options.Timeout),
|
|
|
|
client.DialTimeout(options.Timeout))
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
2021-07-15 22:56:26 +03:00
|
|
|
nctx := metadata.NewOutgoingContext(ctx, req.Header)
|
2021-09-28 23:43:43 +03:00
|
|
|
err := options.Client.Call(nctx, options.Client.NewRequest(s.service, s.method, &codec.Frame{Data: req.Body}), rsp, copts...)
|
2021-07-15 22:56:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
md, _ := metadata.FromOutgoingContext(nctx)
|
|
|
|
return &Message{Header: md, Body: rsp.Data}, err
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type microPublishStep struct {
|
2021-07-15 22:56:26 +03:00
|
|
|
req *Message
|
|
|
|
rsp *Message
|
2021-09-30 01:24:16 +03:00
|
|
|
topic string
|
2021-09-30 21:00:02 +03:00
|
|
|
opts StepOptions
|
2021-07-15 22:56:26 +03:00
|
|
|
status Status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microPublishStep) Request() *Message {
|
|
|
|
return s.req
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microPublishStep) Response() *Message {
|
|
|
|
return s.rsp
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microPublishStep) ID() string {
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microPublishStep) Options() StepOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microPublishStep) Endpoint() string {
|
|
|
|
return s.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microPublishStep) Requires() []string {
|
|
|
|
return s.opts.Requires
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microPublishStep) Require(steps ...Step) error {
|
|
|
|
for _, step := range steps {
|
|
|
|
s.opts.Requires = append(s.opts.Requires, step.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microPublishStep) String() string {
|
|
|
|
if s.opts.ID != "" {
|
|
|
|
return s.opts.ID
|
|
|
|
}
|
2021-09-28 23:43:43 +03:00
|
|
|
return s.topic
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microPublishStep) Name() string {
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microPublishStep) Hashcode() interface{} {
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
2021-07-15 22:56:26 +03:00
|
|
|
func (s *microPublishStep) GetStatus() Status {
|
|
|
|
return s.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *microPublishStep) SetStatus(status Status) {
|
|
|
|
s.status = status
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (s *microPublishStep) Execute(ctx context.Context, req *Message, opts ...options.Option) (*Message, error) {
|
2021-07-15 22:56:26 +03:00
|
|
|
return nil, nil
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// NewCallStep create new step with client.Call
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewCallStep(service string, name string, method string, opts ...options.Option) Step {
|
2021-06-30 17:50:58 +03:00
|
|
|
options := NewStepOptions(opts...)
|
2021-07-14 17:12:54 +03:00
|
|
|
return µCallStep{service: service, method: name + "." + method, opts: options}
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// NewPublishStep create new step with client.Publish
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewPublishStep(topic string, opts ...options.Option) Step {
|
2021-06-30 17:50:58 +03:00
|
|
|
options := NewStepOptions(opts...)
|
|
|
|
return µPublishStep{topic: topic, opts: options}
|
|
|
|
}
|