2015-09-25 12:07:36 +05:30
|
|
|
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
|
2015-10-24 18:46:33 +05:30
|
|
|
builds.joins(commit: { gl_project: :gitlab_ci_project }).where(ci_projects: { shared_runners_enabled: true })
|
2015-09-25 12:07:36 +05:30
|
|
|
else
|
|
|
|
# do run projects which are only assigned to this runner
|
2015-10-24 18:46:33 +05:30
|
|
|
builds.joins(:commit).where(ci_commits: { gl_project_id: current_runner.gl_projects_ids })
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
builds = builds.order('created_at ASC')
|
|
|
|
|
|
|
|
build = builds.find do |build|
|
2015-10-24 18:46:33 +05:30
|
|
|
build.can_be_served?(current_runner)
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
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
|