summaryrefslogtreecommitdiff
path: root/app/workers/bulk_import_worker.rb
blob: e6bc54895a7fef59e13392016b04562e736c0a8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true

class BulkImportWorker # rubocop:disable Scalability/IdempotentWorker
  include ApplicationWorker

  feature_category :importers

  sidekiq_options retry: false, dead: false

  PERFORM_DELAY = 5.seconds
  DEFAULT_BATCH_SIZE = 5

  def perform(bulk_import_id)
    @bulk_import = BulkImport.find_by_id(bulk_import_id)

    return unless @bulk_import
    return if @bulk_import.finished?
    return @bulk_import.finish! if all_entities_processed? && @bulk_import.started?
    return re_enqueue if max_batch_size_exceeded? # Do not start more jobs if max allowed are already running

    @bulk_import.start! if @bulk_import.created?

    created_entities.first(next_batch_size).each do |entity|
      entity.start!

      BulkImports::EntityWorker.perform_async(entity.id)
    end

    re_enqueue
  rescue => e
    Gitlab::ErrorTracking.track_exception(e, bulk_import_id: @bulk_import&.id)

    @bulk_import&.fail_op
  end

  private

  def entities
    @entities ||= @bulk_import.entities
  end

  def started_entities
    entities.with_status(:started)
  end

  def created_entities
    entities.with_status(:created)
  end

  def all_entities_processed?
    entities.all? { |entity| entity.finished? || entity.failed? }
  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

  # 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)
  end
end