2021-10-27 15:23:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
class BuildTraceMetadata < Ci::ApplicationRecord
|
2023-03-04 22:38:38 +05:30
|
|
|
include Ci::Partitionable
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
MAX_ATTEMPTS = 5
|
2021-10-27 15:23:28 +05:30
|
|
|
self.table_name = 'ci_build_trace_metadata'
|
|
|
|
self.primary_key = :build_id
|
|
|
|
|
|
|
|
belongs_to :build, class_name: 'Ci::Build'
|
|
|
|
belongs_to :trace_artifact, class_name: 'Ci::JobArtifact'
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
partitionable scope: :build
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
validates :build, presence: true
|
2021-11-11 11:23:49 +05:30
|
|
|
validates :archival_attempts, presence: true
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def self.find_or_upsert_for!(build_id, partition_id)
|
|
|
|
record = find_by(build_id: build_id, partition_id: partition_id)
|
2021-11-11 11:23:49 +05:30
|
|
|
return record if record
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
upsert({ build_id: build_id, partition_id: partition_id }, unique_by: :build_id)
|
|
|
|
find_by!(build_id: build_id, partition_id: partition_id)
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# The job is retried around 5 times during the 7 days retention period for
|
|
|
|
# trace chunks as defined in `Ci::BuildTraceChunks::RedisBase::CHUNK_REDIS_TTL`
|
|
|
|
def can_attempt_archival_now?
|
|
|
|
return false unless archival_attempts_available?
|
|
|
|
return true unless last_archival_attempt_at
|
|
|
|
|
|
|
|
last_archival_attempt_at + backoff < Time.current
|
|
|
|
end
|
|
|
|
|
|
|
|
def archival_attempts_available?
|
|
|
|
archival_attempts <= MAX_ATTEMPTS
|
|
|
|
end
|
|
|
|
|
|
|
|
def increment_archival_attempts!
|
|
|
|
increment!(:archival_attempts, touch: :last_archival_attempt_at)
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
def track_archival!(trace_artifact_id, checksum)
|
2023-06-20 00:43:36 +05:30
|
|
|
update!(trace_artifact_id: trace_artifact_id, checksum: checksum, archived_at: Time.current)
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def archival_attempts_message
|
|
|
|
if archival_attempts_available?
|
|
|
|
'The job can not be archived right now.'
|
|
|
|
else
|
|
|
|
'The job is out of archival attempts.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
def remote_checksum_valid?
|
|
|
|
checksum.present? &&
|
|
|
|
checksum == remote_checksum
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def backoff
|
|
|
|
::Gitlab::Ci::Trace::Backoff.new(archival_attempts).value_with_jitter
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
end
|