2020-10-23 11:08:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
module Pipeline
|
|
|
|
module Runner
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2020-11-30 10:09:21 -05:00
|
|
|
MarkedAsFailedError = Class.new(StandardError)
|
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
def run
|
2021-03-10 19:09:38 -05:00
|
|
|
raise MarkedAsFailedError if context.entity.failed?
|
2020-11-30 10:09:21 -05:00
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
info(message: 'Pipeline started')
|
2020-11-16 07:09:05 -05:00
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
extracted_data = extracted_data_from
|
2021-02-01 07:09:03 -05:00
|
|
|
|
2021-03-23 14:09:05 -04:00
|
|
|
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-01-12 01:10:31 -05:00
|
|
|
end
|
2020-11-16 07:09:05 -05:00
|
|
|
|
2021-03-23 14:09:05 -04:00
|
|
|
run_pipeline_step(:loader, loader.class.name) do
|
|
|
|
loader.load(context, entry)
|
|
|
|
end
|
2020-11-16 07:09:05 -05:00
|
|
|
end
|
2020-11-19 19:09:06 -05:00
|
|
|
|
2021-03-23 14:09:05 -04:00
|
|
|
tracker.update!(
|
|
|
|
has_next_page: extracted_data.has_next_page?,
|
|
|
|
next_page: extracted_data.next_page
|
|
|
|
)
|
|
|
|
|
2021-02-16 07:09:03 -05:00
|
|
|
run_pipeline_step(:after_run) do
|
|
|
|
after_run(extracted_data)
|
|
|
|
end
|
|
|
|
end
|
2021-02-04 07:09:25 -05:00
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
info(message: 'Pipeline finished')
|
2020-11-30 10:09:21 -05:00
|
|
|
rescue MarkedAsFailedError
|
2021-03-18 11:09:04 -04:00
|
|
|
skip!('Skipping pipeline due to failed entity')
|
2020-11-16 07:09:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private # rubocop:disable Lint/UselessAccessModifier
|
|
|
|
|
2021-02-16 07:09:03 -05:00
|
|
|
def run_pipeline_step(step, class_name = nil)
|
2021-03-10 19:09:38 -05:00
|
|
|
raise MarkedAsFailedError if context.entity.failed?
|
2020-11-30 10:09:21 -05:00
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
info(pipeline_step: step, step_class: class_name)
|
2020-11-30 10:09:21 -05:00
|
|
|
|
|
|
|
yield
|
|
|
|
rescue MarkedAsFailedError
|
2021-03-18 11:09:04 -04:00
|
|
|
skip!(
|
|
|
|
'Skipping pipeline due to failed entity',
|
|
|
|
pipeline_step: step,
|
|
|
|
step_class: class_name
|
|
|
|
)
|
2022-07-05 08:09:46 -04:00
|
|
|
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-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2022-07-05 08:09:46 -04:00
|
|
|
log_and_fail(e, step)
|
2020-11-30 10:09:21 -05:00
|
|
|
end
|
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
def extracted_data_from
|
|
|
|
run_pipeline_step(:extractor, extractor.class.name) do
|
2021-01-12 01:10:31 -05:00
|
|
|
extractor.extract(context)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-23 14:09:05 -04:00
|
|
|
def after_run(extracted_data)
|
|
|
|
run if extracted_data.has_next_page?
|
|
|
|
end
|
|
|
|
|
2022-07-05 08:09:46 -04:00
|
|
|
def log_and_fail(exception, step)
|
|
|
|
log_import_failure(exception, step)
|
2020-11-30 10:09:21 -05:00
|
|
|
|
2021-03-18 11:09:04 -04:00
|
|
|
tracker.fail_op!
|
2022-07-05 08:09:46 -04:00
|
|
|
|
|
|
|
if abort_on_failure?
|
|
|
|
warn(message: 'Aborting entity migration due to pipeline failure')
|
|
|
|
context.entity.fail_op!
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
2020-11-30 10:09:21 -05:00
|
|
|
end
|
|
|
|
|
2021-03-18 11:09:04 -04:00
|
|
|
def skip!(message, extra = {})
|
|
|
|
warn({ message: message }.merge(extra))
|
|
|
|
|
|
|
|
tracker.skip!
|
2020-11-30 10:09:21 -05:00
|
|
|
end
|
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
def log_import_failure(exception, step)
|
2020-11-30 10:09:21 -05:00
|
|
|
attributes = {
|
|
|
|
bulk_import_entity_id: context.entity.id,
|
|
|
|
pipeline_class: pipeline,
|
2021-01-27 19:09:33 -05:00
|
|
|
pipeline_step: step,
|
2020-11-30 10:09:21 -05:00
|
|
|
exception_class: exception.class.to_s,
|
|
|
|
exception_message: exception.message.truncate(255),
|
|
|
|
correlation_id_value: Labkit::Correlation::CorrelationId.current_or_new_id
|
|
|
|
}
|
|
|
|
|
2021-03-10 19:09:38 -05:00
|
|
|
error(
|
|
|
|
pipeline_step: step,
|
|
|
|
exception_class: exception.class.to_s,
|
|
|
|
exception_message: exception.message
|
|
|
|
)
|
|
|
|
|
2020-11-30 10:09:21 -05:00
|
|
|
BulkImports::Failure.create(attributes)
|
|
|
|
end
|
|
|
|
|
2021-03-10 19:09:38 -05:00
|
|
|
def info(extra = {})
|
|
|
|
logger.info(log_params(extra))
|
|
|
|
end
|
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
def warn(extra = {})
|
2021-02-16 07:09:03 -05:00
|
|
|
logger.warn(log_params(extra))
|
2020-11-30 10:09:21 -05:00
|
|
|
end
|
|
|
|
|
2021-03-10 19:09:38 -05:00
|
|
|
def error(extra = {})
|
|
|
|
logger.error(log_params(extra))
|
2020-11-30 10:09:21 -05:00
|
|
|
end
|
|
|
|
|
2021-02-16 07:09:03 -05:00
|
|
|
def log_params(extra)
|
|
|
|
defaults = {
|
2021-03-10 19:09:38 -05:00
|
|
|
bulk_import_id: context.bulk_import.id,
|
2020-11-30 10:09:21 -05:00
|
|
|
bulk_import_entity_id: context.entity.id,
|
2021-02-04 07:09:25 -05:00
|
|
|
bulk_import_entity_type: context.entity.source_type,
|
2021-03-10 19:09:38 -05:00
|
|
|
pipeline_class: pipeline,
|
|
|
|
context_extra: context.extra
|
2020-11-30 10:09:21 -05:00
|
|
|
}
|
2021-02-16 07:09:03 -05:00
|
|
|
|
2021-03-10 19:09:38 -05:00
|
|
|
defaults
|
|
|
|
.merge(extra)
|
|
|
|
.reject { |_key, value| value.blank? }
|
2020-11-16 07:09:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def logger
|
|
|
|
@logger ||= Gitlab::Import::Logger.build
|
|
|
|
end
|
2020-10-23 11:08:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|