-
Notifications
You must be signed in to change notification settings - Fork 321
/
instance.go
117 lines (103 loc) · 2.64 KB
/
instance.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
package docker
import (
"github.com/baidu/openedge/logger"
"github.com/baidu/openedge/master/engine"
"github.com/baidu/openedge/utils"
)
type attribute struct {
Name string `yaml:"name" json:"name"`
Container struct {
ID string `yaml:"id" json:"id"`
Name string `yaml:"name" json:"name"`
} `yaml:"container" json:"container"`
}
func (a attribute) toPartialStats() engine.PartialStats {
return engine.PartialStats{
engine.KeyName: a.Name,
"container": a.Container,
}
}
// Instance instance of service
type dockerInstance struct {
id string
name string
service *dockerService
log logger.Logger
tomb utils.Tomb
}
func (s *dockerService) newInstance(name string, params containerConfigs) (*dockerInstance, error) {
log := s.log.WithField("instance", name)
cid, err := s.engine.startContainer(name, params)
if err != nil {
log.WithError(err).Warnln("failed to start instance, clean and retry")
// remove and retry
s.engine.removeContainerByName(name)
cid, err = s.engine.startContainer(name, params)
if err != nil {
log.WithError(err).Warnln("failed to start instance again")
return nil, err
}
}
i := &dockerInstance{
service: s,
id: cid,
name: name,
log: log.WithField("cid", cid[:12]),
}
err = i.tomb.Go(func() error {
return engine.Supervising(i)
})
if err != nil {
i.Close()
return nil, err
}
i.log.Infof("instance started")
return i, nil
}
func (i *dockerInstance) Service() engine.Service {
return i.service
}
func (i *dockerInstance) Name() string {
return i.name
}
func (i *dockerInstance) Info() engine.PartialStats {
var attr attribute
attr.Name = i.name
attr.Container.ID = i.id
attr.Container.Name = i.name
return attr.toPartialStats()
}
func (i *dockerInstance) Stats() engine.PartialStats {
return i.service.engine.statsContainer(i.id)
}
func (i *dockerInstance) Wait(s chan<- error) {
defer i.log.Infof("instance stopped")
err := i.service.engine.waitContainer(i.id)
s <- err
}
func (i *dockerInstance) Restart() error {
err := i.service.engine.restartContainer(i.id)
if err != nil {
i.log.WithError(err).Errorf("failed to restart instance")
return err
}
i.log.Infof("instance restarted")
return nil
}
func (i *dockerInstance) Stop() {
i.log.Infof("to stop instance")
err := i.service.engine.stopContainer(i.id)
if err != nil {
i.log.WithError(err).Errorf("failed to stop instance")
}
i.service.engine.removeContainer(i.id)
i.service.instances.Remove(i.name)
}
func (i *dockerInstance) Dying() <-chan struct{} {
return i.tomb.Dying()
}
func (i *dockerInstance) Close() error {
i.log.Infof("to close instance")
i.tomb.Kill(nil)
return i.tomb.Wait()
}