debian-mirror-gitlab/app/workers/bulk_import_worker.rb

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

76 lines
1.9 KiB
Ruby
Raw Permalink Normal View History

2021-01-29 00:20:46 +05:30
# frozen_string_literal: true
class BulkImportWorker # rubocop:disable Scalability/IdempotentWorker
include ApplicationWorker
2022-06-21 17:19:12 +05:30
PERFORM_DELAY = 5.seconds
2023-04-23 21:23:45 +05:30
DEFAULT_BATCH_SIZE = 5
2021-10-27 15:23:28 +05:30
2022-06-21 17:19:12 +05:30
data_consistency :always
2021-01-29 00:20:46 +05:30
feature_category :importers
sidekiq_options retry: false, dead: false
def perform(bulk_import_id)
2021-03-08 18:12:59 +05:30
@bulk_import = BulkImport.find_by_id(bulk_import_id)
return unless @bulk_import
2021-09-04 01:27:46 +05:30
return if @bulk_import.finished? || @bulk_import.failed?
return @bulk_import.fail_op! if all_entities_failed?
2021-03-08 18:12:59 +05:30
return @bulk_import.finish! if all_entities_processed? && @bulk_import.started?
2023-04-23 21:23:45 +05:30
return re_enqueue if max_batch_size_exceeded? # Do not start more jobs if max allowed are already running
2021-03-08 18:12:59 +05:30
@bulk_import.start! if @bulk_import.created?
2023-04-23 21:23:45 +05:30
created_entities.first(next_batch_size).each do |entity|
2022-07-23 23:45:48 +05:30
BulkImports::CreatePipelineTrackersService.new(entity).execute!
2021-03-08 18:12:59 +05:30
2021-04-29 21:17:54 +05:30
entity.start!
2022-11-25 23:54:43 +05:30
BulkImports::ExportRequestWorker.perform_async(entity.id)
2021-03-08 18:12:59 +05:30
end
re_enqueue
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2021-03-11 19:13:27 +05:30
Gitlab::ErrorTracking.track_exception(e, bulk_import_id: @bulk_import&.id)
@bulk_import&.fail_op
2021-03-08 18:12:59 +05:30
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-09-04 01:27:46 +05:30
def all_entities_failed?
entities.all? { |entity| entity.failed? }
end
2021-03-08 18:12:59 +05:30
# 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)
2021-01-29 00:20:46 +05:30
end
2023-04-23 21:23:45 +05:30
def started_entities
entities.with_status(:started)
end
def max_batch_size_exceeded?
started_entities.count >= DEFAULT_BATCH_SIZE
end
def next_batch_size
[DEFAULT_BATCH_SIZE - started_entities.count, 0].max
end
2021-01-29 00:20:46 +05:30
end