debian-mirror-gitlab/app/models/ci/resource_group.rb

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

96 lines
2.9 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module Ci
2021-10-27 15:23:28 +05:30
class ResourceGroup < Ci::ApplicationRecord
2020-03-13 15:44:24 +05:30
belongs_to :project, inverse_of: :resource_groups
has_many :resources, class_name: 'Ci::Resource', inverse_of: :resource_group
2021-03-11 19:13:27 +05:30
has_many :processables, class_name: 'Ci::Processable', inverse_of: :resource_group
2020-03-13 15:44:24 +05:30
validates :key,
length: { maximum: 255 },
format: { with: Gitlab::Regex.environment_name_regex,
message: Gitlab::Regex.environment_name_regex_message }
before_create :ensure_resource
2021-11-18 22:05:49 +05:30
enum process_mode: {
unordered: 0,
oldest_first: 1,
newest_first: 2
}
2020-03-13 15:44:24 +05:30
##
# NOTE: This is concurrency-safe method that the subquery in the `UPDATE`
# works as explicit locking.
2021-03-11 19:13:27 +05:30
def assign_resource_to(processable)
2023-03-04 22:38:38 +05:30
attrs = {
build_id: processable.id,
partition_id: processable.partition_id
}
2023-05-27 22:25:52 +05:30
success = resources.free.limit(1).update_all(attrs) > 0
log_event(success: success, processable: processable, action: "assign resource to processable")
success
2020-03-13 15:44:24 +05:30
end
2021-03-11 19:13:27 +05:30
def release_resource_from(processable)
2023-03-04 22:38:38 +05:30
attrs = { build_id: nil, partition_id: nil }
2023-05-27 22:25:52 +05:30
success = resources.retained_by(processable).update_all(attrs) > 0
log_event(success: success, processable: processable, action: "release resource from processable")
success
2020-03-13 15:44:24 +05:30
end
2021-11-18 22:05:49 +05:30
def upcoming_processables
if unordered?
processables.waiting_for_resource
elsif oldest_first?
processables.waiting_for_resource_or_upcoming
.order(Arel.sql("commit_id ASC, #{sort_by_job_status}"))
elsif newest_first?
processables.waiting_for_resource_or_upcoming
.order(Arel.sql("commit_id DESC, #{sort_by_job_status}"))
else
Ci::Processable.none
end
end
2023-06-20 00:43:36 +05:30
def current_processable
Ci::Processable.find_by('(id, partition_id) IN (?)', resources.select('build_id, partition_id'))
end
2020-03-13 15:44:24 +05:30
private
2021-11-18 22:05:49 +05:30
# In order to avoid deadlock, we do NOT specify the job execution order in the same pipeline.
# The system processes wherever ready to transition to `pending` status from `waiting_for_resource`.
# See https://gitlab.com/gitlab-org/gitlab/-/issues/202186 for more information.
def sort_by_job_status
<<~SQL
CASE status
WHEN 'waiting_for_resource' THEN 0
ELSE 1
END ASC
SQL
end
2020-03-13 15:44:24 +05:30
def ensure_resource
# Currently we only support one resource per group, which means
# maximum one build can be set to the resource group, thus builds
# belong to the same resource group are executed once at time.
self.resources.build if self.resources.empty?
end
2023-05-27 22:25:52 +05:30
def log_event(success:, processable:, action:)
Gitlab::Ci::ResourceGroups::Logger.build.info({
resource_group_id: self.id,
processable_id: processable.id,
message: "attempted to #{action}",
success: success
})
end
2020-03-13 15:44:24 +05:30
end
end