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"
|
|
|
|
"go.unistack.org/micro/v4/store"
|
|
|
|
"go.unistack.org/micro/v4/tracer"
|
2021-06-30 17:50:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Option func
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logger sets the logger option
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Meter sets the meter option
|
|
|
|
func Meter(m meter.Meter) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client to use for sync/async communication
|
|
|
|
func Client(c client.Client) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Client = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context specifies a context for the service.
|
|
|
|
// Can be used to signal shutdown of the flow
|
2022-01-10 16:47:37 +03:00
|
|
|
// or can be used for extra option values.
|
2021-06-30 17:50:58 +03:00
|
|
|
func Context(ctx context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracer mechanism for distributed tracking
|
|
|
|
func Tracer(t tracer.Tracer) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tracer = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store used for intermediate results
|
|
|
|
func Store(s store.Store) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Store = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// WorkflowOption func signature
|
2021-06-30 17:50:58 +03:00
|
|
|
type WorkflowOption func(*WorkflowOptions)
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// WorkflowID set workflow id
|
|
|
|
func WorkflowID(id string) WorkflowOption {
|
|
|
|
return func(o *WorkflowOptions) {
|
|
|
|
o.ID = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// ExecuteOption func signature
|
2021-06-30 17:50:58 +03:00
|
|
|
type ExecuteOption func(*ExecuteOptions)
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// ExecuteClient pass client.Client to ExecuteOption
|
2021-06-30 17:50:58 +03:00
|
|
|
func ExecuteClient(c client.Client) ExecuteOption {
|
|
|
|
return func(o *ExecuteOptions) {
|
|
|
|
o.Client = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// ExecuteTracer pass tracer.Tracer to ExecuteOption
|
2021-06-30 17:50:58 +03:00
|
|
|
func ExecuteTracer(t tracer.Tracer) ExecuteOption {
|
|
|
|
return func(o *ExecuteOptions) {
|
|
|
|
o.Tracer = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// ExecuteLogger pass logger.Logger to ExecuteOption
|
2021-06-30 17:50:58 +03:00
|
|
|
func ExecuteLogger(l logger.Logger) ExecuteOption {
|
|
|
|
return func(o *ExecuteOptions) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// ExecuteMeter pass meter.Meter to ExecuteOption
|
2021-06-30 17:50:58 +03:00
|
|
|
func ExecuteMeter(m meter.Meter) ExecuteOption {
|
|
|
|
return func(o *ExecuteOptions) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// ExecuteContext pass context.Context ot ExecuteOption
|
2021-06-30 17:50:58 +03:00
|
|
|
func ExecuteContext(ctx context.Context) ExecuteOption {
|
|
|
|
return func(o *ExecuteOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// ExecuteReverse says that dag must be run in reverse order
|
2021-06-30 17:50:58 +03:00
|
|
|
func ExecuteReverse(b bool) ExecuteOption {
|
|
|
|
return func(o *ExecuteOptions) {
|
|
|
|
o.Reverse = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// ExecuteTimeout pass timeout time.Duration for execution
|
2021-06-30 17:50:58 +03:00
|
|
|
func ExecuteTimeout(td time.Duration) ExecuteOption {
|
|
|
|
return func(o *ExecuteOptions) {
|
|
|
|
o.Timeout = td
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// ExecuteAsync says that caller does not wait for execution complete
|
2021-07-16 00:17:16 +03:00
|
|
|
func ExecuteAsync(b bool) ExecuteOption {
|
|
|
|
return func(o *ExecuteOptions) {
|
|
|
|
o.Async = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// NewExecuteOptions create new ExecuteOptions struct
|
2021-06-30 17:50:58 +03:00
|
|
|
func NewExecuteOptions(opts ...ExecuteOption) 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
|
|
|
// StepOption func signature
|
2021-06-30 17:50:58 +03:00
|
|
|
type StepOption func(*StepOptions)
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// NewStepOptions create new StepOptions struct
|
2021-06-30 17:50:58 +03:00
|
|
|
func NewStepOptions(opts ...StepOption) 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
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// StepID sets the step id for dag
|
2021-06-30 17:50:58 +03:00
|
|
|
func StepID(id string) StepOption {
|
|
|
|
return func(o *StepOptions) {
|
|
|
|
o.ID = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// StepRequires specifies required steps
|
2021-06-30 17:50:58 +03:00
|
|
|
func StepRequires(steps ...string) StepOption {
|
|
|
|
return func(o *StepOptions) {
|
|
|
|
o.Requires = steps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// StepFallback set the step to run on error
|
2021-06-30 17:50:58 +03:00
|
|
|
func StepFallback(step string) StepOption {
|
|
|
|
return func(o *StepOptions) {
|
|
|
|
o.Fallback = step
|
|
|
|
}
|
|
|
|
}
|