-
Notifications
You must be signed in to change notification settings - Fork 0
/
builds_helper.go
100 lines (82 loc) · 1.76 KB
/
builds_helper.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
package commands
import (
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"sync"
)
type buildsHelper struct {
counts map[string]int
builds []*common.Build
lock sync.Mutex
}
func (b *buildsHelper) acquire(runner *common.RunnerConfig) bool {
b.lock.Lock()
defer b.lock.Unlock()
// Check number of builds
count, _ := b.counts[runner.Token]
if runner.Limit > 0 && count >= runner.Limit {
// Too many builds
return false
}
// Create a new build
if b.counts == nil {
b.counts = make(map[string]int)
}
b.counts[runner.Token]++
return true
}
func (b *buildsHelper) release(runner *common.RunnerConfig) bool {
b.lock.Lock()
defer b.lock.Unlock()
_, ok := b.counts[runner.Token]
if ok {
b.counts[runner.Token]--
return true
}
return false
}
func (b *buildsHelper) addBuild(build *common.Build) {
b.lock.Lock()
defer b.lock.Unlock()
runners := make(map[int]bool)
projectRunners := make(map[int]bool)
for _, otherBuild := range b.builds {
if otherBuild.Runner.Token != build.Runner.Token {
continue
}
runners[otherBuild.RunnerID] = true
if otherBuild.ProjectID != build.ProjectID {
continue
}
projectRunners[otherBuild.ProjectRunnerID] = true
}
for {
if !runners[build.RunnerID] {
break
}
build.RunnerID++
}
for {
if !projectRunners[build.ProjectRunnerID] {
break
}
build.ProjectRunnerID++
}
b.builds = append(b.builds, build)
return
}
func (b *buildsHelper) removeBuild(deleteBuild *common.Build) bool {
b.lock.Lock()
defer b.lock.Unlock()
for idx, build := range b.builds {
if build == deleteBuild {
b.builds = append(b.builds[0:idx], b.builds[idx+1:]...)
return true
}
}
return false
}
func (b *buildsHelper) buildsCount() int {
b.lock.Lock()
defer b.lock.Unlock()
return len(b.builds)
}