Welcome to mirror list, hosted at ThFree Co, Russian Federation.

register_build_service.rb « ci « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e0b58a5dc9d7b7006cbcd1eb1ea4691a59d084a (plain)
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
module Ci
  # This class responsible for assigning
  # proper pending build to runner on runner API request
  class RegisterBuildService
    def execute(current_runner)
      builds = Ci::Build.pending.unstarted

      builds =
        if current_runner.shared?
          # don't run projects which have not enables shared runners
          builds.includes(:project).where(projects: { shared_runners_enabled: true })
        else
          # do run projects which are only assigned to this runner
          builds.where(project_id: current_runner.projects)
        end

      builds = builds.order('created_at ASC')

      build = builds.find do |build|
        (build.tag_list - current_runner.tag_list).empty?
      end
        

      if build
        # In case when 2 runners try to assign the same build, second runner will be declined
        # with StateMachine::InvalidTransition in run! method.
        build.with_lock do
          build.runner_id = current_runner.id
          build.save!
          build.run!
        end
      end

      build

    rescue StateMachine::InvalidTransition
      nil
    end
  end
end