Skip to content

Commit 5e288b5

Browse files
committed
fetch job-run logs on container failure as well
fixes #141
1 parent 8bdd89b commit 5e288b5

File tree

5 files changed

+43
-35
lines changed

5 files changed

+43
-35
lines changed

core/common.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"crypto/rand"
55
"errors"
66
"fmt"
7-
"github.com/armon/circbuf"
87
"reflect"
98
"strings"
109
"time"
1110

11+
"github.com/armon/circbuf"
1212
docker "github.com/fsouza/go-dockerclient"
1313
)
1414

@@ -21,8 +21,11 @@ var (
2121
ErrLocalImageNotFound = errors.New("couldn't find image on the host")
2222
)
2323

24-
// maximum size of a stdout/stderr stream to be kept in memory and optional stored/sent via mail
25-
const maxStreamSize = 10 * 1024 * 1024
24+
const (
25+
// maximum size of a stdout/stderr stream to be kept in memory and optional stored/sent via mail
26+
maxStreamSize = 10 * 1024 * 1024
27+
logPrefix = "[Job %q (%s)] %s"
28+
)
2629

2730
type Job interface {
2831
GetName() string
@@ -111,19 +114,23 @@ func (c *Context) Stop(err error) {
111114
}
112115

113116
func (c *Context) Log(msg string) {
114-
format := "[Job %q (%s)] %s"
115117
args := []interface{}{c.Job.GetName(), c.Execution.ID, msg}
116118

117119
switch {
118120
case c.Execution.Failed:
119-
c.Logger.Errorf(format, args...)
121+
c.Logger.Errorf(logPrefix, args...)
120122
case c.Execution.Skipped:
121-
c.Logger.Warningf(format, args...)
123+
c.Logger.Warningf(logPrefix, args...)
122124
default:
123-
c.Logger.Noticef(format, args...)
125+
c.Logger.Noticef(logPrefix, args...)
124126
}
125127
}
126128

129+
func (c *Context) Warn(msg string) {
130+
args := []interface{}{c.Job.GetName(), c.Execution.ID, msg}
131+
c.Logger.Warningf(logPrefix, args...)
132+
}
133+
127134
// Execution contains all the information relative to a Job execution.
128135
type Execution struct {
129136
ID string

core/runjob.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,32 @@ func (j *RunJob) Run(ctx *Context) error {
102102
return err
103103
}
104104

105-
if err := j.watchContainer(container.ID); err != nil {
105+
err = j.watchContainer(container.ID)
106+
if err == ErrUnexpected {
106107
return err
107108
}
108109

109-
if err := j.Client.Logs(docker.LogsOptions{
110+
if logsErr := j.Client.Logs(docker.LogsOptions{
110111
Container: container.ID,
111112
OutputStream: ctx.Execution.OutputStream,
112113
ErrorStream: ctx.Execution.ErrorStream,
113114
Stdout: true,
114115
Stderr: true,
115116
Since: startTime.Unix(),
116-
RawTerminal: true,
117-
}); err != nil {
118-
return err
117+
RawTerminal: j.TTY,
118+
}); logsErr != nil {
119+
ctx.Warn("failed to fetch container logs: " + logsErr.Error())
119120
}
120121

121122
if j.Container == "" {
122-
return j.deleteContainer(container.ID)
123+
defer func() {
124+
if delErr := j.deleteContainer(container.ID); delErr != nil {
125+
ctx.Warn("failed to delete container: " + delErr.Error())
126+
}
127+
}()
123128
}
124-
return nil
129+
130+
return err
125131
}
126132

127133
func (j *RunJob) searchLocalImage() error {

core/scheduler.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ func (w *jobWrapper) stop(ctx *Context, err error) {
107107
errText = ctx.Execution.Error.Error()
108108
}
109109

110-
output := ctx.Execution.OutputStream.Bytes()
110+
if ctx.Execution.OutputStream.TotalWritten() > 0 {
111+
ctx.Log("StdOut: " + ctx.Execution.OutputStream.String())
112+
}
111113

112-
if len(output) > 0 {
113-
ctx.Log("Output: " + string(output))
114+
if ctx.Execution.ErrorStream.TotalWritten() > 0 {
115+
ctx.Log("StdErr: " + ctx.Execution.ErrorStream.String())
114116
}
115117

116118
msg := fmt.Sprintf(

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ github.com/containerd/go-runc v0.0.0-20180907222934-5a6d9f37cfa3/go.mod h1:IV7qH
2828
github.com/containerd/ttrpc v0.0.0-20190828154514-0e0f228740de/go.mod h1:PvCDdDGpgqzQIzDW1TphrGLssLDZp2GuS+X5DkEJB8o=
2929
github.com/containerd/typeurl v0.0.0-20180627222232-a93fcdb778cd/go.mod h1:Cm3kwCdlkCfMSHURc+r6fwoGH6/F1hH3S4sg0rLFWPc=
3030
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
31+
github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w=
3132
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
3233
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3334
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -63,7 +64,9 @@ github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs
6364
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
6465
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
6566
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
67+
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
6668
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
69+
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
6770
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
6871
github.com/hashicorp/errwrap v0.0.0-20141028054710-7554cd9344ce/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
6972
github.com/hashicorp/go-multierror v0.0.0-20161216184304-ed905158d874/go.mod h1:JMRHfdO9jKNzS/+BTlxCjKNQHg/jZAft8U7LloJvN7I=
@@ -92,6 +95,7 @@ github.com/moby/term v0.0.0-20200915141129-7f0af18e79f2 h1:SPoLlS9qUUnXcIY4pvA4C
9295
github.com/moby/term v0.0.0-20200915141129-7f0af18e79f2/go.mod h1:TjQg8pa4iejrUrjiz0MCtMV38jdMNW4doKSiBrEvCQQ=
9396
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
9497
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
98+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
9599
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
96100
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
97101
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -185,6 +189,7 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3
185189
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
186190
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
187191
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
192+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
188193
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
189194
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
190195
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=

middlewares/save.go

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package middlewares
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"fmt"
7-
"io"
8-
"os"
6+
"io/ioutil"
97
"path/filepath"
108

119
"github.com/mcuadros/ofelia/core"
@@ -60,12 +58,12 @@ func (m *Save) saveToDisk(ctx *core.Context) error {
6058
))
6159

6260
e := ctx.Execution
63-
err := m.saveReaderToDisk(bytes.NewReader(e.ErrorStream.Bytes()), fmt.Sprintf("%s.stderr.log", root))
61+
err := m.writeFile(e.ErrorStream.Bytes(), fmt.Sprintf("%s.stderr.log", root))
6462
if err != nil {
6563
return err
6664
}
6765

68-
err = m.saveReaderToDisk(bytes.NewReader(e.OutputStream.Bytes()), fmt.Sprintf("%s.stdout.log", root))
66+
err = m.writeFile(e.OutputStream.Bytes(), fmt.Sprintf("%s.stdout.log", root))
6967
if err != nil {
7068
return err
7169
}
@@ -84,19 +82,9 @@ func (m *Save) saveContextToDisk(ctx *core.Context, filename string) error {
8482
"Execution": ctx.Execution,
8583
}, "", " ")
8684

87-
return m.saveReaderToDisk(bytes.NewBuffer(js), filename)
85+
return m.writeFile(js, filename)
8886
}
8987

90-
func (m *Save) saveReaderToDisk(r io.Reader, filename string) error {
91-
f, err := os.Create(filename)
92-
if err != nil {
93-
return err
94-
}
95-
96-
defer f.Close()
97-
if _, err := io.Copy(f, r); err != nil {
98-
return err
99-
}
100-
101-
return nil
88+
func (m *Save) writeFile(data []byte, filename string) error {
89+
return ioutil.WriteFile(filename, data, 0644)
10290
}

0 commit comments

Comments
 (0)