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
|
|
|
|
if RequestStore.active?
|
|
|
|
RequestStore.fetch(:current_application_settings) { ensure_application_settings! }
|
|
|
|
else
|
|
|
|
ensure_application_settings!
|
|
|
|
end
|
|
|
|
end
|
2017-08-31 05:47:03 -04:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
def fake_application_settings(defaults = ::ApplicationSetting.defaults)
|
|
|
|
Gitlab::FakeApplicationSettings.new(defaults)
|
2016-05-27 17:28:04 -04:00
|
|
|
end
|
2016-01-28 04:14:21 -05:00
|
|
|
|
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!
|
|
|
|
return in_memory_application_settings if ENV['IN_MEMORY_APPLICATION_SETTINGS'] == 'true'
|
2016-05-27 17:28:04 -04:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
cached_application_settings || uncached_application_settings
|
|
|
|
end
|
2015-01-08 14:26:16 -05:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
def cached_application_settings
|
|
|
|
begin
|
|
|
|
::ApplicationSetting.cached
|
|
|
|
rescue ::Redis::BaseError, ::Errno::ENOENT, ::Errno::EADDRNOTAVAIL
|
|
|
|
# In case Redis isn't running or the Redis UNIX socket file is not available
|
|
|
|
end
|
2017-06-06 12:48:10 -04:00
|
|
|
end
|
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
def uncached_application_settings
|
|
|
|
return fake_application_settings unless connect_to_db?
|
2017-06-17 10:35:30 -04:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
db_settings = ::ApplicationSetting.current
|
2017-06-17 10:35:30 -04:00
|
|
|
|
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?
|
|
|
|
defaults = ::ApplicationSetting.defaults
|
|
|
|
defaults.merge!(db_settings.attributes.symbolize_keys) if db_settings.present?
|
|
|
|
return fake_application_settings(defaults)
|
|
|
|
end
|
2017-06-17 10:35:30 -04:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
return db_settings if db_settings.present?
|
2017-06-17 10:35:30 -04:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
::ApplicationSetting.create_from_defaults || in_memory_application_settings
|
|
|
|
end
|
2016-11-04 18:54:24 -04:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
def in_memory_application_settings
|
|
|
|
@in_memory_application_settings ||= ::ApplicationSetting.new(::ApplicationSetting.defaults) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
rescue ActiveRecord::StatementInvalid, ActiveRecord::UnknownAttributeError
|
|
|
|
# In case migrations the application_settings table is not created yet,
|
|
|
|
# we fallback to a simple OpenStruct
|
|
|
|
fake_application_settings
|
|
|
|
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
|