forked from digitalocean/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.go
80 lines (62 loc) · 1.9 KB
/
updater.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
package maxinflight
import (
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/dbng"
)
//go:generate counterfeiter . Updater
type Updater interface {
UpdateMaxInFlightReached(logger lager.Logger, jobConfig atc.JobConfig, buildID int) (bool, error)
}
func NewUpdater(pipeline dbng.Pipeline) Updater {
return &updater{pipeline: pipeline}
}
type updater struct {
pipeline dbng.Pipeline
}
func (u *updater) UpdateMaxInFlightReached(logger lager.Logger, jobConfig atc.JobConfig, buildID int) (bool, error) {
logger = logger.Session("is-max-in-flight-reached", lager.Data{"job-name": jobConfig.Name})
job, found, err := u.pipeline.Job(jobConfig.Name)
if err != nil {
logger.Error("failed-to-get-job", err)
return false, err
}
if !found {
logger.Info("job-not-found")
return true, nil
}
reached, err := u.isMaxInFlightReached(logger, jobConfig, buildID, job)
if err != nil {
return false, err
}
err = job.SetMaxInFlightReached(reached)
if err != nil {
logger.Error("failed-to-set-max-in-flight-reached", err)
return false, err
}
return reached, nil
}
func (u *updater) isMaxInFlightReached(logger lager.Logger, jobConfig atc.JobConfig, buildID int, job dbng.Job) (bool, error) {
maxInFlight := jobConfig.MaxInFlight()
if maxInFlight == 0 {
return false, nil
}
builds, err := job.GetRunningBuildsBySerialGroup(jobConfig.GetSerialGroups())
if err != nil {
logger.Error("failed-to-get-running-builds-by-serial-group", err)
return false, err
}
if len(builds) >= maxInFlight {
return true, nil
}
nextMostPendingBuild, found, err := job.GetNextPendingBuildBySerialGroup(jobConfig.GetSerialGroups())
if err != nil {
logger.Error("failed-to-get-next-pending-build-by-serial-group", err)
return false, err
}
if !found {
logger.Info("pending-build-disappeared-from-serial-group")
return true, nil
}
return nextMostPendingBuild.ID() != buildID, nil
}