2020-10-23 11:08:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BulkImportWorker # rubocop:disable Scalability/IdempotentWorker
|
|
|
|
include ApplicationWorker
|
|
|
|
|
2022-04-06 23:08:34 -04:00
|
|
|
PERFORM_DELAY = 5.seconds
|
2021-07-21 08:09:35 -04:00
|
|
|
|
2022-04-06 23:08:34 -04:00
|
|
|
data_consistency :always
|
2020-10-23 11:08:42 -04:00
|
|
|
feature_category :importers
|
|
|
|
sidekiq_options retry: false, dead: false
|
|
|
|
|
|
|
|
def perform(bulk_import_id)
|
2021-01-12 07:10:49 -05:00
|
|
|
@bulk_import = BulkImport.find_by_id(bulk_import_id)
|
|
|
|
|
|
|
|
return unless @bulk_import
|
2021-06-15 11:10:04 -04:00
|
|
|
return if @bulk_import.finished? || @bulk_import.failed?
|
|
|
|
return @bulk_import.fail_op! if all_entities_failed?
|
2021-01-12 07:10:49 -05:00
|
|
|
return @bulk_import.finish! if all_entities_processed? && @bulk_import.started?
|
|
|
|
|
|
|
|
@bulk_import.start! if @bulk_import.created?
|
|
|
|
|
2022-04-06 23:08:34 -04:00
|
|
|
created_entities.find_each do |entity|
|
2022-05-26 23:08:03 -04:00
|
|
|
BulkImports::CreatePipelineTrackersService.new(entity).execute!
|
2021-01-12 07:10:49 -05:00
|
|
|
|
2021-04-13 05:11:10 -04:00
|
|
|
entity.start!
|
2022-09-28 05:09:28 -04:00
|
|
|
|
|
|
|
BulkImports::ExportRequestWorker.perform_async(entity.id)
|
2021-01-12 07:10:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
re_enqueue
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2021-01-21 10:08:36 -05:00
|
|
|
Gitlab::ErrorTracking.track_exception(e, bulk_import_id: @bulk_import&.id)
|
|
|
|
|
|
|
|
@bulk_import&.fail_op
|
2021-01-12 07:10:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def entities
|
|
|
|
@entities ||= @bulk_import.entities
|
|
|
|
end
|
|
|
|
|
|
|
|
def created_entities
|
|
|
|
entities.with_status(:created)
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_entities_processed?
|
|
|
|
entities.all? { |entity| entity.finished? || entity.failed? }
|
|
|
|
end
|
|
|
|
|
2021-06-15 11:10:04 -04:00
|
|
|
def all_entities_failed?
|
|
|
|
entities.all? { |entity| entity.failed? }
|
|
|
|
end
|
|
|
|
|
2021-01-12 07:10:49 -05:00
|
|
|
# A new BulkImportWorker job is enqueued to either
|
|
|
|
# - Process the new BulkImports::Entity created during import (e.g. for the subgroups)
|
|
|
|
# - Or to mark the `bulk_import` as finished
|
|
|
|
def re_enqueue
|
|
|
|
BulkImportWorker.perform_in(PERFORM_DELAY, @bulk_import.id)
|
2020-10-23 11:08:42 -04:00
|
|
|
end
|
|
|
|
end
|