summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/migrations/base_background_runner.rb
blob: dbb85bad95cf5c2ce2e7154d221fd9d7483643e4 (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
# frozen_string_literal: true

module Gitlab
  module Database
    module Migrations
      class BaseBackgroundRunner
        attr_reader :result_dir, :connection

        def initialize(result_dir:, connection:)
          @result_dir = result_dir
          @connection = connection
        end

        def jobs_by_migration_name
          raise NotImplementedError, 'subclass must implement'
        end

        def run_job(job)
          raise NotImplementedError, 'subclass must implement'
        end

        def run_jobs(for_duration:)
          jobs_to_run = jobs_by_migration_name
          return if jobs_to_run.empty?

          # without .to_f, we do integer division
          # For example, 3.minutes / 2 == 1.minute whereas 3.minutes / 2.to_f == (1.minute + 30.seconds)
          duration_per_migration_type = for_duration / jobs_to_run.count.to_f
          jobs_to_run.each do |migration_name, jobs|
            run_until = duration_per_migration_type.from_now

            run_jobs_for_migration(migration_name: migration_name, jobs: jobs, run_until: run_until)
          end
        end

        private

        def run_jobs_for_migration(migration_name:, jobs:, run_until:)
          per_background_migration_result_dir = File.join(@result_dir, migration_name)

          instrumentation = Instrumentation.new(result_dir: per_background_migration_result_dir)
          batch_names = (1..).each.lazy.map { |i| "batch_#{i}" }

          jobs.each do |j|
            break if run_until <= Time.current

            instrumentation.observe(version: nil,
                                    name: batch_names.next,
                                    connection: connection) do
              run_job(j)
            end
          end
        end
      end
    end
  end
end