forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncer.go
118 lines (93 loc) · 2.7 KB
/
syncer.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
package pipelines
import (
"os"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc/db"
"github.com/concourse/atc/dbng"
"github.com/tedsuo/ifrit"
)
//go:generate counterfeiter . SyncherDB
type SyncherDB interface {
GetAllPipelines() ([]db.SavedPipeline, error)
}
type PipelineRunnerFactory func(db.PipelineDB, dbng.Pipeline) ifrit.Runner
type Syncer struct {
logger lager.Logger
syncherDB SyncherDB
pipelineDBFactory db.PipelineDBFactory
pipelineFactory dbng.PipelineFactory
pipelineRunnerFactory PipelineRunnerFactory
runningPipelines map[int]runningPipeline
}
type runningPipeline struct {
Name string
ifrit.Process
Exited <-chan error
}
func NewSyncer(
logger lager.Logger,
syncherDB SyncherDB,
pipelineDBFactory db.PipelineDBFactory,
pipelineFactory dbng.PipelineFactory,
pipelineRunnerFactory PipelineRunnerFactory,
) *Syncer {
return &Syncer{
logger: logger,
syncherDB: syncherDB,
pipelineDBFactory: pipelineDBFactory,
pipelineFactory: pipelineFactory,
pipelineRunnerFactory: pipelineRunnerFactory,
runningPipelines: map[int]runningPipeline{},
}
}
func (syncer *Syncer) Sync() {
pipelines, err := syncer.syncherDB.GetAllPipelines()
if err != nil {
syncer.logger.Error("failed-to-get-pipelines", err)
return
}
for id, runningPipeline := range syncer.runningPipelines {
select {
case <-runningPipeline.Exited:
syncer.logger.Debug("pipeline-exited", lager.Data{"pipeline-id": id})
syncer.removePipeline(id)
default:
}
var found bool
for _, pipeline := range pipelines {
if pipeline.Paused {
continue
}
if pipeline.ID == id && pipeline.Name == runningPipeline.Name {
found = true
}
}
if !found {
syncer.logger.Debug("stopping-pipeline", lager.Data{"pipeline-id": id})
runningPipeline.Process.Signal(os.Interrupt)
syncer.removePipeline(id)
}
}
for _, pipeline := range pipelines {
if pipeline.Paused || syncer.isPipelineRunning(pipeline.ID) {
continue
}
pipelineDB := syncer.pipelineDBFactory.Build(pipeline)
dbPipeline := syncer.pipelineFactory.GetPipelineByID(pipelineDB.TeamID(), pipelineDB.Pipeline().ID)
runner := syncer.pipelineRunnerFactory(pipelineDB, dbPipeline)
syncer.logger.Debug("starting-pipeline", lager.Data{"pipeline": pipeline.Name})
process := ifrit.Invoke(runner)
syncer.runningPipelines[pipeline.ID] = runningPipeline{
Name: pipeline.Name,
Process: process,
Exited: process.Wait(),
}
}
}
func (syncer *Syncer) removePipeline(pipelineID int) {
delete(syncer.runningPipelines, pipelineID)
}
func (syncer *Syncer) isPipelineRunning(pipelineID int) bool {
_, found := syncer.runningPipelines[pipelineID]
return found
}