2019-01-12 22:45:25 -06:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2020-02-07 00:17:58 -06:00
|
|
|
"context"
|
2019-01-12 22:45:25 -06:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Warning that implements `error` but safe to ignore
|
|
|
|
type Warning struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error the contract for error
|
|
|
|
func (w Warning) Error() string {
|
|
|
|
return w.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warningf create a warning
|
|
|
|
func Warningf(format string, args ...interface{}) Warning {
|
|
|
|
w := Warning{
|
|
|
|
Message: fmt.Sprintf(format, args...),
|
|
|
|
}
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Executor define contract for the steps of a workflow
|
2020-02-07 00:17:58 -06:00
|
|
|
type Executor func(ctx context.Context) error
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
// Conditional define contract for the conditional predicate
|
2020-02-07 00:17:58 -06:00
|
|
|
type Conditional func(ctx context.Context) bool
|
|
|
|
|
|
|
|
// NewInfoExecutor is an executor that logs messages
|
|
|
|
func NewInfoExecutor(format string, args ...interface{}) Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := Logger(ctx)
|
|
|
|
logger.Infof(format, args...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
// NewDebugExecutor is an executor that logs messages
|
|
|
|
func NewDebugExecutor(format string, args ...interface{}) Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := Logger(ctx)
|
|
|
|
logger.Debugf(format, args...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
// NewPipelineExecutor creates a new executor from a series of other executors
|
|
|
|
func NewPipelineExecutor(executors ...Executor) Executor {
|
2020-02-17 00:04:13 -06:00
|
|
|
if len(executors) == 0 {
|
2020-02-07 00:17:58 -06:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var rtn Executor
|
|
|
|
for _, executor := range executors {
|
|
|
|
if rtn == nil {
|
|
|
|
rtn = executor
|
|
|
|
} else {
|
|
|
|
rtn = rtn.Then(executor)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
return rtn
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConditionalExecutor creates a new executor based on conditions
|
|
|
|
func NewConditionalExecutor(conditional Conditional, trueExecutor Executor, falseExecutor Executor) Executor {
|
2020-02-07 00:17:58 -06:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
if conditional(ctx) {
|
2019-01-12 22:45:25 -06:00
|
|
|
if trueExecutor != nil {
|
2020-02-07 00:17:58 -06:00
|
|
|
return trueExecutor(ctx)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if falseExecutor != nil {
|
2020-02-07 00:17:58 -06:00
|
|
|
return falseExecutor(ctx)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewErrorExecutor creates a new executor that always errors out
|
|
|
|
func NewErrorExecutor(err error) Executor {
|
2020-02-07 00:17:58 -06:00
|
|
|
return func(ctx context.Context) error {
|
2019-01-12 22:45:25 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewParallelExecutor creates a new executor from a parallel of other executors
|
2022-02-25 12:47:16 -06:00
|
|
|
func NewParallelExecutor(parallel int, executors ...Executor) Executor {
|
2020-02-07 00:17:58 -06:00
|
|
|
return func(ctx context.Context) error {
|
2022-02-25 12:47:16 -06:00
|
|
|
work := make(chan Executor, len(executors))
|
|
|
|
errs := make(chan error, len(executors))
|
|
|
|
|
|
|
|
for i := 0; i < parallel; i++ {
|
|
|
|
go func(work <-chan Executor, errs chan<- error) {
|
|
|
|
for executor := range work {
|
|
|
|
errs <- executor(ctx)
|
2020-02-10 18:53:14 -06:00
|
|
|
}
|
2022-02-25 12:47:16 -06:00
|
|
|
}(work, errs)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2022-02-25 12:47:16 -06:00
|
|
|
for i := 0; i < len(executors); i++ {
|
|
|
|
work <- executors[i]
|
|
|
|
}
|
|
|
|
close(work)
|
|
|
|
|
2021-04-06 08:43:02 -05:00
|
|
|
// Executor waits all executors to cleanup these resources.
|
|
|
|
var firstErr error
|
2019-01-12 22:45:25 -06:00
|
|
|
for i := 0; i < len(executors); i++ {
|
2022-02-25 12:47:16 -06:00
|
|
|
err := <-errs
|
|
|
|
if firstErr == nil {
|
2021-04-06 08:43:02 -05:00
|
|
|
firstErr = err
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
|
|
|
}
|
2022-02-25 12:47:16 -06:00
|
|
|
|
2021-04-06 08:43:02 -05:00
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return firstErr
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then runs another executor if this executor succeeds
|
|
|
|
func (e Executor) Then(then Executor) Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
err := e(ctx)
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case Warning:
|
|
|
|
log.Warning(err.Error())
|
|
|
|
default:
|
2022-01-27 10:20:44 -06:00
|
|
|
return err
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 10:20:44 -06:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
return then(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If only runs this executor if conditional is true
|
|
|
|
func (e Executor) If(conditional Conditional) Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
if conditional(ctx) {
|
|
|
|
return e(ctx)
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
|
|
|
|
// IfNot only runs this executor if conditional is true
|
|
|
|
func (e Executor) IfNot(conditional Conditional) Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
if !conditional(ctx) {
|
|
|
|
return e(ctx)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IfBool only runs this executor if conditional is true
|
|
|
|
func (e Executor) IfBool(conditional bool) Executor {
|
|
|
|
return e.If(func(ctx context.Context) bool {
|
|
|
|
return conditional
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally adds an executor to run after other executor
|
|
|
|
func (e Executor) Finally(finally Executor) Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
err := e(ctx)
|
|
|
|
err2 := finally(ctx)
|
|
|
|
if err2 != nil {
|
|
|
|
return fmt.Errorf("Error occurred running finally: %v (original error: %v)", err2, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not return an inverted conditional
|
|
|
|
func (c Conditional) Not() Conditional {
|
|
|
|
return func(ctx context.Context) bool {
|
|
|
|
return !c(ctx)
|
|
|
|
}
|
|
|
|
}
|