Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] refactor logger #2552

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pkg/common/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"errors"
"fmt"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -130,6 +131,31 @@
}
}

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)

Check warning on line 149 in pkg/common/executor.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/executor.go#L145-L149

Added lines #L145 - L149 were not covered by tests
}
}
if ctx.Err() != nil {
return ctx.Err()
}

Check warning on line 154 in pkg/common/executor.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/executor.go#L153-L154

Added lines #L153 - L154 were not covered by tests
return then(ctx, err)
}
}

// Then runs another executor if this executor succeeds
func (e Executor) Then(then Executor) Executor {
return func(ctx context.Context) error {
Expand All @@ -149,6 +175,25 @@
}
}

// 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())

Check warning on line 185 in pkg/common/executor.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/executor.go#L184-L185

Added lines #L184 - L185 were not covered by tests
default:
return errors.Join(err, then(ctx))
}
}
if ctx.Err() != nil {
return ctx.Err()
}

Check warning on line 192 in pkg/common/executor.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/executor.go#L191-L192

Added lines #L191 - L192 were not covered by tests
return nil
}
}

// If only runs this executor if conditional is true
func (e Executor) If(conditional Conditional) Executor {
return func(ctx context.Context) error {
Expand Down
52 changes: 35 additions & 17 deletions pkg/runner/job_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
}
}

postExecutor = postExecutor.Finally(func(ctx context.Context) error {
var stopContainerExecutor common.Executor = func(ctx context.Context) error {
jobError := common.JobError(ctx)
var err error
if rc.Config.AutoRemove || jobError == nil {
Expand All @@ -109,30 +109,48 @@
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)
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 = append(pipeline, rc.InitializeNodeTool())
pipeline = append(pipeline, preSteps...)
pipeline = append(pipeline, steps...)

return common.NewPipelineExecutor(info.startContainer(), common.NewPipelineExecutor(pipeline...).
Finally(func(ctx context.Context) error { //nolint:contextcheck
var cancel context.CancelFunc
if ctx.Err() == context.Canceled {
// in case of an aborted run, we still should execute the
// post steps to allow cleanup.
ctx, cancel = context.WithTimeout(common.WithLogger(context.Background(), common.Logger(ctx)), 5*time.Minute)
defer cancel()
}
return postExecutor(ctx)
}).
Finally(info.interpolateOutputs()).
Finally(info.closeContainer()))
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
var cancel context.CancelFunc
if ctx.Err() == context.Canceled {
// in case of an aborted run, we still should execute the
// post steps to allow cleanup.
ctx, cancel = context.WithTimeout(common.WithLogger(context.Background(), common.Logger(ctx)), 5*time.Minute)
defer cancel()
}

Check warning on line 144 in pkg/runner/job_executor.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/job_executor.go#L140-L144

Added lines #L140 - L144 were not covered by tests
return postExecutor(ctx)
}).
Finally(common.NewFieldExecutor("step", "Complete job", common.NewFieldExecutor("stepid", []string{"--complete-job"},
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) {
Expand Down
Loading