2015-04-26 12:48:37 +05:30
|
|
|
module Gitlab
|
|
|
|
module CurrentSettings
|
|
|
|
def current_application_settings
|
2016-06-16 23:09:34 +05:30
|
|
|
if RequestStore.active?
|
|
|
|
RequestStore.fetch(:current_application_settings) { ensure_application_settings! }
|
|
|
|
else
|
|
|
|
ensure_application_settings!
|
|
|
|
end
|
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def ensure_application_settings!
|
2017-08-17 22:00:37 +05:30
|
|
|
return fake_application_settings unless connect_to_db?
|
|
|
|
|
|
|
|
unless ENV['IN_MEMORY_APPLICATION_SETTINGS'] == 'true'
|
2016-08-24 12:49:21 +05:30
|
|
|
begin
|
|
|
|
settings = ::ApplicationSetting.current
|
|
|
|
# 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-04-02 18:10:28 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
settings ||= ::ApplicationSetting.create_from_defaults unless ActiveRecord::Migrator.needs_migration?
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
settings || in_memory_application_settings
|
|
|
|
end
|
|
|
|
|
|
|
|
delegate :sidekiq_throttling_enabled?, to: :current_application_settings
|
|
|
|
|
|
|
|
def in_memory_application_settings
|
|
|
|
@in_memory_application_settings ||= ::ApplicationSetting.new(::ApplicationSetting.defaults)
|
|
|
|
# In case migrations the application_settings table is not created yet,
|
|
|
|
# we fallback to a simple OpenStruct
|
|
|
|
rescue ActiveRecord::StatementInvalid, ActiveRecord::UnknownAttributeError
|
|
|
|
fake_application_settings
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def fake_application_settings
|
2017-08-17 22:00:37 +05:30
|
|
|
OpenStruct.new(::ApplicationSetting.defaults)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def connect_to_db?
|
2016-04-02 18:10:28 +05:30
|
|
|
# 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-11-03 12:29:30 +05:30
|
|
|
ActiveRecord::Base.connection.table_exists?('application_settings')
|
2016-01-14 18:37:52 +05:30
|
|
|
rescue ActiveRecord::NoDatabaseError
|
|
|
|
false
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|