2015-04-26 12:48:37 +05:30
|
|
|
module Gitlab
|
|
|
|
module CurrentSettings
|
2018-03-17 18:26:18 +05:30
|
|
|
class << self
|
|
|
|
def current_application_settings
|
|
|
|
if RequestStore.active?
|
|
|
|
RequestStore.fetch(:current_application_settings) { ensure_application_settings! }
|
|
|
|
else
|
|
|
|
ensure_application_settings!
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def fake_application_settings(defaults = ::ApplicationSetting.defaults)
|
|
|
|
Gitlab::FakeApplicationSettings.new(defaults)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def method_missing(name, *args, &block)
|
|
|
|
current_application_settings.send(name, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def respond_to_missing?(name, include_private = false)
|
|
|
|
current_application_settings.respond_to?(name, include_private) || super
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
private
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def ensure_application_settings!
|
|
|
|
return in_memory_application_settings if ENV['IN_MEMORY_APPLICATION_SETTINGS'] == 'true'
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
cached_application_settings || uncached_application_settings
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
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
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def uncached_application_settings
|
|
|
|
return fake_application_settings unless connect_to_db?
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
db_settings = ::ApplicationSetting.current
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# 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-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
return db_settings if db_settings.present?
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
::ApplicationSetting.create_from_defaults || in_memory_application_settings
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
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-04-02 18:10:28 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
active_db_connection &&
|
2018-03-27 19:54:05 +05:30
|
|
|
Gitlab::Database.cached_table_exists?('application_settings')
|
2018-03-17 18:26:18 +05:30
|
|
|
rescue ActiveRecord::NoDatabaseError
|
|
|
|
false
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|