2021-06-30 17:50:58 +03:00
|
|
|
package flow
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/client"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
|
|
|
"go.unistack.org/micro/v4/meter"
|
2023-07-29 00:40:58 +03:00
|
|
|
"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/tracer"
|
2021-06-30 17:50:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Options server struct
|
|
|
|
type Options struct {
|
|
|
|
// Context holds the external options and can be used for flow shutdown
|
|
|
|
Context context.Context
|
|
|
|
// Client holds the client.Client
|
|
|
|
Client client.Client
|
|
|
|
// Tracer holds the tracer
|
|
|
|
Tracer tracer.Tracer
|
|
|
|
// Logger holds the logger
|
|
|
|
Logger logger.Logger
|
|
|
|
// Meter holds the meter
|
|
|
|
Meter meter.Meter
|
|
|
|
// Store used for intermediate results
|
|
|
|
Store store.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewOptions returns new options struct with default or passed values
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewOptions(opts ...options.Option) Options {
|
2021-06-30 17:50:58 +03:00
|
|
|
options := Options{
|
|
|
|
Context: context.Background(),
|
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
|
|
|
Client: client.DefaultClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
|
|
|
// WorkflowOptions holds workflow options
|
|
|
|
type WorkflowOptions struct {
|
|
|
|
Context context.Context
|
2021-09-30 21:00:02 +03:00
|
|
|
ID string
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// ExecuteOptions holds execute options
|
2021-06-30 17:50:58 +03:00
|
|
|
type ExecuteOptions struct {
|
|
|
|
// Client holds the client.Client
|
|
|
|
Client client.Client
|
|
|
|
// Tracer holds the tracer
|
|
|
|
Tracer tracer.Tracer
|
|
|
|
// Logger holds the logger
|
|
|
|
Logger logger.Logger
|
|
|
|
// Meter holds the meter
|
|
|
|
Meter meter.Meter
|
|
|
|
// Context can be used to abort execution or pass additional opts
|
|
|
|
Context context.Context
|
|
|
|
// Start step
|
|
|
|
Start string
|
|
|
|
// Timeout for execution
|
|
|
|
Timeout time.Duration
|
2021-09-30 01:24:16 +03:00
|
|
|
// Reverse execution
|
|
|
|
Reverse bool
|
2021-07-16 00:17:16 +03:00
|
|
|
// Async enables async execution
|
|
|
|
Async bool
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
// Reverse says that dag must be run in reverse order
|
|
|
|
func Reverse(b bool) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, b, ".Reverse")
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
// Async says that caller does not wait for execution complete
|
|
|
|
func Async(b bool) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, b, ".Async")
|
2021-07-16 00:17:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// NewExecuteOptions create new ExecuteOptions struct
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewExecuteOptions(opts ...options.Option) ExecuteOptions {
|
2021-07-15 22:56:26 +03:00
|
|
|
options := ExecuteOptions{
|
|
|
|
Client: client.DefaultClient,
|
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
2021-06-30 17:50:58 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// StepOptions holds step options
|
2021-06-30 17:50:58 +03:00
|
|
|
type StepOptions struct {
|
|
|
|
Context context.Context
|
|
|
|
Fallback string
|
2021-09-30 21:00:02 +03:00
|
|
|
ID string
|
|
|
|
Requires []string
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// NewStepOptions create new StepOptions struct
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewStepOptions(opts ...options.Option) StepOptions {
|
2021-07-15 22:56:26 +03:00
|
|
|
options := StepOptions{
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
2021-06-30 17:50:58 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
// Requires specifies required steps
|
|
|
|
func Requires(steps ...string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, steps, ".Requires")
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
// Fallback set the step to run on error
|
|
|
|
func Fallback(step string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, step, ".Fallback")
|
2021-06-30 17:50:58 +03:00
|
|
|
}
|
|
|
|
}
|
2023-08-15 07:55:26 +03:00
|
|
|
|
|
|
|
// ID sets the step ID
|
|
|
|
func StepID(id string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, id, ".ID")
|
|
|
|
}
|
|
|
|
}
|