-
Notifications
You must be signed in to change notification settings - Fork 2
/
listener.go
84 lines (67 loc) · 1.65 KB
/
listener.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
package docker
import (
"github.com/ActiveState/log"
"github.com/ActiveState/logyard-apps/apptail/storage"
"github.com/ActiveState/logyard-apps/common"
"github.com/ActiveState/logyard-apps/docker_events"
"runtime"
"sync"
)
const ID_LENGTH = 12
const RETRY = 3
type dockerListener struct {
waiters map[string]chan bool
mux sync.Mutex
}
var DockerListener *dockerListener
func init() {
DockerListener = new(dockerListener)
DockerListener.waiters = make(map[string]chan bool)
}
func (l *dockerListener) BlockUntilContainerStops(id string) {
var total int
ch := make(chan bool)
id = id[:ID_LENGTH]
if len(id) != ID_LENGTH {
common.Fatal("Invalid docker ID length: %v", len(id))
}
// Add a wait channel
func() {
l.mux.Lock()
if _, ok := l.waiters[id]; ok {
log.Warn("already added")
} else {
l.waiters[id] = ch
}
total = len(l.waiters)
l.mux.Unlock()
runtime.Gosched()
}()
// Wait
log.Infof("Waiting for container %v to exit (total waiters: %d)", id, total)
<-ch
}
func (l *dockerListener) Listen() {
for evt := range docker_events.Stream() {
id := evt.Id[:ID_LENGTH]
if len(id) != ID_LENGTH {
common.Fatal("Invalid docker ID length: %v (orig: %v)", len(id), len(evt.Id))
}
// Notify container stop events by closing the appropriate ch.
if !(evt.Status == "die" || evt.Status == "kill") {
continue
}
l.mux.Lock()
if ch, ok := l.waiters[id]; ok {
close(ch)
delete(l.waiters, id)
}
l.mux.Unlock()
}
}
func (l *dockerListener) TrackerCleanUp(tracker storage.Tracker) {
all_containers := docker_events.GetLiveDockerContainers(RETRY)
if len(all_containers) > 0 {
tracker.CleanUp(all_containers)
}
}