2015-01-08 12:53:35 -05:00
|
|
|
module Gitlab
|
|
|
|
module CurrentSettings
|
2017-08-31 05:47:03 -04:00
|
|
|
extend self
|
|
|
|
|
2015-01-08 12:53:35 -05:00
|
|
|
def current_application_settings
|
2016-05-27 17:28:04 -04:00
|
|
|
if RequestStore.active?
|
|
|
|
RequestStore.fetch(:current_application_settings) { ensure_application_settings! }
|
|
|
|
else
|
|
|
|
ensure_application_settings!
|
|
|
|
end
|
|
|
|
end
|
2016-01-28 04:14:21 -05:00
|
|
|
|
2017-06-06 12:48:10 -04:00
|
|
|
delegate :sidekiq_throttling_enabled?, to: :current_application_settings
|
2017-01-21 19:11:19 -05:00
|
|
|
|
2017-06-21 02:26:02 -04:00
|
|
|
def fake_application_settings(defaults = ::ApplicationSetting.defaults)
|
2017-06-17 10:35:30 -04:00
|
|
|
FakeApplicationSettings.new(defaults)
|
2017-06-06 12:48:10 -04:00
|
|
|
end
|
2016-01-28 04:14:21 -05:00
|
|
|
|
2017-06-06 12:48:10 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def ensure_application_settings!
|
2017-06-17 10:35:30 -04:00
|
|
|
return in_memory_application_settings if ENV['IN_MEMORY_APPLICATION_SETTINGS'] == 'true'
|
2016-05-27 17:28:04 -04:00
|
|
|
|
2017-06-17 10:35:30 -04:00
|
|
|
cached_application_settings || uncached_application_settings
|
2015-01-08 12:53:35 -05:00
|
|
|
end
|
2015-01-08 14:26:16 -05:00
|
|
|
|
2017-06-17 10:35:30 -04:00
|
|
|
def cached_application_settings
|
2017-06-06 12:48:10 -04:00
|
|
|
begin
|
2017-06-21 02:26:02 -04:00
|
|
|
::ApplicationSetting.cached
|
2017-07-11 02:23:51 -04:00
|
|
|
rescue ::Redis::BaseError, ::Errno::ENOENT, ::Errno::EADDRNOTAVAIL
|
2017-06-17 10:35:30 -04:00
|
|
|
# In case Redis isn't running or the Redis UNIX socket file is not available
|
2017-06-06 12:48:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-17 10:35:30 -04:00
|
|
|
def uncached_application_settings
|
|
|
|
return fake_application_settings unless connect_to_db?
|
|
|
|
|
2017-07-10 08:29:16 -04:00
|
|
|
db_settings = ::ApplicationSetting.current
|
2017-06-17 10:35:30 -04: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?
|
2017-06-21 02:26:02 -04:00
|
|
|
defaults = ::ApplicationSetting.defaults
|
2017-06-17 10:35:30 -04:00
|
|
|
defaults.merge!(db_settings.attributes.symbolize_keys) if db_settings.present?
|
|
|
|
return fake_application_settings(defaults)
|
|
|
|
end
|
|
|
|
|
|
|
|
return db_settings if db_settings.present?
|
|
|
|
|
2017-06-21 02:26:02 -04:00
|
|
|
::ApplicationSetting.create_from_defaults || in_memory_application_settings
|
2017-06-06 12:48:10 -04:00
|
|
|
end
|
2016-11-04 18:54:24 -04:00
|
|
|
|
2017-01-21 19:11:19 -05:00
|
|
|
def in_memory_application_settings
|
2017-01-26 06:46:02 -05:00
|
|
|
@in_memory_application_settings ||= ::ApplicationSetting.new(::ApplicationSetting.defaults)
|
2017-01-23 10:58:13 -05:00
|
|
|
rescue ActiveRecord::StatementInvalid, ActiveRecord::UnknownAttributeError
|
2017-06-06 12:48:10 -04:00
|
|
|
# In case migrations the application_settings table is not created yet,
|
|
|
|
# we fallback to a simple OpenStruct
|
2017-01-21 19:11:19 -05:00
|
|
|
fake_application_settings
|
|
|
|
end
|
|
|
|
|
2015-09-01 06:45:14 -04:00
|
|
|
def connect_to_db?
|
2016-01-28 06:23:37 -05:00
|
|
|
# When the DBMS is not available, an exception (e.g. PG::ConnectionBad) is raised
|
|
|
|
active_db_connection = ActiveRecord::Base.connection.active? rescue false
|
|
|
|
|
|
|
|
active_db_connection &&
|
2017-06-17 10:35:30 -04:00
|
|
|
ActiveRecord::Base.connection.table_exists?('application_settings')
|
2016-01-08 08:31:39 -05:00
|
|
|
rescue ActiveRecord::NoDatabaseError
|
|
|
|
false
|
2015-09-01 06:45:14 -04:00
|
|
|
end
|
2015-01-08 12:53:35 -05:00
|
|
|
end
|
|
|
|
end
|