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

111 lines
3 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
module Ci
# 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-10-27 15:23:28 +05:30
class BuildMetadata < Ci::ApplicationRecord
2019-12-04 20:38:33 +05:30
BuildTimeout = Struct.new(:value, :source)
2022-11-25 23:54:43 +05:30
include Ci::Partitionable
2018-05-09 12:01:36 +05:30
include Presentable
include ChronicDurationAttribute
2019-12-04 20:38:33 +05:30
include Gitlab::Utils::StrongMemoize
2023-05-27 22:25:52 +05:30
include IgnorableColumns
2023-06-20 00:43:36 +05:30
include SafelyChangeColumnDefault
2018-05-09 12:01:36 +05:30
2023-03-04 22:38:38 +05:30
self.table_name = 'p_ci_builds_metadata'
2022-11-25 23:54:43 +05:30
self.primary_key = 'id'
2023-06-20 00:43:36 +05:30
columns_changing_default :partition_id
2023-01-13 00:05:48 +05:30
2023-03-04 22:38:38 +05:30
partitionable scope: :build
2018-05-09 12:01:36 +05:30
2023-05-27 22:25:52 +05:30
ignore_column :runner_machine_id, remove_with: '16.0', remove_after: '2023-04-22'
2019-03-02 22:35:43 +05:30
belongs_to :build, class_name: 'CommitStatus'
2018-05-09 12:01:36 +05:30
belongs_to :project
2019-02-15 15:39:39 +05:30
before_create :set_build_project
2018-05-09 12:01:36 +05:30
validates :build, presence: true
2022-08-27 11:52:29 +05:30
validates :id_tokens, json_schema: { filename: 'build_metadata_id_tokens' }
2020-07-28 23:09:34 +05:30
validates :secrets, json_schema: { filename: 'build_metadata_secrets' }
2019-02-15 15:39:39 +05:30
2023-01-13 00:05:48 +05:30
attribute :config_options, :sym_jsonb
attribute :config_variables, :sym_jsonb
attribute :runtime_runner_features, :sym_jsonb
2018-05-09 12:01:36 +05:30
chronic_duration_attr_reader :timeout_human_readable, :timeout
2022-11-25 23:54:43 +05:30
scope :scoped_build, -> { where("#{quoted_table_name}.build_id = #{Ci::Build.quoted_table_name}.id") }
2019-12-04 20:38:33 +05:30
scope :with_interruptible, -> { where(interruptible: true) }
2019-12-26 22:10:19 +05:30
scope :with_exposed_artifacts, -> { where(has_exposed_artifacts: true) }
2019-12-04 20:38:33 +05:30
2018-05-09 12:01:36 +05:30
enum timeout_source: {
unknown_timeout_source: 1,
project_timeout_source: 2,
2019-12-04 20:38:33 +05:30
runner_timeout_source: 3,
job_timeout_source: 4
2018-05-09 12:01:36 +05:30
}
def update_timeout_state
2019-12-04 20:38:33 +05:30
timeout = timeout_with_highest_precedence
2018-05-09 12:01:36 +05:30
2019-12-04 20:38:33 +05:30
return unless timeout
2018-05-09 12:01:36 +05:30
2019-12-04 20:38:33 +05:30
update(timeout: timeout.value, timeout_source: timeout.source)
2018-05-09 12:01:36 +05:30
end
2019-02-15 15:39:39 +05:30
2021-12-11 22:18:48 +05:30
def set_cancel_gracefully
2023-01-13 00:05:48 +05:30
runtime_runner_features.merge!({ cancel_gracefully: true })
2021-12-11 22:18:48 +05:30
end
def cancel_gracefully?
runtime_runner_features[:cancel_gracefully] == true
end
2023-03-04 22:38:38 +05:30
def enable_debug_trace!
self.debug_trace_enabled = true
save! if changes.any?
true
end
2019-02-15 15:39:39 +05:30
private
def set_build_project
2023-04-23 21:23:45 +05:30
self.project_id ||= build.project_id
2019-02-15 15:39:39 +05:30
end
2019-12-04 20:38:33 +05:30
def timeout_with_highest_precedence
2023-03-17 16:20:25 +05:30
[(job_timeout || project_timeout), runner_timeout].compact.min_by(&:value)
2019-12-04 20:38:33 +05:30
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-05-09 12:01:36 +05:30
end
end