builds_helper.go 1.11 KB
Newer Older
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
package commands

import (
	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
	"sync"
)

type buildsHelper struct {
	builds     []*common.Build
	buildsLock sync.Mutex
}

func (b *buildsHelper) count(runner *common.RunnerConfig) int {
	count := 0
	for _, build := range b.builds {
		if build.Runner.ShortDescription() == runner.ShortDescription() {
			count++
		}
	}
	return count
}

func (b *buildsHelper) acquire(runner *runnerAcquire) (build *common.Build) {
	b.buildsLock.Lock()
	defer b.buildsLock.Unlock()

	// Check number of builds
	count := b.count(&runner.RunnerConfig)
	if runner.Limit > 0 && count >= runner.Limit {
		// Too many builds
		return
	}

	// Create a new build
	build = &common.Build{
		Runner:       &runner.RunnerConfig,
		ExecutorData: runner.data,
	}
	build.AssignID(b.builds...)
	b.builds = append(b.builds, build)
	return
}

func (b *buildsHelper) release(deleteBuild *common.Build) bool {
	b.buildsLock.Lock()
	defer b.buildsLock.Unlock()

	for idx, build := range b.builds {
		if build == deleteBuild {
			b.builds = append(b.builds[0:idx], b.builds[idx+1:]...)
			return true
		}
	}
	return false
}