debian-mirror-gitlab/lib/gitlab/database/migrations/base_background_runner.rb

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

71 lines
2.1 KiB
Ruby
Raw Normal View History

2022-07-16 23:28:13 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module Migrations
class BaseBackgroundRunner
2022-11-25 23:54:43 +05:30
attr_reader :result_dir, :connection
2022-07-16 23:28:13 +05:30
2022-11-25 23:54:43 +05:30
def initialize(result_dir:, connection:)
2022-07-16 23:28:13 +05:30
@result_dir = result_dir
2022-11-25 23:54:43 +05:30
@connection = connection
2022-07-16 23:28:13 +05:30
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)
2023-04-23 21:23:45 +05:30
instrumentation = Instrumentation.new(result_dir: per_background_migration_result_dir,
observer_classes: observers)
2022-08-27 11:52:29 +05:30
batch_names = (1..).each.lazy.map { |i| "batch_#{i}" }
2022-07-16 23:28:13 +05:30
2022-10-11 01:57:18 +05:30
jobs.each do |j|
2022-07-16 23:28:13 +05:30
break if run_until <= Time.current
2023-04-23 21:23:45 +05:30
meta = { job_meta: job_meta(j) }
2023-03-17 16:20:25 +05:30
2022-07-16 23:28:13 +05:30
instrumentation.observe(version: nil,
name: batch_names.next,
2023-03-17 16:20:25 +05:30
connection: connection,
meta: meta) do
2022-07-16 23:28:13 +05:30
run_job(j)
end
end
end
2023-03-17 16:20:25 +05:30
2023-04-23 21:23:45 +05:30
def job_meta(_job)
2023-03-17 16:20:25 +05:30
{}
end
2023-04-23 21:23:45 +05:30
def observers
::Gitlab::Database::Migrations::Observers.all_observers
end
2022-07-16 23:28:13 +05:30
end
end
end
end