refactor logger (#2552)
This commit is contained in:
@@ -2,6 +2,7 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -130,6 +131,31 @@ func NewParallelExecutor(parallel int, executors ...Executor) Executor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewFieldExecutor(name string, value interface{}, exec Executor) Executor {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
return exec(WithLogger(ctx, Logger(ctx).WithField(name, value)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then runs another executor if this executor succeeds
|
||||||
|
func (e Executor) ThenError(then func(ctx context.Context, err error) error) Executor {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
err := e(ctx)
|
||||||
|
if err != nil {
|
||||||
|
switch err.(type) {
|
||||||
|
case Warning:
|
||||||
|
Logger(ctx).Warning(err.Error())
|
||||||
|
default:
|
||||||
|
return then(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
return then(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Then runs another executor if this executor succeeds
|
// Then runs another executor if this executor succeeds
|
||||||
func (e Executor) Then(then Executor) Executor {
|
func (e Executor) Then(then Executor) Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
@@ -149,6 +175,25 @@ func (e Executor) Then(then Executor) Executor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Then runs another executor if this executor succeeds
|
||||||
|
func (e Executor) OnError(then Executor) Executor {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
err := e(ctx)
|
||||||
|
if err != nil {
|
||||||
|
switch err.(type) {
|
||||||
|
case Warning:
|
||||||
|
Logger(ctx).Warning(err.Error())
|
||||||
|
default:
|
||||||
|
return errors.Join(err, then(ctx))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If only runs this executor if conditional is true
|
// If only runs this executor if conditional is true
|
||||||
func (e Executor) If(conditional Conditional) Executor {
|
func (e Executor) If(conditional Conditional) Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
postExecutor = postExecutor.Finally(func(ctx context.Context) error {
|
var stopContainerExecutor common.Executor = func(ctx context.Context) error {
|
||||||
jobError := common.JobError(ctx)
|
jobError := common.JobError(ctx)
|
||||||
var err error
|
var err error
|
||||||
if rc.Config.AutoRemove || jobError == nil {
|
if rc.Config.AutoRemove || jobError == nil {
|
||||||
@@ -108,18 +108,31 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
|
|||||||
logger.Errorf("Error while stop job container: %v", err)
|
logger.Errorf("Error while stop job container: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var setJobResultExecutor common.Executor = func(ctx context.Context) error {
|
||||||
|
jobError := common.JobError(ctx)
|
||||||
setJobResult(ctx, info, rc, jobError == nil)
|
setJobResult(ctx, info, rc, jobError == nil)
|
||||||
setJobOutputs(ctx, rc)
|
setJobOutputs(ctx, rc)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
return err
|
var setJobError = func(ctx context.Context, err error) error {
|
||||||
})
|
common.SetJobError(ctx, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
pipeline := make([]common.Executor, 0)
|
pipeline := make([]common.Executor, 0)
|
||||||
pipeline = append(pipeline, rc.InitializeNodeTool())
|
|
||||||
pipeline = append(pipeline, preSteps...)
|
pipeline = append(pipeline, preSteps...)
|
||||||
pipeline = append(pipeline, steps...)
|
pipeline = append(pipeline, steps...)
|
||||||
|
|
||||||
return common.NewPipelineExecutor(info.startContainer(), common.NewPipelineExecutor(pipeline...).
|
return common.NewPipelineExecutor(
|
||||||
|
common.NewFieldExecutor("step", "Set up job", common.NewFieldExecutor("stepid", []string{"--setup-job"},
|
||||||
|
common.NewPipelineExecutor(common.NewInfoExecutor("\u2B50 Run Set up job"), info.startContainer(), rc.InitializeNodeTool()).
|
||||||
|
Then(common.NewFieldExecutor("stepResult", model.StepStatusSuccess, common.NewInfoExecutor(" \u2705 Success - Set up job"))).
|
||||||
|
OnError(common.NewFieldExecutor("stepResult", model.StepStatusFailure, common.NewInfoExecutor(" \u274C Failure - Set up job")).ThenError(setJobError)))),
|
||||||
|
common.NewPipelineExecutor(pipeline...).
|
||||||
Finally(func(ctx context.Context) error { //nolint:contextcheck
|
Finally(func(ctx context.Context) error { //nolint:contextcheck
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
if ctx.Err() == context.Canceled {
|
if ctx.Err() == context.Canceled {
|
||||||
@@ -130,8 +143,13 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
|
|||||||
}
|
}
|
||||||
return postExecutor(ctx)
|
return postExecutor(ctx)
|
||||||
}).
|
}).
|
||||||
Finally(info.interpolateOutputs()).
|
Finally(common.NewFieldExecutor("step", "Complete job", common.NewFieldExecutor("stepid", []string{"--complete-job"},
|
||||||
Finally(info.closeContainer()))
|
common.NewInfoExecutor("\u2B50 Run Complete job").
|
||||||
|
Finally(stopContainerExecutor).
|
||||||
|
Finally(
|
||||||
|
info.interpolateOutputs().Finally(info.closeContainer()).Then(common.NewFieldExecutor("stepResult", model.StepStatusSuccess, common.NewInfoExecutor(" \u2705 Success - Complete job"))).
|
||||||
|
OnError(common.NewFieldExecutor("stepResult", model.StepStatusFailure, common.NewInfoExecutor(" \u274C Failure - Complete job"))),
|
||||||
|
)))).Finally(setJobResultExecutor))
|
||||||
}
|
}
|
||||||
|
|
||||||
func setJobResult(ctx context.Context, info jobInfo, rc *RunContext, success bool) {
|
func setJobResult(ctx context.Context, info jobInfo, rc *RunContext, success bool) {
|
||||||
|
|||||||
Reference in New Issue
Block a user