2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-15 16:37:52 -05:00
|
|
|
module Ci
|
2016-12-20 22:27:03 -05:00
|
|
|
class UpdateBuildQueueService
|
2021-05-19 11:10:40 -04:00
|
|
|
InvalidQueueTransition = Class.new(StandardError)
|
|
|
|
|
|
|
|
attr_reader :metrics
|
|
|
|
|
|
|
|
def initialize(metrics = ::Gitlab::Ci::Queue::Metrics)
|
|
|
|
@metrics = metrics
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Add a build to the pending builds queue
|
|
|
|
#
|
|
|
|
def push(build, transition)
|
|
|
|
return unless maintain_pending_builds_queue?(build)
|
|
|
|
|
|
|
|
raise InvalidQueueTransition unless transition.to == 'pending'
|
|
|
|
|
|
|
|
transition.within_transaction do
|
2021-06-08 05:09:56 -04:00
|
|
|
result = build.create_queuing_entry!
|
2021-05-19 11:10:40 -04:00
|
|
|
|
|
|
|
unless result.empty?
|
|
|
|
metrics.increment_queue_operation(:build_queue_push)
|
|
|
|
|
|
|
|
result.rows.dig(0, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Remove a build from the pending builds queue
|
|
|
|
#
|
|
|
|
def pop(build, transition)
|
|
|
|
return unless maintain_pending_builds_queue?(build)
|
|
|
|
|
|
|
|
raise InvalidQueueTransition unless transition.from == 'pending'
|
|
|
|
|
|
|
|
transition.within_transaction do
|
|
|
|
removed = build.all_queuing_entries.delete_all
|
|
|
|
|
|
|
|
if removed > 0
|
|
|
|
metrics.increment_queue_operation(:build_queue_pop)
|
|
|
|
|
|
|
|
build.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-11 11:09:58 -04:00
|
|
|
##
|
|
|
|
# Add shared runner build tracking entry (used for queuing).
|
|
|
|
#
|
|
|
|
def track(build, transition)
|
|
|
|
return unless Feature.enabled?(:ci_track_shared_runner_builds, build.project, default_enabled: :yaml)
|
|
|
|
return unless build.shared_runner_build?
|
|
|
|
|
|
|
|
raise InvalidQueueTransition unless transition.to == 'running'
|
|
|
|
|
|
|
|
transition.within_transaction do
|
|
|
|
result = ::Ci::RunningBuild.upsert_shared_runner_build!(build)
|
|
|
|
|
|
|
|
unless result.empty?
|
|
|
|
metrics.increment_queue_operation(:shared_runner_build_new)
|
|
|
|
|
|
|
|
result.rows.dig(0, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Remove a runtime build tracking entry for a shared runner build (used for
|
|
|
|
# queuing).
|
|
|
|
#
|
|
|
|
def untrack(build, transition)
|
|
|
|
return unless Feature.enabled?(:ci_untrack_shared_runner_builds, build.project, default_enabled: :yaml)
|
|
|
|
return unless build.shared_runner_build?
|
|
|
|
|
|
|
|
raise InvalidQueueTransition unless transition.from == 'running'
|
|
|
|
|
|
|
|
transition.within_transaction do
|
|
|
|
removed = build.all_runtime_metadata.delete_all
|
|
|
|
|
|
|
|
if removed > 0
|
|
|
|
metrics.increment_queue_operation(:shared_runner_build_done)
|
|
|
|
|
|
|
|
build.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
##
|
|
|
|
# Unblock runner associated with given project / build
|
|
|
|
#
|
|
|
|
def tick(build)
|
2021-05-31 11:11:12 -04:00
|
|
|
tick_for(build, build.project.all_available_runners)
|
2017-10-30 11:17:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
def tick_for(build, runners)
|
2020-09-29 11:10:08 -04:00
|
|
|
runners = runners.with_recent_runner_queue
|
2021-03-02 19:10:50 -05:00
|
|
|
runners = runners.with_tags if Feature.enabled?(:ci_preload_runner_tags, default_enabled: :yaml)
|
|
|
|
|
2021-02-26 13:11:10 -05:00
|
|
|
metrics.observe_active_runners(-> { runners.to_a.size })
|
2019-08-22 07:08:46 -04:00
|
|
|
|
2017-10-30 11:17:17 -04:00
|
|
|
runners.each do |runner|
|
2021-02-26 13:11:10 -05:00
|
|
|
metrics.increment_runner_tick(runner)
|
|
|
|
|
2018-05-02 10:42:12 -04:00
|
|
|
runner.pick_build!(build)
|
2017-01-20 08:32:32 -05:00
|
|
|
end
|
2016-12-15 16:37:52 -05:00
|
|
|
end
|
2021-05-19 11:10:40 -04:00
|
|
|
|
|
|
|
def maintain_pending_builds_queue?(build)
|
|
|
|
Feature.enabled?(:ci_pending_builds_queue_maintain, build.project, default_enabled: :yaml)
|
|
|
|
end
|
2016-12-15 16:37:52 -05:00
|
|
|
end
|
|
|
|
end
|