2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-01-08 12:53:35 -05:00
|
|
|
module Gitlab
|
|
|
|
module CurrentSettings
|
2018-02-02 13:39:55 -05:00
|
|
|
class << self
|
|
|
|
def current_application_settings
|
2018-09-20 15:32:54 -04:00
|
|
|
Gitlab::SafeRequestStore.fetch(:current_application_settings) { ensure_application_settings! }
|
2018-02-02 13:39:55 -05:00
|
|
|
end
|
2017-08-31 05:47:03 -04:00
|
|
|
|
2018-09-10 05:48:09 -04:00
|
|
|
def clear_in_memory_application_settings!
|
2018-09-10 18:04:27 -04:00
|
|
|
@in_memory_application_settings = nil
|
2018-09-10 05:48:09 -04:00
|
|
|
end
|
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
def method_missing(name, *args, &block)
|
|
|
|
current_application_settings.send(name, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
2017-01-21 19:11:19 -05:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
def respond_to_missing?(name, include_private = false)
|
|
|
|
current_application_settings.respond_to?(name, include_private) || super
|
|
|
|
end
|
2016-01-28 04:14:21 -05:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
private
|
2017-06-06 12:48:10 -04:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
def ensure_application_settings!
|
2018-06-07 09:14:27 -04:00
|
|
|
cached_application_settings || uncached_application_settings
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_application_settings
|
2018-02-02 13:39:55 -05:00
|
|
|
return in_memory_application_settings if ENV['IN_MEMORY_APPLICATION_SETTINGS'] == 'true'
|
2018-06-07 09:14:27 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
::ApplicationSetting.cached
|
2018-06-07 11:23:38 -04:00
|
|
|
rescue
|
|
|
|
# In case Redis isn't running
|
|
|
|
# or the Redis UNIX socket file is not available
|
|
|
|
# or the DB is not running (we use migrations in the cache key)
|
2018-06-07 09:14:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def uncached_application_settings
|
2018-02-02 13:39:55 -05:00
|
|
|
return fake_application_settings unless connect_to_db?
|
2017-06-17 10:35:30 -04:00
|
|
|
|
2018-05-16 11:10:48 -04:00
|
|
|
current_settings = ::ApplicationSetting.current
|
2018-02-02 13:39:55 -05:00
|
|
|
# If there are pending migrations, it's possible there are columns that
|
|
|
|
# need to be added to the application settings. To prevent Rake tasks
|
|
|
|
# and other callers from failing, use any loaded settings and return
|
|
|
|
# defaults for missing columns.
|
|
|
|
if ActiveRecord::Migrator.needs_migration?
|
2018-12-12 06:59:06 -05:00
|
|
|
db_attributes = current_settings&.attributes || {}
|
2019-03-26 10:33:56 -04:00
|
|
|
fake_application_settings(db_attributes)
|
2018-12-14 04:14:21 -05:00
|
|
|
elsif current_settings.present?
|
|
|
|
current_settings
|
|
|
|
else
|
2018-12-14 09:45:14 -05:00
|
|
|
::ApplicationSetting.create_from_defaults
|
2018-05-16 11:10:48 -04:00
|
|
|
end
|
2018-02-02 13:39:55 -05:00
|
|
|
end
|
2016-11-04 18:54:24 -04:00
|
|
|
|
2018-12-14 04:14:21 -05:00
|
|
|
def fake_application_settings(attributes = {})
|
|
|
|
Gitlab::FakeApplicationSettings.new(::ApplicationSetting.defaults.merge(attributes || {}))
|
2018-05-16 11:10:48 -04:00
|
|
|
end
|
|
|
|
|
2018-12-14 04:14:21 -05:00
|
|
|
def in_memory_application_settings
|
|
|
|
@in_memory_application_settings ||= ::ApplicationSetting.build_from_defaults
|
2018-02-02 13:39:55 -05:00
|
|
|
end
|
2017-01-21 19:11:19 -05:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
def connect_to_db?
|
|
|
|
# When the DBMS is not available, an exception (e.g. PG::ConnectionBad) is raised
|
|
|
|
active_db_connection = ActiveRecord::Base.connection.active? rescue false
|
2016-01-28 06:23:37 -05:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
active_db_connection &&
|
2018-03-15 16:12:00 -04:00
|
|
|
Gitlab::Database.cached_table_exists?('application_settings')
|
2018-02-02 13:39:55 -05:00
|
|
|
rescue ActiveRecord::NoDatabaseError
|
|
|
|
false
|
|
|
|
end
|
2015-09-01 06:45:14 -04:00
|
|
|
end
|
2015-01-08 12:53:35 -05:00
|
|
|
end
|
|
|
|
end
|