From 8ce22709f0f46c563eefb97ec7f4c00b3f7474a2 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 13 Jul 2016 10:48:51 -0700 Subject: [PATCH] write exit code to logs --- build/pipeline.go | 33 +++++++++++++++++++++++++++++++++ build/types.go | 15 ++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/build/pipeline.go b/build/pipeline.go index 1c17274dc..5f7e366ae 100644 --- a/build/pipeline.go +++ b/build/pipeline.go @@ -2,8 +2,10 @@ package build import ( "bufio" + "strconv" "time" + "github.com/Sirupsen/logrus" "github.com/drone/drone/yaml" ) @@ -48,6 +50,12 @@ func (p *Pipeline) Next() <-chan error { // Exec executes the current step. func (p *Pipeline) Exec() { go func() { + defer func() { + if r := recover(); r != nil { + logrus.Errorln("recover executing build step", r) + } + }() + err := p.exec(p.head.Container) if err != nil { p.err = err @@ -119,6 +127,11 @@ func (p *Pipeline) step() { // close closes open channels and signals the pipeline is done. func (p *Pipeline) close(err error) { go func() { + defer func() { + if r := recover(); r != nil { + logrus.Errorln("recover closing the pipeline", r) + } + }() p.done <- err }() } @@ -131,6 +144,12 @@ func (p *Pipeline) exec(c *yaml.Container) error { p.containers = append(p.containers, name) go func() { + defer func() { + if r := recover(); r != nil { + logrus.Errorln("recover writing build output", r) + } + }() + rc, rerr := p.engine.ContainerLogs(name) if rerr != nil { return @@ -165,5 +184,19 @@ func (p *Pipeline) exec(c *yaml.Container) error { } else if state.ExitCode != 0 { return &ExitError{c.Name, state.ExitCode} } + + go func() { + defer func() { + if r := recover(); r != nil { + logrus.Errorln("recover writing exit code to output", r) + } + }() + + p.pipe <- &Line{ + Proc: c.Name, + Type: ExitCodeLine, + Out: strconv.Itoa(state.ExitCode), + } + }() return nil } diff --git a/build/types.go b/build/types.go index 44d12633a..1a79e98ab 100644 --- a/build/types.go +++ b/build/types.go @@ -2,6 +2,14 @@ package build import "fmt" +const ( + StdoutLine int = iota + StderrLine + ExitCodeLine + MetadataLine + ProgressLine +) + // Line is a line of console output. type Line struct { Proc string `json:"proc,omitempty"` @@ -12,7 +20,12 @@ type Line struct { } func (l *Line) String() string { - return fmt.Sprintf("[%s:L%v:%vs] %s", l.Proc, l.Pos, l.Time, l.Out) + switch l.Type { + case ExitCodeLine: + return fmt.Sprintf("[%s] exit code %s", l.Proc, l.Out) + default: + return fmt.Sprintf("[%s:L%v:%vs] %s", l.Proc, l.Pos, l.Time, l.Out) + } } // State defines the state of the container.