Skip to content

Commit 519a2be

Browse files
Skarlsomichelvocks
authored andcommitted
Remove the channel Iterator method from ActivePipelines (#175)
* Remove the iter channel. * Renamed and fixed comments about iter usage. * Failed to save the file...
1 parent d37e5f8 commit 519a2be

File tree

7 files changed

+43
-85
lines changed

7 files changed

+43
-85
lines changed

handlers/hook.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ func GitWebHook(c echo.Context) error {
122122
}
123123

124124
var foundPipe *gaia.Pipeline
125-
for pipe := range pipeline.GlobalActivePipelines.Iter() {
125+
for _, pipe := range pipeline.GlobalActivePipelines.GetAll() {
126126
if pipe.Repo.URL == p.Repo.GitURL || pipe.Repo.URL == p.Repo.HTMLURL || pipe.Repo.URL == p.Repo.SSHURL {
127127
foundPipe = &pipe
128+
break
128129
}
129130
}
130131
err = pipeline.UpdateRepository(foundPipe)

handlers/pipeline.go

+14-24
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,8 @@ func PipelineNameAvailable(c echo.Context) error {
9494

9595
// PipelineGetAll returns all registered pipelines.
9696
func PipelineGetAll(c echo.Context) error {
97-
var pipelines []gaia.Pipeline
98-
9997
// Get all active pipelines
100-
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
101-
pipelines = append(pipelines, pipeline)
102-
}
98+
pipelines := pipeline.GlobalActivePipelines.GetAll()
10399

104100
// Return as json
105101
return c.JSON(http.StatusOK, pipelines)
@@ -116,18 +112,12 @@ func PipelineGet(c echo.Context) error {
116112
}
117113

118114
// Look up pipeline for the given id
119-
var foundPipeline gaia.Pipeline
120-
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
115+
for _, pipeline := range pipeline.GlobalActivePipelines.GetAll() {
121116
if pipeline.ID == pipelineID {
122-
foundPipeline = pipeline
117+
return c.JSON(http.StatusOK, pipeline)
123118
}
124119
}
125120

126-
if foundPipeline.Name != "" {
127-
return c.JSON(http.StatusOK, foundPipeline)
128-
}
129-
130-
// Pipeline not found
131121
return c.String(http.StatusNotFound, errPipelineNotFound.Error())
132122
}
133123

@@ -142,9 +132,10 @@ func PipelineUpdate(c echo.Context) error {
142132

143133
// Look up pipeline for the given id
144134
var foundPipeline gaia.Pipeline
145-
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
135+
for _, pipeline := range pipeline.GlobalActivePipelines.GetAll() {
146136
if pipeline.ID == p.ID {
147137
foundPipeline = pipeline
138+
break
148139
}
149140
}
150141

@@ -248,14 +239,13 @@ func PipelineDelete(c echo.Context) error {
248239

249240
// Look up pipeline for the given id
250241
var foundPipeline gaia.Pipeline
251-
var index int
252242
var deletedPipelineIndex int
253-
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
243+
for index, pipeline := range pipeline.GlobalActivePipelines.GetAll() {
254244
if pipeline.ID == pipelineID {
255245
foundPipeline = pipeline
256246
deletedPipelineIndex = index
247+
break
257248
}
258-
index++
259249
}
260250

261251
if foundPipeline.Name == "" {
@@ -302,9 +292,10 @@ func PipelineTrigger(c echo.Context) error {
302292

303293
// Look up pipeline for the given id
304294
var foundPipeline gaia.Pipeline
305-
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
295+
for _, pipeline := range pipeline.GlobalActivePipelines.GetAll() {
306296
if pipeline.ID == pipelineID {
307297
foundPipeline = pipeline
298+
break
308299
}
309300
}
310301

@@ -343,9 +334,10 @@ func PipelineResetToken(c echo.Context) error {
343334

344335
// Look up pipeline for the given id
345336
var foundPipeline gaia.Pipeline
346-
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
337+
for _, pipeline := range pipeline.GlobalActivePipelines.GetAll() {
347338
if pipeline.ID == pipelineID {
348339
foundPipeline = pipeline
340+
break
349341
}
350342
}
351343

@@ -408,9 +400,10 @@ func PipelineStart(c echo.Context) error {
408400

409401
// Look up pipeline for the given id
410402
var foundPipeline gaia.Pipeline
411-
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
403+
for _, pipeline := range pipeline.GlobalActivePipelines.GetAll() {
412404
if pipeline.ID == pipelineID {
413405
foundPipeline = pipeline
406+
break
414407
}
415408
}
416409

@@ -437,10 +430,7 @@ type getAllWithLatestRun struct {
437430
func PipelineGetAllWithLatestRun(c echo.Context) error {
438431
// Get all active pipelines
439432
storeService, _ := services.StorageService()
440-
var pipelines []gaia.Pipeline
441-
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
442-
pipelines = append(pipelines, pipeline)
443-
}
433+
pipelines := pipeline.GlobalActivePipelines.GetAll()
444434

445435
// Iterate all pipelines
446436
var pipelinesWithLatestRun []getAllWithLatestRun

handlers/pipeline_run.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ func PipelineStop(c echo.Context) error {
7373

7474
// Look up pipeline for the given id
7575
var foundPipeline gaia.Pipeline
76-
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
76+
for _, pipeline := range pipeline.GlobalActivePipelines.GetAll() {
7777
if pipeline.ID == p {
7878
foundPipeline = pipeline
79+
break
7980
}
8081
}
8182

workers/pipeline/create_pipeline.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,11 @@ func ValidatePipelineName(pName string) error {
177177
}
178178

179179
// Check if pipeline name is already in use.
180-
alreadyInUse := false
181-
for activePipeline := range GlobalActivePipelines.Iter() {
180+
for _, activePipeline := range GlobalActivePipelines.GetAll() {
182181
if strings.ToLower(s) == strings.ToLower(activePipeline.Name) {
183-
alreadyInUse = true
182+
return errPipelineNameInUse
184183
}
185184
}
186-
187-
// Throw error because it's already in use.
188-
if alreadyInUse {
189-
return errPipelineNameInUse
190-
}
191185
}
192186
return nil
193187
}

workers/pipeline/git.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,9 @@ func gitCloneRepo(repo *gaia.GitRepo) error {
187187

188188
func updateAllCurrentPipelines() {
189189
gaia.Cfg.Logger.Debug("starting updating of pipelines...")
190-
var allPipelines []gaia.Pipeline
190+
allPipelines := GlobalActivePipelines.GetAll()
191191
var wg sync.WaitGroup
192192
sem := make(chan int, 4)
193-
for pipeline := range GlobalActivePipelines.Iter() {
194-
allPipelines = append(allPipelines, pipeline)
195-
}
196193
for _, p := range allPipelines {
197194
wg.Add(1)
198195
go func(pipe gaia.Pipeline) {

workers/pipeline/pipeline.go

+19-44
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,13 @@ func (ap *ActivePipelines) Remove(index int) {
133133

134134
// GetByName looks up the pipeline by the given name.
135135
func (ap *ActivePipelines) GetByName(n string) *gaia.Pipeline {
136-
var foundPipeline gaia.Pipeline
137-
for pipeline := range ap.Iter() {
136+
for _, pipeline := range ap.GetAll() {
138137
if pipeline.Name == n {
139-
foundPipeline = pipeline
138+
return &pipeline
140139
}
141140
}
142141

143-
if foundPipeline.Name == "" {
144-
return nil
145-
}
146-
147-
return &foundPipeline
142+
return nil
148143
}
149144

150145
// Replace takes the given pipeline and replaces it in the ActivePipelines
@@ -174,62 +169,43 @@ func (ap *ActivePipelines) Replace(p gaia.Pipeline) bool {
174169

175170
// ReplaceByName replaces the pipeline that has the given name with the given pipeline.
176171
func (ap *ActivePipelines) ReplaceByName(n string, p gaia.Pipeline) bool {
177-
178-
var index int
179-
var pipelineIndex int
180-
var found bool
181-
182-
for pipeline := range ap.Iter() {
172+
for index, pipeline := range ap.GetAll() {
183173
if pipeline.Name == n {
184-
found = true
185-
pipelineIndex = index
174+
ap.Update(index, p)
175+
return true
186176
}
187-
index++
188177
}
189-
190-
if found {
191-
ap.Update(pipelineIndex, p)
192-
}
193-
194-
return found
195-
178+
return false
196179
}
197180

198-
// Iter iterates over the pipelines in the concurrent slice.
199-
func (ap *ActivePipelines) Iter() <-chan gaia.Pipeline {
200-
c := make(chan gaia.Pipeline)
201-
202-
go func() {
203-
ap.RLock()
204-
defer ap.RUnlock()
205-
for _, pipeline := range ap.Pipelines {
206-
c <- pipeline
207-
}
208-
close(c)
209-
}()
210-
181+
// GetAll iterates over the pipelines in the concurrent slice.
182+
func (ap *ActivePipelines) GetAll() []gaia.Pipeline {
183+
c := make([]gaia.Pipeline, 0)
184+
ap.RLock()
185+
defer ap.RUnlock()
186+
for _, pipeline := range ap.Pipelines {
187+
c = append(c, pipeline)
188+
}
211189
return c
212190
}
213191

214192
// Contains checks if the given pipeline name has been already appended
215193
// to the given ActivePipelines instance.
216194
func (ap *ActivePipelines) Contains(n string) bool {
217-
var foundPipeline bool
218-
for pipeline := range ap.Iter() {
195+
for _, pipeline := range ap.GetAll() {
219196
if pipeline.Name == n {
220-
foundPipeline = true
197+
return true
221198
}
222199
}
223200

224-
return foundPipeline
201+
return false
225202
}
226203

227204
// RemoveDeletedPipelines removes the pipelines whose names are NOT
228205
// present in `existingPipelineNames` from the given ActivePipelines instance.
229206
func (ap *ActivePipelines) RemoveDeletedPipelines(existingPipelineNames []string) {
230207
var deletedPipelineIndices []int
231-
var index int
232-
for pipeline := range ap.Iter() {
208+
for index, pipeline := range ap.GetAll() {
233209
found := false
234210
for _, name := range existingPipelineNames {
235211
if pipeline.Name == name {
@@ -240,7 +216,6 @@ func (ap *ActivePipelines) RemoveDeletedPipelines(existingPipelineNames []string
240216
if !found {
241217
deletedPipelineIndices = append(deletedPipelineIndices, index)
242218
}
243-
index++
244219
}
245220
for _, idx := range deletedPipelineIndices {
246221
ap.Remove(idx)

workers/pipeline/pipeline_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestRemove(t *testing.T) {
7474
ap.Remove(1)
7575

7676
count := 0
77-
for pipeline := range ap.Iter() {
77+
for _, pipeline := range ap.GetAll() {
7878
count++
7979
if pipeline.Name == "Pipeline B" {
8080
t.Fatalf("Pipeline B still exists. It should have been removed.")
@@ -185,7 +185,7 @@ func TestIter(t *testing.T) {
185185
}
186186

187187
count := 0
188-
for pipeline := range ap.Iter() {
188+
for _, pipeline := range ap.GetAll() {
189189
count++
190190
retrievedNames = append(retrievedNames, pipeline.Name)
191191
}
@@ -246,7 +246,7 @@ func TestRemoveDeletedPipelines(t *testing.T) {
246246

247247
ap.RemoveDeletedPipelines(existingPipelineNames)
248248

249-
for pipeline := range ap.Iter() {
249+
for _, pipeline := range ap.GetAll() {
250250
if pipeline.Name == "Pipeline B" {
251251
t.Fatalf("Pipeline B still exists. It should have been removed.")
252252
}

0 commit comments

Comments
 (0)