2020-01-17 21:08:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class ImportFailureService
|
|
|
|
RETRIABLE_EXCEPTIONS = [GRPC::DeadlineExceeded, ActiveRecord::QueryCanceled].freeze
|
|
|
|
|
|
|
|
attr_reader :importable
|
|
|
|
|
|
|
|
def initialize(importable)
|
|
|
|
@importable = importable
|
|
|
|
@association = importable.association(:import_failures)
|
|
|
|
end
|
|
|
|
|
2020-01-24 18:09:00 +00:00
|
|
|
def with_retry(action:, relation_key: nil, relation_index: nil)
|
2020-01-17 21:08:29 +00:00
|
|
|
on_retry = -> (exception, retry_count, *_args) do
|
2020-01-24 18:09:00 +00:00
|
|
|
log_import_failure(
|
|
|
|
source: action,
|
|
|
|
relation_key: relation_key,
|
|
|
|
relation_index: relation_index,
|
|
|
|
exception: exception,
|
|
|
|
retry_count: retry_count)
|
2020-01-17 21:08:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Retriable.with_context(:relation_import, on_retry: on_retry) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-24 18:09:00 +00:00
|
|
|
def log_import_failure(source:, relation_key: nil, relation_index: nil, exception:, retry_count: 0)
|
2020-11-20 21:09:12 +00:00
|
|
|
attributes = {
|
2020-01-17 21:08:29 +00:00
|
|
|
relation_index: relation_index,
|
2020-11-20 21:09:12 +00:00
|
|
|
source: source,
|
|
|
|
retry_count: retry_count,
|
|
|
|
importable_column_name => importable.id
|
2020-01-17 21:08:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 21:09:12 +00:00
|
|
|
Gitlab::ErrorTracking.track_exception(
|
|
|
|
exception,
|
|
|
|
attributes.merge(relation_name: relation_key)
|
|
|
|
)
|
|
|
|
|
|
|
|
ImportFailure.create(
|
|
|
|
attributes.merge(
|
|
|
|
exception_class: exception.class.to_s,
|
|
|
|
exception_message: exception.message.truncate(255),
|
|
|
|
correlation_id_value: Labkit::Correlation::CorrelationId.current_or_new_id,
|
|
|
|
relation_key: relation_key
|
|
|
|
)
|
|
|
|
)
|
2020-01-17 21:08:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def importable_column_name
|
|
|
|
@importable_column_name ||= @association.reflection.foreign_key.to_sym
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|