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
|
|
|
|
2016-05-27 17:28:04 -04:00
|
|
|
def ensure_application_settings!
|
2017-01-21 19:11:19 -05:00
|
|
|
return fake_application_settings unless connect_to_db?
|
|
|
|
|
|
|
|
unless ENV['IN_MEMORY_APPLICATION_SETTINGS'] == 'true'
|
2016-06-26 02:29:28 -04:00
|
|
|
begin
|
2016-06-29 17:28:41 -04:00
|
|
|
settings = ::ApplicationSetting.current
|
2016-06-26 02:29:28 -04:00
|
|
|
# In case Redis isn't running or the Redis UNIX socket file is not available
|
|
|
|
rescue ::Redis::BaseError, ::Errno::ENOENT
|
|
|
|
settings = ::ApplicationSetting.last
|
|
|
|
end
|
2016-01-28 04:14:21 -05:00
|
|
|
|
2016-05-27 17:28:04 -04:00
|
|
|
settings ||= ::ApplicationSetting.create_from_defaults unless ActiveRecord::Migrator.needs_migration?
|
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-02-22 12:51:46 -05:00
|
|
|
delegate :sidekiq_throttling_enabled?, to: :current_application_settings
|
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-21 19:11:19 -05:00
|
|
|
# In case migrations the application_settings table is not created yet,
|
|
|
|
# we fallback to a simple OpenStruct
|
2017-01-23 10:58:13 -05:00
|
|
|
rescue ActiveRecord::StatementInvalid, ActiveRecord::UnknownAttributeError
|
2017-01-21 19:11:19 -05:00
|
|
|
fake_application_settings
|
|
|
|
end
|
|
|
|
|
2015-01-08 14:26:16 -05:00
|
|
|
def fake_application_settings
|
2017-01-26 06:46:02 -05:00
|
|
|
OpenStruct.new(::ApplicationSetting.defaults)
|
2015-01-08 14:26:16 -05:00
|
|
|
end
|
2015-09-01 06:45:14 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
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 &&
|
2016-09-23 04:50:46 -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
|