2020-10-23 11:08:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
module Pipeline
|
|
|
|
extend ActiveSupport::Concern
|
2021-02-22 13:10:55 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-11-20 04:09:06 -05:00
|
|
|
include Gitlab::ClassAttributes
|
2021-02-12 13:08:59 -05:00
|
|
|
include Runner
|
2020-10-23 11:08:42 -04:00
|
|
|
|
2021-03-01 13:11:21 -05:00
|
|
|
NotAllowedError = Class.new(StandardError)
|
2021-05-27 14:10:52 -04:00
|
|
|
ExpiredError = Class.new(StandardError)
|
|
|
|
FailedError = Class.new(StandardError)
|
2021-03-01 13:11:21 -05:00
|
|
|
|
2021-03-02 10:10:57 -05:00
|
|
|
CACHE_KEY_EXPIRATION = 2.hours
|
2021-05-27 14:10:52 -04:00
|
|
|
NDJSON_EXPORT_TIMEOUT = 30.minutes
|
2021-03-02 10:10:57 -05:00
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
def initialize(context)
|
|
|
|
@context = context
|
|
|
|
end
|
2020-11-20 04:09:06 -05:00
|
|
|
|
2021-03-18 11:09:04 -04:00
|
|
|
def tracker
|
|
|
|
@tracker ||= context.tracker
|
|
|
|
end
|
|
|
|
|
2021-05-27 14:10:52 -04:00
|
|
|
def portable
|
|
|
|
@portable ||= context.portable
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_export_config
|
|
|
|
@import_export_config ||= context.import_export_config
|
|
|
|
end
|
|
|
|
|
2021-06-02 14:10:01 -04:00
|
|
|
def current_user
|
|
|
|
@current_user ||= context.current_user
|
|
|
|
end
|
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
included do
|
2020-11-20 04:09:06 -05:00
|
|
|
private
|
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
attr_reader :context
|
|
|
|
|
2021-02-19 07:11:06 -05:00
|
|
|
# Fetch pipeline extractor.
|
|
|
|
# An extractor is defined either by instance `#extract(context)` method
|
|
|
|
# or by using `extractor` DSL.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# class MyPipeline
|
|
|
|
# extractor MyExtractor, foo: :bar
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# class MyPipeline
|
|
|
|
# def extract(context)
|
|
|
|
# puts 'Fetch some data'
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# If pipeline implements instance method `extract` - use it
|
|
|
|
# and ignore class `extractor` method implementation.
|
2021-01-12 01:10:31 -05:00
|
|
|
def extractor
|
2021-02-19 07:11:06 -05:00
|
|
|
@extractor ||= self.respond_to?(:extract) ? self : instantiate(self.class.get_extractor)
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
2021-02-19 07:11:06 -05:00
|
|
|
# Fetch pipeline transformers.
|
|
|
|
#
|
|
|
|
# A transformer can be defined using:
|
|
|
|
# - `transformer` class method
|
|
|
|
# - `transform` instance method
|
|
|
|
#
|
|
|
|
# Multiple transformers can be defined within a single
|
|
|
|
# pipeline and run sequentially for each record in the
|
|
|
|
# following order:
|
|
|
|
# - Instance method `transform`
|
2021-09-13 05:11:26 -04:00
|
|
|
# - Transformers defined using `transformer` class method
|
2021-02-19 07:11:06 -05:00
|
|
|
#
|
|
|
|
# Instance method `transform` is always the last to run.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# class MyPipeline
|
|
|
|
# transformer MyTransformerOne, foo: :bar
|
|
|
|
# transformer MyTransformerTwo, foo: :bar
|
|
|
|
#
|
|
|
|
# def transform(context, data)
|
|
|
|
# # perform transformation here
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2021-02-22 13:10:55 -05:00
|
|
|
# In the example above `#transform` is the first to run and
|
|
|
|
# `MyTransformerTwo` method is the last.
|
2020-11-20 04:09:06 -05:00
|
|
|
def transformers
|
2021-02-22 13:10:55 -05:00
|
|
|
strong_memoize(:transformers) do
|
|
|
|
defined_transformers = self.class.transformers.map(&method(:instantiate))
|
|
|
|
|
|
|
|
transformers = []
|
|
|
|
transformers << self if respond_to?(:transform)
|
|
|
|
transformers.concat(defined_transformers)
|
|
|
|
transformers
|
|
|
|
end
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
2021-02-19 07:11:06 -05:00
|
|
|
# Fetch pipeline loader.
|
|
|
|
# A loader is defined either by instance method `#load(context, data)`
|
|
|
|
# or by using `loader` DSL.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# class MyPipeline
|
|
|
|
# loader MyLoader, foo: :bar
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# class MyPipeline
|
|
|
|
# def load(context, data)
|
|
|
|
# puts 'Load some data'
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# If pipeline implements instance method `load` - use it
|
|
|
|
# and ignore class `loader` method implementation.
|
2021-01-12 01:10:31 -05:00
|
|
|
def loader
|
2021-02-19 07:11:06 -05:00
|
|
|
@loader ||= self.respond_to?(:load) ? self : instantiate(self.class.get_loader)
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
2020-11-30 10:09:21 -05:00
|
|
|
def pipeline
|
2020-11-20 04:09:06 -05:00
|
|
|
@pipeline ||= self.class.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def instantiate(class_config)
|
2021-02-19 07:11:06 -05:00
|
|
|
options = class_config[:options]
|
|
|
|
|
|
|
|
if options
|
2021-05-27 14:10:52 -04:00
|
|
|
class_config[:klass].new(**class_config[:options])
|
2021-02-19 07:11:06 -05:00
|
|
|
else
|
|
|
|
class_config[:klass].new
|
|
|
|
end
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
2020-11-30 10:09:21 -05:00
|
|
|
|
|
|
|
def abort_on_failure?
|
|
|
|
self.class.abort_on_failure?
|
|
|
|
end
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def extractor(klass, options = nil)
|
2021-01-12 01:10:31 -05:00
|
|
|
class_attributes[:extractor] = { klass: klass, options: options }
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def transformer(klass, options = nil)
|
|
|
|
add_attribute(:transformers, klass, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def loader(klass, options = nil)
|
2021-01-12 01:10:31 -05:00
|
|
|
class_attributes[:loader] = { klass: klass, options: options }
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
2021-01-12 01:10:31 -05:00
|
|
|
def get_extractor
|
|
|
|
class_attributes[:extractor]
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def transformers
|
2021-02-22 13:10:55 -05:00
|
|
|
class_attributes[:transformers] || []
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
2021-01-12 01:10:31 -05:00
|
|
|
def get_loader
|
|
|
|
class_attributes[:loader]
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
2020-11-30 10:09:21 -05:00
|
|
|
def abort_on_failure!
|
|
|
|
class_attributes[:abort_on_failure] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def abort_on_failure?
|
|
|
|
class_attributes[:abort_on_failure]
|
|
|
|
end
|
|
|
|
|
2022-05-12 08:08:30 -04:00
|
|
|
def file_extraction_pipeline!
|
|
|
|
class_attributes[:file_extraction_pipeline] = true
|
2021-05-27 14:10:52 -04:00
|
|
|
end
|
|
|
|
|
2022-05-12 08:08:30 -04:00
|
|
|
def file_extraction_pipeline?
|
|
|
|
class_attributes[:file_extraction_pipeline]
|
2021-05-27 14:10:52 -04:00
|
|
|
end
|
|
|
|
|
2021-06-02 14:10:01 -04:00
|
|
|
def relation_name(name)
|
|
|
|
class_attributes[:relation_name] = name
|
|
|
|
end
|
|
|
|
|
|
|
|
def relation
|
|
|
|
class_attributes[:relation_name]
|
|
|
|
end
|
|
|
|
|
2020-11-20 04:09:06 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def add_attribute(sym, klass, options)
|
|
|
|
class_attributes[sym] ||= []
|
|
|
|
class_attributes[sym] << { klass: klass, options: options }
|
|
|
|
end
|
2020-10-23 11:08:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|