Welcome to mirror list, hosted at ThFree Co, Russian Federation.

scheduler.rb « background_migration « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5f8a5ec06a58ccd761c0a0b030e9effaae4e6fcd (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
# frozen_string_literal: true

module Gitlab
  module Database
    module BackgroundMigration
      class Scheduler
        def perform(migration_wrapper: BatchedMigrationWrapper.new)
          active_migration = BatchedMigration.active.queue_order.first

          return unless active_migration&.interval_elapsed?

          if next_batched_job = create_next_batched_job!(active_migration)
            migration_wrapper.perform(next_batched_job)
          else
            finish_active_migration(active_migration)
          end
        end

        private

        def create_next_batched_job!(active_migration)
          next_batch_range = find_next_batch_range(active_migration)

          return if next_batch_range.nil?

          active_migration.create_batched_job!(next_batch_range.min, next_batch_range.max)
        end

        def find_next_batch_range(active_migration)
          batching_strategy = active_migration.batch_class.new
          batch_min_value = active_migration.next_min_value

          next_batch_bounds = batching_strategy.next_batch(
            active_migration.table_name,
            active_migration.column_name,
            batch_min_value: batch_min_value,
            batch_size: active_migration.batch_size)

          return if next_batch_bounds.nil?

          clamped_batch_range(active_migration, next_batch_bounds)
        end

        def clamped_batch_range(active_migration, next_bounds)
          min_value, max_value = next_bounds

          return if min_value > active_migration.max_value

          max_value = max_value.clamp(min_value, active_migration.max_value)

          (min_value..max_value)
        end

        def finish_active_migration(active_migration)
          active_migration.finished!
        end
      end
    end
  end
end