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

63 lines
1.6 KiB
Ruby
Raw Normal View History

2021-03-11 19:13:27 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module Migrations
class Instrumentation
2021-06-08 01:23:25 +05:30
RESULT_DIR = Rails.root.join('tmp', 'migration-testing').freeze
STATS_FILENAME = 'migration-stats.json'
2021-03-11 19:13:27 +05:30
attr_reader :observations
2021-10-27 15:23:28 +05:30
def initialize(observer_classes = ::Gitlab::Database::Migrations::Observers.all_observers)
@observer_classes = observer_classes
2021-03-11 19:13:27 +05:30
@observations = []
end
2021-10-27 15:23:28 +05:30
def observe(version:, name:, &block)
observation = Observation.new(version, name)
2021-03-11 19:13:27 +05:30
observation.success = true
2021-10-27 15:23:28 +05:30
observers = observer_classes.map { |c| c.new(observation) }
2021-03-11 19:13:27 +05:30
exception = nil
2021-10-27 15:23:28 +05:30
on_each_observer(observers) { |observer| observer.before }
2021-03-11 19:13:27 +05:30
observation.walltime = Benchmark.realtime do
yield
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2021-03-11 19:13:27 +05:30
exception = e
observation.success = false
end
2021-10-27 15:23:28 +05:30
on_each_observer(observers) { |observer| observer.after }
on_each_observer(observers) { |observer| observer.record }
2021-03-11 19:13:27 +05:30
record_observation(observation)
raise exception if exception
observation
end
private
2021-10-27 15:23:28 +05:30
attr_reader :observer_classes
2021-03-11 19:13:27 +05:30
def record_observation(observation)
@observations << observation
end
2021-10-27 15:23:28 +05:30
def on_each_observer(observers, &block)
2021-03-11 19:13:27 +05:30
observers.each do |observer|
yield observer
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2021-03-11 19:13:27 +05:30
Gitlab::AppLogger.error("Migration observer #{observer.class} failed with: #{e}")
end
end
end
end
end
end