forked from gitlabhq/gitlab-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
details.go
76 lines (63 loc) · 1.63 KB
/
details.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
package machine
import (
"fmt"
"io/ioutil"
"time"
"github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
)
type machineDetails struct {
Name string
Created time.Time `yaml:"-"`
Used time.Time `yaml:"-"`
UsedCount int
State machineState
Reason string
RetryCount int
LastSeen time.Time
}
func (m *machineDetails) isUsed() bool {
return m.State != machineStateIdle
}
func (m *machineDetails) isStuckOnRemove() bool {
return m.State == machineStateRemoving && m.RetryCount >= removeRetryTries
}
func (m *machineDetails) isDead() bool {
return m.State == machineStateIdle &&
time.Since(m.LastSeen) > machineDeadInterval
}
func (m *machineDetails) canBeUsed() bool {
return m.State == machineStateAcquired
}
func (m *machineDetails) match(machineFilter string) bool {
var query string
if n, _ := fmt.Sscanf(m.Name, machineFilter, &query); n != 1 {
return false
}
return true
}
func (m *machineDetails) writeDebugInformation() {
if logrus.GetLevel() < logrus.DebugLevel {
return
}
var details struct {
Details machineDetails
Time string
CreatedAgo time.Duration
}
details.Details = *m
details.Time = time.Now().String()
details.CreatedAgo = time.Since(m.Created)
data := helpers.ToYAML(&details)
ioutil.WriteFile("machines/"+details.Details.Name+".yml", []byte(data), 0600)
}
func (m *machineDetails) logger() *logrus.Entry {
return logrus.WithFields(logrus.Fields{
"name": m.Name,
"created": time.Since(m.Created),
"used": time.Since(m.Used),
"usedCount": m.UsedCount,
"reason": m.Reason,
})
}
type machinesDetails map[string]*machineDetails