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

59 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
STATS_FILENAME = 'migration-stats.json'
2021-03-11 19:13:27 +05:30
attr_reader :observations
2021-11-18 22:05:49 +05:30
def initialize(result_dir:, observer_classes: ::Gitlab::Database::Migrations::Observers.all_observers)
2021-10-27 15:23:28 +05:30
@observer_classes = observer_classes
2021-03-11 19:13:27 +05:30
@observations = []
2021-11-18 22:05:49 +05:30
@result_dir = result_dir
2021-03-11 19:13:27 +05:30
end
2022-01-26 12:08:38 +05:30
def observe(version:, name:, connection:, &block)
2022-04-04 11:22:00 +05:30
observation = Observation.new(version: version, name: name, success: false)
2021-03-11 19:13:27 +05:30
2022-01-26 12:08:38 +05:30
observers = observer_classes.map { |c| c.new(observation, @result_dir, connection) }
2021-10-27 15:23:28 +05:30
on_each_observer(observers) { |observer| observer.before }
2021-03-11 19:13:27 +05:30
2022-04-04 11:22:00 +05:30
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
yield
observation.success = true
observation
ensure
observation.walltime = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
2021-03-11 19:13:27 +05:30
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)
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