debian-mirror-gitlab/lib/gitlab/import/import_failure_service.rb

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

98 lines
2.5 KiB
Ruby
Raw Normal View History

2021-10-27 15:23:28 +05:30
# frozen_string_literal: true
module Gitlab
module Import
class ImportFailureService
def self.track(
exception:,
import_state: nil,
project_id: nil,
error_source: nil,
2021-11-18 22:05:49 +05:30
fail_import: false,
metrics: false
2021-10-27 15:23:28 +05:30
)
new(
exception: exception,
import_state: import_state,
project_id: project_id,
2022-01-26 12:08:38 +05:30
error_source: error_source,
fail_import: fail_import,
metrics: metrics
).execute
2021-10-27 15:23:28 +05:30
end
2022-01-26 12:08:38 +05:30
def initialize(
exception:,
import_state: nil,
project_id: nil,
error_source: nil,
fail_import: false,
metrics: false
)
2021-10-27 15:23:28 +05:30
if import_state.blank? && project_id.blank?
raise ArgumentError, 'import_state OR project_id must be provided'
end
if project_id.blank?
@import_state = import_state
@project = import_state.project
else
@project = Project.find(project_id)
@import_state = @project.import_state
end
@exception = exception
@error_source = error_source
2022-01-26 12:08:38 +05:30
@fail_import = fail_import
@metrics = metrics
2021-10-27 15:23:28 +05:30
end
2022-01-26 12:08:38 +05:30
def execute
2021-10-27 15:23:28 +05:30
track_exception
persist_failure
2021-11-18 22:05:49 +05:30
track_metrics if metrics
2021-10-27 15:23:28 +05:30
import_state.mark_as_failed(exception.message) if fail_import
end
private
2022-01-26 12:08:38 +05:30
attr_reader :exception, :import_state, :project, :error_source, :fail_import, :metrics
2021-10-27 15:23:28 +05:30
def track_exception
attributes = {
import_type: project.import_type,
project_id: project.id,
source: error_source
}
Gitlab::Import::Logger.error(
attributes.merge(
message: 'importer failed',
'error.message': exception.message
)
)
Gitlab::ErrorTracking.track_exception(exception, attributes)
end
2022-01-26 12:08:38 +05:30
# Failures with `retry_count: 0` are considered "hard_failures" and those
# are exposed on the REST API projects/:id/import
2021-10-27 15:23:28 +05:30
def persist_failure
project.import_failures.create(
source: error_source,
exception_class: exception.class.to_s,
exception_message: exception.message.truncate(255),
2022-01-26 12:08:38 +05:30
correlation_id_value: Labkit::Correlation::CorrelationId.current_or_new_id,
retry_count: fail_import ? 0 : nil
2021-10-27 15:23:28 +05:30
)
end
2021-11-18 22:05:49 +05:30
def track_metrics
Gitlab::Import::Metrics.new("#{project.import_type}_importer", project).track_failed_import
end
2021-10-27 15:23:28 +05:30
end
end
end