-
Notifications
You must be signed in to change notification settings - Fork 0
/
containergroup.go
74 lines (63 loc) · 1.87 KB
/
containergroup.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
package cntest
// GroupedContainer container object
// tracks containers on which this depends
type GroupedContainer struct {
Container *Container
dependsOn map[string]*GroupedContainer
started chan bool
}
// NewGroupedContainer constructor with a container
func NewGroupedContainer(withContainer *Container) *GroupedContainer {
return &GroupedContainer{
Container: withContainer,
dependsOn: map[string]*GroupedContainer{},
started: make(chan bool),
}
}
// ContainerGroup manages a group of connected containers
// like docker-compose but in code
type ContainerGroup map[string]*GroupedContainer
// Add adds a container to the group keyed by its name
func (cg ContainerGroup) Add(cnt *GroupedContainer) {
cg[cnt.Container.name] = cnt
}
func (gc *GroupedContainer) getDoneChannel() chan<- bool {
return gc.started
}
// Start starts all the containers
func (cg ContainerGroup) Start() {
for _, container := range cg {
go container.Start()
}
}
// Await blocks until all containers have started
func (cg ContainerGroup) Await() {
for _, eachContainer := range cg {
eachContainer.Await()
}
}
// Start starts a container once all the dependents start
func (gc *GroupedContainer) Start() {
for _, depend := range gc.dependsOn {
depend.Await()
}
gc.Container.Start()
gc.Container.AwaitIsReady()
gc.SignalStarted()
}
// SignalStarted signals
func (gc *GroupedContainer) SignalStarted() {
// multiple tasks reading this channel are unblocked
close(gc.started)
}
// Await uses a for range on a channel to wait for all dependent containers to start
func (gc *GroupedContainer) Await() {
for range gc.started {
}
}
// DependsOn is called to ensure this container wont start until these ones have
func (gc *GroupedContainer) DependsOn(containers ...*GroupedContainer) {
for _, eachContainer := range containers {
gc.dependsOn[eachContainer.Container.name] = eachContainer
}
}