2018-08-03 07:15:25 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-01 02:12:32 +00:00
|
|
|
module Ci
|
2018-03-05 16:35:06 +00:00
|
|
|
# The purpose of this class is to store Build related data that can be disposed.
|
|
|
|
# Data that should be persisted forever, should be stored with Ci::Build model.
|
2021-08-03 06:08:50 +00:00
|
|
|
class BuildMetadata < Ci::ApplicationRecord
|
2019-09-16 18:06:05 +00:00
|
|
|
BuildTimeout = Struct.new(:value, :source)
|
2022-10-28 18:10:48 +00:00
|
|
|
ROUTING_FEATURE_FLAG = :ci_partitioning_use_ci_builds_metadata_routing_table
|
2019-09-16 18:06:05 +00:00
|
|
|
|
2022-10-03 18:08:19 +00:00
|
|
|
include Ci::Partitionable
|
2018-03-01 02:12:32 +00:00
|
|
|
include Presentable
|
|
|
|
include ChronicDurationAttribute
|
2019-09-16 18:06:05 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2018-03-01 02:12:32 +00:00
|
|
|
|
|
|
|
self.table_name = 'ci_builds_metadata'
|
2022-09-23 15:11:09 +00:00
|
|
|
self.primary_key = 'id'
|
2022-10-20 09:10:20 +00:00
|
|
|
self.sequence_name = 'ci_builds_metadata_id_seq'
|
2022-10-28 18:10:48 +00:00
|
|
|
|
|
|
|
partitionable scope: :build, through: {
|
|
|
|
table: :p_ci_builds_metadata,
|
|
|
|
flag: ROUTING_FEATURE_FLAG
|
|
|
|
}
|
2018-03-01 02:12:32 +00:00
|
|
|
|
2019-01-29 13:40:47 +00:00
|
|
|
belongs_to :build, class_name: 'CommitStatus'
|
2018-03-21 00:20:15 +00:00
|
|
|
belongs_to :project
|
2018-03-01 02:12:32 +00:00
|
|
|
|
2018-09-02 14:35:15 +00:00
|
|
|
before_create :set_build_project
|
|
|
|
|
2018-03-26 16:58:35 +00:00
|
|
|
validates :build, presence: true
|
2022-08-11 15:11:19 +00:00
|
|
|
validates :id_tokens, json_schema: { filename: 'build_metadata_id_tokens' }
|
2020-06-22 12:08:47 +00:00
|
|
|
validates :secrets, json_schema: { filename: 'build_metadata_secrets' }
|
2018-09-02 14:35:15 +00:00
|
|
|
|
2022-11-01 03:09:22 +00:00
|
|
|
attribute :config_options, :sym_jsonb
|
|
|
|
attribute :config_variables, :sym_jsonb
|
|
|
|
attribute :runtime_runner_features, :sym_jsonb
|
2018-03-01 02:12:32 +00:00
|
|
|
|
2018-03-22 16:52:28 +00:00
|
|
|
chronic_duration_attr_reader :timeout_human_readable, :timeout
|
2018-03-21 00:20:15 +00:00
|
|
|
|
2022-10-05 18:08:18 +00:00
|
|
|
scope :scoped_build, -> { where("#{quoted_table_name}.build_id = #{Ci::Build.quoted_table_name}.id") }
|
2019-09-17 14:16:34 +00:00
|
|
|
scope :with_interruptible, -> { where(interruptible: true) }
|
2019-10-21 15:05:58 +00:00
|
|
|
scope :with_exposed_artifacts, -> { where(has_exposed_artifacts: true) }
|
2019-09-17 14:16:34 +00:00
|
|
|
|
2018-03-01 02:12:32 +00:00
|
|
|
enum timeout_source: {
|
2018-03-05 16:35:06 +00:00
|
|
|
unknown_timeout_source: 1,
|
|
|
|
project_timeout_source: 2,
|
2019-09-16 18:06:05 +00:00
|
|
|
runner_timeout_source: 3,
|
|
|
|
job_timeout_source: 4
|
2018-03-01 02:12:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 15:47:46 +00:00
|
|
|
def update_timeout_state
|
2019-09-16 18:06:05 +00:00
|
|
|
timeout = timeout_with_highest_precedence
|
2018-03-05 16:35:06 +00:00
|
|
|
|
2019-09-16 18:06:05 +00:00
|
|
|
return unless timeout
|
2018-03-05 16:35:06 +00:00
|
|
|
|
2019-09-16 18:06:05 +00:00
|
|
|
update(timeout: timeout.value, timeout_source: timeout.source)
|
2018-03-21 00:20:15 +00:00
|
|
|
end
|
2018-09-02 14:35:15 +00:00
|
|
|
|
2021-10-28 18:14:18 +00:00
|
|
|
def set_cancel_gracefully
|
2022-10-25 09:10:14 +00:00
|
|
|
runtime_runner_features.merge!({ cancel_gracefully: true })
|
2021-10-28 18:14:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def cancel_gracefully?
|
|
|
|
runtime_runner_features[:cancel_gracefully] == true
|
|
|
|
end
|
|
|
|
|
2018-09-02 14:35:15 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_build_project
|
|
|
|
self.project_id ||= self.build.project_id
|
|
|
|
end
|
2019-09-16 18:06:05 +00:00
|
|
|
|
|
|
|
def timeout_with_highest_precedence
|
|
|
|
[(job_timeout || project_timeout), runner_timeout].compact.min_by { |timeout| timeout.value }
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_timeout
|
|
|
|
strong_memoize(:project_timeout) do
|
|
|
|
BuildTimeout.new(project&.build_timeout, :project_timeout_source)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def job_timeout
|
|
|
|
return unless build.options
|
|
|
|
|
|
|
|
strong_memoize(:job_timeout) do
|
|
|
|
if timeout_from_options = build.options[:job_timeout]
|
|
|
|
BuildTimeout.new(timeout_from_options, :job_timeout_source)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def runner_timeout
|
|
|
|
return unless runner_timeout_set?
|
|
|
|
|
|
|
|
strong_memoize(:runner_timeout) do
|
|
|
|
BuildTimeout.new(build.runner.maximum_timeout, :runner_timeout_source)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def runner_timeout_set?
|
|
|
|
build.runner&.maximum_timeout.to_i > 0
|
|
|
|
end
|
2018-03-01 02:12:32 +00:00
|
|
|
end
|
|
|
|
end
|