Skip to content

Commit

Permalink
reconnect log stream unless container is dead
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzoller committed May 19, 2016
1 parent 231ac0e commit 118f322
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions router/pump.go
Expand Up @@ -140,37 +140,58 @@ func (p *LogsPump) pumpLogs(event *docker.APIEvents, backlog bool) {
debug("pump.pumpLogs():", id, "ignored: environ ignore")
return
}
var tail string

var sinceTime time.Time
if backlog {
tail = "all"
sinceTime = time.Unix(0, 0)
} else {
tail = "0"
sinceTime = time.Now()
}
outrd, outwr := io.Pipe()
errrd, errwr := io.Pipe()
p.mu.Lock()
p.pumps[id] = newContainerPump(container, outrd, errrd)
p.mu.Unlock()
p.update(event)
debug("pump.pumpLogs():", id, "started")
go func() {
err := p.client.Logs(docker.LogsOptions{
Container: id,
OutputStream: outwr,
ErrorStream: errwr,
Stdout: true,
Stderr: true,
Follow: true,
Tail: tail,
})
if err != nil {
debug("pump.pumpLogs():", id, "stopped:", err)
for {
debug("pump.pumpLogs():", id, "started")
err := p.client.Logs(docker.LogsOptions{
Container: id,
OutputStream: outwr,
ErrorStream: errwr,
Stdout: true,
Stderr: true,
Follow: true,
Tail: "all",
Since: sinceTime.Unix(),
})
if err != nil {
debug("pump.pumpLogs():", id, "stopped with error:", err)
} else {
debug("pump.pumpLogs():", id, "stopped")
}

sinceTime = time.Now()

container, err := p.client.InspectContainer(id)
if err != nil {
_, four04 := err.(*docker.NoSuchContainer)
if !four04 {
assert(err, "pump")
}
} else if container.State.Running {
continue
}

debug("pump.pumpLogs():", id, "dead")
outwr.Close()
errwr.Close()
p.mu.Lock()
delete(p.pumps, id)
p.mu.Unlock()
return
}
outwr.Close()
errwr.Close()
p.mu.Lock()
delete(p.pumps, id)
p.mu.Unlock()
}()
}

Expand Down

0 comments on commit 118f322

Please sign in to comment.