debian-mirror-gitlab/lib/bulk_imports/pipeline/runner.rb

156 lines
4 KiB
Ruby
Raw Normal View History

2021-01-29 00:20:46 +05:30
# frozen_string_literal: true
module BulkImports
module Pipeline
module Runner
extend ActiveSupport::Concern
2021-02-22 17:27:13 +05:30
MarkedAsFailedError = Class.new(StandardError)
2021-01-29 00:20:46 +05:30
2021-03-11 19:13:27 +05:30
def run
2021-04-17 20:07:23 +05:30
raise MarkedAsFailedError if context.entity.failed?
2021-01-29 00:20:46 +05:30
2021-03-11 19:13:27 +05:30
info(message: 'Pipeline started')
2021-01-29 00:20:46 +05:30
2021-03-11 19:13:27 +05:30
extracted_data = extracted_data_from
2021-04-29 21:17:54 +05:30
if extracted_data
extracted_data.each do |entry|
transformers.each do |transformer|
entry = run_pipeline_step(:transformer, transformer.class.name) do
transformer.transform(context, entry)
end
2021-03-08 18:12:59 +05:30
end
2021-01-29 00:20:46 +05:30
2021-04-29 21:17:54 +05:30
run_pipeline_step(:loader, loader.class.name) do
loader.load(context, entry)
end
2021-02-22 17:27:13 +05:30
end
2021-04-29 21:17:54 +05:30
tracker.update!(
has_next_page: extracted_data.has_next_page?,
next_page: extracted_data.next_page
)
2021-03-11 19:13:27 +05:30
run_pipeline_step(:after_run) do
after_run(extracted_data)
end
end
info(message: 'Pipeline finished')
2021-02-22 17:27:13 +05:30
rescue MarkedAsFailedError
2021-04-29 21:17:54 +05:30
skip!('Skipping pipeline due to failed entity')
2021-01-29 00:20:46 +05:30
end
2021-02-22 17:27:13 +05:30
private # rubocop:disable Lint/UselessAccessModifier
2021-01-29 00:20:46 +05:30
2021-03-11 19:13:27 +05:30
def run_pipeline_step(step, class_name = nil)
2021-04-17 20:07:23 +05:30
raise MarkedAsFailedError if context.entity.failed?
2021-01-29 00:20:46 +05:30
2021-03-11 19:13:27 +05:30
info(pipeline_step: step, step_class: class_name)
2021-01-29 00:20:46 +05:30
2021-02-22 17:27:13 +05:30
yield
rescue MarkedAsFailedError
2021-04-29 21:17:54 +05:30
skip!(
'Skipping pipeline due to failed entity',
pipeline_step: step,
2022-11-25 23:54:43 +05:30
step_class: class_name,
importer: 'gitlab_migration'
2021-04-29 21:17:54 +05:30
)
2022-08-13 15:12:31 +05:30
rescue BulkImports::NetworkError => e
if e.retriable?(context.tracker)
raise BulkImports::RetryPipelineError.new(e.message, e.retry_delay)
else
log_and_fail(e, step)
end
rescue BulkImports::RetryPipelineError
raise
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2022-08-13 15:12:31 +05:30
log_and_fail(e, step)
2021-01-29 00:20:46 +05:30
end
2021-03-11 19:13:27 +05:30
def extracted_data_from
run_pipeline_step(:extractor, extractor.class.name) do
2021-03-08 18:12:59 +05:30
extractor.extract(context)
end
end
2021-04-29 21:17:54 +05:30
def after_run(extracted_data)
run if extracted_data.has_next_page?
end
2022-08-13 15:12:31 +05:30
def log_and_fail(exception, step)
log_import_failure(exception, step)
2021-02-22 17:27:13 +05:30
2021-04-29 21:17:54 +05:30
tracker.fail_op!
2022-08-13 15:12:31 +05:30
if abort_on_failure?
warn(message: 'Aborting entity migration due to pipeline failure')
context.entity.fail_op!
end
nil
2021-02-22 17:27:13 +05:30
end
2021-04-29 21:17:54 +05:30
def skip!(message, extra = {})
warn({ message: message }.merge(extra))
tracker.skip!
2021-02-22 17:27:13 +05:30
end
2021-03-11 19:13:27 +05:30
def log_import_failure(exception, step)
2021-02-22 17:27:13 +05:30
attributes = {
bulk_import_entity_id: context.entity.id,
pipeline_class: pipeline,
2021-03-11 19:13:27 +05:30
pipeline_step: step,
2021-02-22 17:27:13 +05:30
exception_class: exception.class.to_s,
exception_message: exception.message.truncate(255),
correlation_id_value: Labkit::Correlation::CorrelationId.current_or_new_id
}
2021-04-17 20:07:23 +05:30
error(
2022-11-25 23:54:43 +05:30
bulk_import_id: context.bulk_import_id,
2021-04-17 20:07:23 +05:30
pipeline_step: step,
exception_class: exception.class.to_s,
2022-11-25 23:54:43 +05:30
exception_message: exception.message,
message: "Pipeline failed",
importer: 'gitlab_migration'
2021-04-17 20:07:23 +05:30
)
2021-02-22 17:27:13 +05:30
BulkImports::Failure.create(attributes)
end
2021-04-17 20:07:23 +05:30
def info(extra = {})
logger.info(log_params(extra))
end
2021-03-11 19:13:27 +05:30
def warn(extra = {})
logger.warn(log_params(extra))
2021-02-22 17:27:13 +05:30
end
2021-01-29 00:20:46 +05:30
2021-04-17 20:07:23 +05:30
def error(extra = {})
logger.error(log_params(extra))
2021-02-22 17:27:13 +05:30
end
2021-03-11 19:13:27 +05:30
def log_params(extra)
defaults = {
2022-11-25 23:54:43 +05:30
bulk_import_id: context.bulk_import_id,
2021-02-22 17:27:13 +05:30
bulk_import_entity_id: context.entity.id,
2021-03-11 19:13:27 +05:30
bulk_import_entity_type: context.entity.source_type,
2021-04-17 20:07:23 +05:30
pipeline_class: pipeline,
2022-11-25 23:54:43 +05:30
context_extra: context.extra,
importer: 'gitlab_migration'
2021-02-22 17:27:13 +05:30
}
2021-03-11 19:13:27 +05:30
2021-04-17 20:07:23 +05:30
defaults
.merge(extra)
.reject { |_key, value| value.blank? }
2021-01-29 00:20:46 +05:30
end
def logger
@logger ||= Gitlab::Import::Logger.build
end
end
end
end