forked from vmware-archive/atc
/
syncer.go
103 lines (80 loc) · 2.16 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
package pipelines
import (
"os"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc/db"
"github.com/tedsuo/ifrit"
)
type PipelineRunnerFactory func(db.Pipeline) ifrit.Runner
type Syncer struct {
logger lager.Logger
pipelineFactory db.PipelineFactory
pipelineRunnerFactory PipelineRunnerFactory
runningPipelines map[int]runningPipeline
}
type runningPipeline struct {
Name string
ifrit.Process
Exited <-chan error
}
func NewSyncer(
logger lager.Logger,
pipelineFactory db.PipelineFactory,
pipelineRunnerFactory PipelineRunnerFactory,
) *Syncer {
return &Syncer{
logger: logger,
pipelineFactory: pipelineFactory,
pipelineRunnerFactory: pipelineRunnerFactory,
runningPipelines: map[int]runningPipeline{},
}
}
func (syncer *Syncer) Sync() {
pipelines, err := syncer.pipelineFactory.AllPipelines()
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
}
runner := syncer.pipelineRunnerFactory(pipeline)
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
}