ability to limit log size, defaults to 5mb

This commit is contained in:
Brad Rydzewski 2016-05-12 13:24:51 -07:00
parent 2da1728c70
commit 56b6eb1b0c
3 changed files with 27 additions and 4 deletions

View file

@ -42,8 +42,9 @@ func NewClientUpdater(client client.Client) UpdateFunc {
}
}
func NewClientLogger(client client.Client, id int64, rc io.ReadCloser, wc io.WriteCloser) LoggerFunc {
func NewClientLogger(client client.Client, id int64, rc io.ReadCloser, wc io.WriteCloser, limit int64) LoggerFunc {
var once sync.Once
var size int64
return func(line *build.Line) {
// annoying hack to only start streaming once the first line is written
once.Do(func() {
@ -55,8 +56,14 @@ func NewClientLogger(client client.Client, id int64, rc io.ReadCloser, wc io.Wri
}()
})
if size > limit {
return
}
linejson, _ := json.Marshal(line)
wc.Write(linejson)
wc.Write([]byte{'\n'})
size += int64(len(line.Out))
}
}

View file

@ -86,6 +86,18 @@ var AgentCmd = cli.Command{
Name: "debug",
Usage: "start the agent in debug mode",
},
cli.DurationFlag{
EnvVar: "DRONE_TIMEOUT",
Name: "timeout",
Usage: "drone timeout due to log inactivity",
Value: time.Minute * 5,
},
cli.IntFlag{
EnvVar: "DRONE_MAX_LOGS",
Name: "max-log-size",
Usage: "drone maximum log size in megabytes",
Value: 5,
},
cli.StringSliceFlag{
EnvVar: "DRONE_PLUGIN_PRIVILEGED",
Name: "privileged",
@ -157,9 +169,11 @@ func start(c *cli.Context) {
drone: client,
docker: docker,
config: config{
timeout: c.Duration("timeout"),
namespace: c.String("namespace"),
privileged: c.StringSlice("privileged"),
pull: c.Bool("pull"),
logs: int64(c.Int("max-log-size")) * 1000000,
},
}
for {

View file

@ -17,6 +17,8 @@ type config struct {
namespace string
privileged []string
pull bool
logs int64
timeout time.Duration
}
type pipeline struct {
@ -46,10 +48,10 @@ func (r *pipeline) run() error {
a := agent.Agent{
Update: agent.NewClientUpdater(r.drone),
Logger: agent.NewClientLogger(r.drone, w.Job.ID, rc, wc),
Logger: agent.NewClientLogger(r.drone, w.Job.ID, rc, wc, r.config.logs),
Engine: engine,
Timeout: time.Minute * 15,
Platform: r.config.platform,
Timeout: r.config.timeout,
Platform: "linux/amd64",
Namespace: r.config.namespace,
Escalate: r.config.privileged,
Pull: r.config.pull,