forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
162 lines (133 loc) · 4.07 KB
/
scheduler.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package scheduler
import (
"sync"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/config"
"github.com/concourse/atc/db"
"github.com/concourse/atc/db/algorithm"
"github.com/concourse/atc/db/lock"
"github.com/concourse/atc/scheduler/inputmapper"
)
type Scheduler struct {
DB SchedulerDB
InputMapper inputmapper.InputMapper
BuildStarter BuildStarter
Scanner Scanner
}
//go:generate counterfeiter . SchedulerDB
type SchedulerDB interface {
AcquireSchedulingLock(lager.Logger, time.Duration) (lock.Lock, bool, error)
LoadVersionsDB() (*algorithm.VersionsDB, error)
GetPipelineName() string
Reload() (bool, error)
Config() atc.Config
CreateJobBuild(job string) (db.Build, error)
EnsurePendingBuildExists(jobName string) error
GetAllPendingBuilds() (map[string][]db.Build, error)
GetPendingBuildsForJob(jobName string) ([]db.Build, error)
}
//go:generate counterfeiter . Scanner
type Scanner interface {
Scan(lager.Logger, string) error
}
func (s *Scheduler) Schedule(
logger lager.Logger,
versions *algorithm.VersionsDB,
jobConfigs atc.JobConfigs,
resourceConfigs atc.ResourceConfigs,
resourceTypes atc.VersionedResourceTypes,
) (map[string]time.Duration, error) {
jobSchedulingTime := map[string]time.Duration{}
for _, jobConfig := range jobConfigs {
jStart := time.Now()
err := s.ensurePendingBuildExists(logger, versions, jobConfig)
jobSchedulingTime[jobConfig.Name] = time.Since(jStart)
if err != nil {
return jobSchedulingTime, err
}
}
nextPendingBuilds, err := s.DB.GetAllPendingBuilds()
if err != nil {
logger.Error("failed-to-get-all-next-pending-builds", err)
return jobSchedulingTime, err
}
for _, jobConfig := range jobConfigs {
jStart := time.Now()
nextPendingBuildsForJob, ok := nextPendingBuilds[jobConfig.Name]
if !ok {
continue
}
err := s.BuildStarter.TryStartPendingBuildsForJob(logger, jobConfig, resourceConfigs, resourceTypes, nextPendingBuildsForJob)
jobSchedulingTime[jobConfig.Name] = jobSchedulingTime[jobConfig.Name] + time.Since(jStart)
if err != nil {
return jobSchedulingTime, err
}
}
return jobSchedulingTime, nil
}
func (s *Scheduler) ensurePendingBuildExists(
logger lager.Logger,
versions *algorithm.VersionsDB,
jobConfig atc.JobConfig,
) error {
inputMapping, err := s.InputMapper.SaveNextInputMapping(logger, versions, jobConfig)
if err != nil {
return err
}
for _, inputConfig := range config.JobInputs(jobConfig) {
inputVersion, ok := inputMapping[inputConfig.Name]
//trigger: true, and the version has not been used
if ok && inputVersion.FirstOccurrence && inputConfig.Trigger {
err := s.DB.EnsurePendingBuildExists(jobConfig.Name)
if err != nil {
logger.Error("failed-to-ensure-pending-build-exists", err)
return err
}
break
}
}
return nil
}
type Waiter interface {
Wait()
}
func (s *Scheduler) TriggerImmediately(
logger lager.Logger,
jobConfig atc.JobConfig,
resourceConfigs atc.ResourceConfigs,
resourceTypes atc.VersionedResourceTypes,
) (db.Build, Waiter, error) {
logger = logger.Session("trigger-immediately", lager.Data{"job_name": jobConfig.Name})
build, err := s.DB.CreateJobBuild(jobConfig.Name)
if err != nil {
logger.Error("failed-to-create-job-build", err)
return nil, nil, err
}
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
defer wg.Done()
nextPendingBuilds, err := s.DB.GetPendingBuildsForJob(jobConfig.Name)
if err != nil {
logger.Error("failed-to-get-next-pending-build-for-job", err)
return
}
err = s.BuildStarter.TryStartPendingBuildsForJob(logger, jobConfig, resourceConfigs, resourceTypes, nextPendingBuilds)
if err != nil {
logger.Error("failed-to-start-next-pending-build-for-job", err, lager.Data{"job-name": jobConfig.Name})
return
}
}()
return build, wg, nil
}
func (s *Scheduler) SaveNextInputMapping(logger lager.Logger, job atc.JobConfig) error {
versions, err := s.DB.LoadVersionsDB()
if err != nil {
logger.Error("failed-to-load-versions-db", err)
return err
}
_, err = s.InputMapper.SaveNextInputMapping(logger, versions, job)
return err
}