debian-mirror-gitlab/app/services/ci/update_build_queue_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
2.6 KiB
Ruby
Raw Normal View History

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)
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)
raise InvalidQueueTransition unless transition.from == 'pending'
2022-05-07 20:08:51 +05:30
transition.within_transaction { remove!(build) }
end
##
2023-05-27 22:25:52 +05:30
# Force remove build from the queue, without checking a transition state
2022-05-07 20:08:51 +05:30
#
def remove!(build)
removed = build.all_queuing_entries.delete_all
2021-09-04 01:27:46 +05:30
2022-05-07 20:08:51 +05:30
if removed > 0
metrics.increment_queue_operation(:build_queue_pop)
2021-09-04 01:27:46 +05:30
2022-05-07 20:08:51 +05:30
build.id
2021-09-04 01:27:46 +05:30
end
end
##
# Add shared runner build tracking entry (used for queuing).
#
def track(build, transition)
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 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
end
end