-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
97 lines (85 loc) · 2.33 KB
/
types.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
package main
import (
"github.com/jpillora/backoff"
"sync"
"time"
)
// Config structure will hold all our configuration
// values (processed from environment variables)
// after we initialize it with envconfig.Process
type Config struct {
DockerSocket string `default:"/var/run/docker.sock" split_words:"true"`
ScrapeInterval time.Duration `default:"10s" split_words:"true"`
InspectTimeout time.Duration `default:"5s" split_words:"true"`
CleanupInterval time.Duration `default:"10s" split_words:"true"`
RestartBackoffMin time.Duration `default:"10s" split_words:"true"`
RestartBackoffMax time.Duration `default:"5m" split_words:"true"`
DebugMode bool `default:"false" split_words:"true"`
HealMode bool `default:"false" split_words:"true"`
Port string `default:"9102"`
}
type Container struct {
ID string
Image string
State string
Health string
Restarts int
StuckInspect bool
Healed map[string]int
}
type Containers struct {
db map[string]Container
mutex sync.RWMutex
}
func (c *Containers) Get(name string) (Container, bool) {
defer c.mutex.RUnlock()
c.mutex.RLock()
container, ok := c.db[name]
return container, ok
}
func (c *Containers) Put(name string, container Container) {
defer c.mutex.Unlock()
c.mutex.Lock()
c.db[name] = container
}
func (c *Containers) HealSuccess(name string) {
defer c.mutex.Unlock()
c.mutex.Lock()
c.db[name].Healed["success"]++
}
func (c *Containers) HealFail(name string) {
defer c.mutex.Unlock()
c.mutex.Lock()
c.db[name].Healed["fail"]++
}
type Patient struct {
backoff *backoff.Backoff
lastRestartAttempt time.Time
beingTreated bool
}
type Patients struct {
db map[string]*Patient
mutex sync.RWMutex
}
func (p *Patients) Get(name string) (*Patient, bool) {
defer p.mutex.RUnlock()
p.mutex.RLock()
patient, ok := p.db[name]
return patient, ok
}
func (p *Patients) Put(name string, patient *Patient) {
defer p.mutex.Unlock()
p.mutex.Lock()
p.db[name] = patient
}
func (p *Patients) StartTreatment(name string) {
defer p.mutex.Unlock()
p.mutex.Lock()
p.db[name].beingTreated = true
p.db[name].lastRestartAttempt = time.Now()
}
func (p *Patients) StopTreatment(name string) {
defer p.mutex.Unlock()
p.mutex.Lock()
p.db[name].beingTreated = false
}