Class: Gitlab::BackgroundOperation::BaseOperationWorker

Inherits:
Object
  • Object
show all
Includes:
ClassAttributes, Database::DynamicModelHelpers
Defined in:
lib/gitlab/background_operation/base_operation_worker.rb

Overview

rubocop:disable Metrics/ParameterLists – Need a lot of args rubocop:disable CodeReuse/ActiveRecord – Need to manipulate active record object

Constant Summary collapse

DEFAULT_FEATURE_CATEGORY =
:database
MINIMUM_PAUSE_MS =
100

Constants included from Database::DynamicModelHelpers

Database::DynamicModelHelpers::BATCH_SIZE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Database::DynamicModelHelpers

define_batchable_model, #each_batch, #each_batch_range

Constructor Details

#initialize(batch_table:, batch_column:, sub_batch_size:, pause_ms:, connection:, job_arguments: [], min_cursor: nil, max_cursor: nil, sub_batch_exception: nil) ⇒ BaseOperationWorker

Returns a new instance of BaseOperationWorker.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gitlab/background_operation/base_operation_worker.rb', line 55

def initialize(
  batch_table:, batch_column:, sub_batch_size:, pause_ms:, connection:, job_arguments: [],
  min_cursor: nil, max_cursor: nil, sub_batch_exception: nil
)
  @min_cursor = min_cursor
  @max_cursor = max_cursor
  @batch_table = batch_table
  @batch_column = batch_column
  @sub_batch_size = sub_batch_size
  @pause_ms = [pause_ms, MINIMUM_PAUSE_MS].max
  @job_arguments = job_arguments
  @connection = connection
  @sub_batch_exception = sub_batch_exception
end

Class Method Details

.cursor(*args) ⇒ Object



48
49
50
51
52
# File 'lib/gitlab/background_operation/base_operation_worker.rb', line 48

def cursor(*args)
  define_singleton_method(:cursor_columns) do
    args
  end
end

.cursor?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/gitlab/background_operation/base_operation_worker.rb', line 44

def cursor?
  cursor_columns.count >= 1
end

.cursor_columnsObject



40
41
42
# File 'lib/gitlab/background_operation/base_operation_worker.rb', line 40

def cursor_columns
  []
end

.feature_category(feature_category_name = nil) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/gitlab/background_operation/base_operation_worker.rb', line 32

def feature_category(feature_category_name = nil)
  if feature_category_name.present?
    set_class_attribute(:feature_category, feature_category_name)
  else
    get_class_attribute(:feature_category) || DEFAULT_FEATURE_CATEGORY
  end
end

.job_arguments(*args) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/gitlab/background_operation/base_operation_worker.rb', line 24

def job_arguments(*args)
  args.each_with_index do |arg, index|
    define_method(arg) do
      @job_arguments[index]
    end
  end
end

.operation_name(operation) ⇒ Object



18
19
20
21
22
# File 'lib/gitlab/background_operation/base_operation_worker.rb', line 18

def operation_name(operation)
  define_method(:operation_name) do
    operation
  end
end

Instance Method Details

#batch_metricsObject



74
75
76
# File 'lib/gitlab/background_operation/base_operation_worker.rb', line 74

def batch_metrics
  @batch_metrics ||= Gitlab::Database::Batch::Metrics.new
end

#performObject

Raises:

  • (NotImplementedError)


70
71
72
# File 'lib/gitlab/background_operation/base_operation_worker.rb', line 70

def perform
  raise NotImplementedError, "subclasses of #{self.class.name} must implement #{__method__}"
end