2015-01-08 12:53:35 -05:00
|
|
|
module Gitlab
|
|
|
|
module CurrentSettings
|
|
|
|
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-06 12:48:10 -04:00
|
|
|
def fake_application_settings
|
|
|
|
OpenStruct.new(::ApplicationSetting.defaults)
|
|
|
|
end
|
2016-01-28 04:14:21 -05:00
|
|
|
|
2017-06-06 12:48:10 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def ensure_application_settings!
|
|
|
|
unless ENV['IN_MEMORY_APPLICATION_SETTINGS'] == 'true'
|
|
|
|
settings = retrieve_settings_from_database?
|
2015-01-08 13:30:35 -05:00
|
|
|
end
|
2016-05-27 17:28:04 -04:00
|
|
|
|
2017-01-21 19:11:19 -05:00
|
|
|
settings || in_memory_application_settings
|
2015-01-08 12:53:35 -05:00
|
|
|
end
|
2015-01-08 14:26:16 -05:00
|
|
|
|
2017-06-06 12:48:10 -04:00
|
|
|
def retrieve_settings_from_database?
|
|
|
|
settings = retrieve_settings_from_database_cache?
|
|
|
|
return settings if settings.present?
|
|
|
|
|
|
|
|
return fake_application_settings unless connect_to_db?
|
|
|
|
|
|
|
|
begin
|
|
|
|
db_settings = ::ApplicationSetting.current
|
|
|
|
# In case Redis isn't running or the Redis UNIX socket file is not available
|
|
|
|
rescue ::Redis::BaseError, ::Errno::ENOENT
|
|
|
|
db_settings = ::ApplicationSetting.last
|
|
|
|
end
|
|
|
|
db_settings || ::ApplicationSetting.create_from_defaults
|
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve_settings_from_database_cache?
|
|
|
|
begin
|
|
|
|
settings = ApplicationSetting.cached
|
|
|
|
rescue ::Redis::BaseError, ::Errno::ENOENT
|
|
|
|
# In case Redis isn't running or the Redis UNIX socket file is not available
|
|
|
|
settings = nil
|
|
|
|
end
|
|
|
|
settings
|
|
|
|
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-16 16:13:09 -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
|