-
Notifications
You must be signed in to change notification settings - Fork 86
/
http_log_writer.go
198 lines (160 loc) · 3.78 KB
/
http_log_writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package worker
import (
"fmt"
"time"
gocontext "context"
"github.com/sirupsen/logrus"
"github.com/travis-ci/worker/context"
)
type httpLogPart struct {
Content string
Final bool
JobID uint64
Number uint64
Token string
}
type httpLogWriter struct {
ctx gocontext.Context
cancel gocontext.CancelFunc
jobID uint64
authToken string
closeChan chan struct{}
logPartNumber uint64
bytesWritten int
maxLength int
maxLengthReached bool
lps *httpLogPartSink
timer *time.Timer
timeout time.Duration
}
func newHTTPLogWriter(ctx gocontext.Context, url string, authToken string, jobID uint64, timeout time.Duration) (*httpLogWriter, error) {
writer := &httpLogWriter{
ctx: context.FromComponent(ctx, "log_writer"),
jobID: jobID,
authToken: authToken,
closeChan: make(chan struct{}),
timer: time.NewTimer(time.Hour),
timeout: timeout,
lps: getHTTPLogPartSinkByURL(url),
}
return writer, nil
}
func (w *httpLogWriter) Write(p []byte) (int, error) {
if w.closed() {
return 0, fmt.Errorf("attempted write to closed log")
}
logger := context.LoggerFromContext(w.ctx).WithFields(logrus.Fields{
"self": "http_log_writer",
"inst": fmt.Sprintf("%p", w),
})
logger.WithFields(logrus.Fields{
"length": len(p),
"bytes": string(p),
}).Debug("begin writing bytes")
w.timer.Reset(w.timeout)
w.bytesWritten += len(p)
if w.bytesWritten > w.maxLength {
logger.Info("wrote past maximum log length - cancelling context")
w.maxLengthReached = true
if w.cancel == nil {
logger.Error("cancel function does not exist")
} else {
w.cancel()
}
return 0, nil
}
err := w.lps.Add(w.ctx, &httpLogPart{
Content: string(p),
JobID: w.jobID,
Number: w.logPartNumber,
Token: w.authToken,
})
if err != nil {
context.LoggerFromContext(w.ctx).WithFields(logrus.Fields{
"err": err,
"self": "http_log_writer",
}).Error("could not add log part to sink")
return 0, err
}
w.logPartNumber++
return len(p), err
}
func (w *httpLogWriter) Close() error {
if w.closed() {
return nil
}
w.timer.Stop()
close(w.closeChan)
err := w.lps.Add(w.ctx, &httpLogPart{
Final: true,
JobID: w.jobID,
Number: w.logPartNumber,
Token: w.authToken,
})
if err != nil {
context.LoggerFromContext(w.ctx).WithFields(logrus.Fields{
"err": err,
"self": "http_log_writer",
}).Error("could not add log part to sink")
return err
}
w.logPartNumber++
return nil
}
func (w *httpLogWriter) Timeout() <-chan time.Time {
return w.timer.C
}
func (w *httpLogWriter) SetMaxLogLength(bytes int) {
w.maxLength = bytes
}
func (w *httpLogWriter) SetJobStarted(meta *JobStartedMeta) {}
func (w *httpLogWriter) SetCancelFunc(cancel gocontext.CancelFunc) {
w.cancel = cancel
}
func (w *httpLogWriter) MaxLengthReached() bool {
return w.maxLengthReached
}
func (w *httpLogWriter) WriteAndClose(p []byte) (int, error) {
if w.closed() {
return 0, fmt.Errorf("log already closed")
}
w.timer.Stop()
close(w.closeChan)
err := w.lps.Add(w.ctx, &httpLogPart{
Content: string(p),
JobID: w.jobID,
Number: w.logPartNumber,
Token: w.authToken,
})
if err != nil {
context.LoggerFromContext(w.ctx).WithFields(logrus.Fields{
"err": err,
"self": "http_log_writer",
}).Error("could not add log part to sink")
return 0, err
}
w.logPartNumber++
err = w.lps.Add(w.ctx, &httpLogPart{
Final: true,
JobID: w.jobID,
Number: w.logPartNumber,
Token: w.authToken,
})
if err != nil {
context.LoggerFromContext(w.ctx).WithFields(logrus.Fields{
"err": err,
"self": "http_log_writer",
}).Error("could not add log part to sink")
return 0, err
}
w.logPartNumber++
return len(p), nil
}
func (w *httpLogWriter) closed() bool {
select {
case <-w.closeChan:
return true
default:
return false
}
}