-
Notifications
You must be signed in to change notification settings - Fork 3
/
jobs.go
52 lines (47 loc) · 1.43 KB
/
jobs.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
package getaredis
import "time"
func CleanRedisInstances(ctx *context) (containerNames []string) {
var instances = make([]Instance, 0)
containerNames = make([]string, 0)
var maxTimeStamp = time.Now().Add(-1 * time.Second * 60 * 60 * time.Duration(ctx.config.MaxInstanceTime))
ctx.db.Model(&Instance{}).Where("running = 1 AND created_at < ?", maxTimeStamp).Find(&instances)
for _, instance := range instances {
ctx.RemoveContainer(instance.HostedAtIP, instance.ContainerID)
containerNames = append(containerNames, instance.Name)
}
ctx.db.Model(&Instance{}).Where("running = 1 AND created_at < ?", maxTimeStamp).UpdateColumn("running", false)
return
}
func MonitorHosts(ctx *context) (startedHosts bool, deletedHosts []string, err error) {
hosts := ctx.ListHosts()
deletedHosts = make([]string, 0)
zeros := 0
mini := 1000000
for _, host := range hosts {
if host.NumberOfContainers == 0 {
zeros++
} else if host.NumberOfContainers < mini {
mini = host.NumberOfContainers
}
}
if zeros == 0 && mini > ctx.config.MaxContainersPerHost/2 {
err = ctx.NewHostFromImage()
if err != nil {
return
}
startedHosts = true
return
} else if zeros > 0 {
for _, host := range hosts {
if mini > ctx.config.MaxContainersPerHost/2 && zeros == 1 {
break
}
if host.NumberOfContainers == 0 {
err = ctx.DeleteHost(host.PublicIP)
zeros--
deletedHosts = append(deletedHosts, host.PublicIP)
}
}
}
return
}