Skip to content

Commit

Permalink
Pipeline: propagate ActionContext to Listener when logging custom mes…
Browse files Browse the repository at this point in the history
…sages

Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed Aug 12, 2024
1 parent 1932dcd commit cc26cc8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions pipeline/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ func (ac actContext) Snapshot() map[string]interface{} {
}

func (p *exec) newCtx(a Action) *actContext {
return &actContext{
ctx := &actContext{
c: a,
d: p.gd,
e: p,
f: b,
t: p.t,
l: &listenerLoggerAdapter{l: p.l},
}
ctx.l.c = ctx
return ctx
}

func (p *exec) Execute(act Action) (err error) {
Expand All @@ -71,16 +73,17 @@ func (p *exec) Execute(act Action) (err error) {

type noopListener struct{}

func (n *noopListener) OnBefore(ActionContext) {}
func (n *noopListener) OnAfter(ActionContext, error) {}
func (n *noopListener) OnLog(...interface{}) {}
func (n *noopListener) OnBefore(ActionContext) {}
func (n *noopListener) OnAfter(ActionContext, error) {}
func (n *noopListener) OnLog(ActionContext, ...interface{}) {}

type listenerLoggerAdapter struct {
c ActionContext
l Listener
}

func (n *listenerLoggerAdapter) Log(v ...interface{}) {
n.l.OnLog(v)
n.l.OnLog(n.c, v)
}

type Opt func(*exec)
Expand Down
2 changes: 1 addition & 1 deletion pipeline/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type Listener interface {
// Any error returned by invoking Do() method is returned as last parameter.
OnAfter(ctx ActionContext, err error)
// OnLog is called whenever action invokes Log method on Logger instance
OnLog(v ...interface{})
OnLog(ctx ActionContext, v ...interface{})
}

// ActionContext is created by Executor implementation for sole purpose of invoking Action's Do function.
Expand Down

0 comments on commit cc26cc8

Please sign in to comment.