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-05-16 11:10:48 -04:00
|
|
|
def fake_application_settings(attributes = {})
|
|
|
|
Gitlab::FakeApplicationSettings.new(::ApplicationSetting.defaults.merge(attributes || {}))
|
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!
|
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-05-16 11:10:48 -04:00
|
|
|
return fake_application_settings(current_settings&.attributes)
|
2018-02-02 13:39:55 -05:00
|
|
|
end
|
2017-06-17 10:35:30 -04:00
|
|
|
|
2018-05-16 11:10:48 -04:00
|
|
|
return current_settings if current_settings.present?
|
2017-06-17 10:35:30 -04:00
|
|
|
|
2018-05-16 11:10:48 -04:00
|
|
|
with_fallback_to_fake_application_settings do
|
|
|
|
::ApplicationSetting.create_from_defaults || in_memory_application_settings
|
|
|
|
end
|
2018-02-02 13:39:55 -05:00
|
|
|
end
|
2016-11-04 18:54:24 -04:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
def in_memory_application_settings
|
2018-05-16 11:10:48 -04:00
|
|
|
with_fallback_to_fake_application_settings do
|
2018-07-02 06:43:06 -04:00
|
|
|
@in_memory_application_settings ||= ::ApplicationSetting.build_from_defaults
|
2018-05-16 11:10:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_fallback_to_fake_application_settings(&block)
|
|
|
|
yield
|
|
|
|
rescue
|
|
|
|
# In case the application_settings table is not created yet, or if a new
|
|
|
|
# ApplicationSetting column is not yet migrated we fallback to a simple OpenStruct
|
2018-02-02 13:39:55 -05:00
|
|
|
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
|