2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Ci
|
|
|
|
class UpdateBuildQueueService
|
2021-09-04 01:27:46 +05:30
|
|
|
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)
|
2022-01-26 12:08:38 +05:30
|
|
|
return unless maintain_pending_builds_queue?
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
raise InvalidQueueTransition unless transition.to == 'pending'
|
|
|
|
|
|
|
|
transition.within_transaction do
|
|
|
|
result = build.create_queuing_entry!
|
|
|
|
|
|
|
|
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)
|
2022-01-26 12:08:38 +05:30
|
|
|
return unless maintain_pending_builds_queue?
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
##
|
|
|
|
# Add shared runner build tracking entry (used for queuing).
|
|
|
|
#
|
|
|
|
def track(build, transition)
|
2022-01-26 12:08:38 +05:30
|
|
|
return unless maintain_pending_builds_queue?
|
2021-09-04 01:27:46 +05:30
|
|
|
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)
|
2022-01-26 12:08:38 +05:30
|
|
|
return unless maintain_pending_builds_queue?
|
2021-09-04 01:27:46 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
##
|
|
|
|
# Unblock runner associated with given project / build
|
|
|
|
#
|
|
|
|
def tick(build)
|
|
|
|
tick_for(build, build.project.all_available_runners)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
private
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def tick_for(build, runners)
|
2022-03-02 08:16:31 +05:30
|
|
|
runners = runners.with_recent_runner_queue
|
2022-04-04 11:22:00 +05:30
|
|
|
runners = runners.with_tags
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
metrics.observe_active_runners(-> { runners.to_a.size })
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
runners.each do |runner|
|
|
|
|
metrics.increment_runner_tick(runner)
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
runner.pick_build!(build)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
def maintain_pending_builds_queue?
|
|
|
|
::Ci::PendingBuild.maintain_denormalized_data?
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|