2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-01-08 17:53:35 +00:00
|
|
|
module Gitlab
|
|
|
|
module CurrentSettings
|
2018-02-02 18:39:55 +00:00
|
|
|
class << self
|
|
|
|
def current_application_settings
|
2018-09-20 19:32:54 +00:00
|
|
|
Gitlab::SafeRequestStore.fetch(:current_application_settings) { ensure_application_settings! }
|
2018-02-02 18:39:55 +00:00
|
|
|
end
|
2017-08-31 09:47:03 +00:00
|
|
|
|
2019-08-07 18:40:36 +00:00
|
|
|
def expire_current_application_settings
|
|
|
|
::ApplicationSetting.expire
|
|
|
|
Gitlab::SafeRequestStore.delete(:current_application_settings)
|
|
|
|
end
|
|
|
|
|
2018-09-10 09:48:09 +00:00
|
|
|
def clear_in_memory_application_settings!
|
2018-09-10 22:04:27 +00:00
|
|
|
@in_memory_application_settings = nil
|
2018-09-10 09:48:09 +00:00
|
|
|
end
|
|
|
|
|
2018-02-02 18:39:55 +00:00
|
|
|
def method_missing(name, *args, &block)
|
|
|
|
current_application_settings.send(name, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
2017-01-22 00:11:19 +00:00
|
|
|
|
2018-02-02 18:39:55 +00:00
|
|
|
def respond_to_missing?(name, include_private = false)
|
|
|
|
current_application_settings.respond_to?(name, include_private) || super
|
|
|
|
end
|
2016-01-28 09:14:21 +00:00
|
|
|
|
2018-02-02 18:39:55 +00:00
|
|
|
private
|
2017-06-06 16:48:10 +00:00
|
|
|
|
2018-02-02 18:39:55 +00:00
|
|
|
def ensure_application_settings!
|
2018-06-07 13:14:27 +00:00
|
|
|
cached_application_settings || uncached_application_settings
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_application_settings
|
2018-02-02 18:39:55 +00:00
|
|
|
return in_memory_application_settings if ENV['IN_MEMORY_APPLICATION_SETTINGS'] == 'true'
|
2018-06-07 13:14:27 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
::ApplicationSetting.cached
|
2018-06-07 15:23:38 +00: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 13:14:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def uncached_application_settings
|
2018-02-02 18:39:55 +00:00
|
|
|
return fake_application_settings unless connect_to_db?
|
2017-06-17 14:35:30 +00:00
|
|
|
|
2018-05-16 15:10:48 +00:00
|
|
|
current_settings = ::ApplicationSetting.current
|
2018-02-02 18:39:55 +00: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.
|
2020-02-17 18:09:00 +00:00
|
|
|
if Gitlab::Runtime.rake? && ActiveRecord::Base.connection.migration_context.needs_migration?
|
2018-12-12 11:59:06 +00:00
|
|
|
db_attributes = current_settings&.attributes || {}
|
2019-03-26 14:33:56 +00:00
|
|
|
fake_application_settings(db_attributes)
|
2018-12-14 09:14:21 +00:00
|
|
|
elsif current_settings.present?
|
|
|
|
current_settings
|
|
|
|
else
|
2018-12-14 14:45:14 +00:00
|
|
|
::ApplicationSetting.create_from_defaults
|
2018-05-16 15:10:48 +00:00
|
|
|
end
|
2018-02-02 18:39:55 +00:00
|
|
|
end
|
2016-11-04 22:54:24 +00:00
|
|
|
|
2018-12-14 09:14:21 +00:00
|
|
|
def fake_application_settings(attributes = {})
|
|
|
|
Gitlab::FakeApplicationSettings.new(::ApplicationSetting.defaults.merge(attributes || {}))
|
2018-05-16 15:10:48 +00:00
|
|
|
end
|
|
|
|
|
2018-12-14 09:14:21 +00:00
|
|
|
def in_memory_application_settings
|
|
|
|
@in_memory_application_settings ||= ::ApplicationSetting.build_from_defaults
|
2018-02-02 18:39:55 +00:00
|
|
|
end
|
2017-01-22 00:11:19 +00:00
|
|
|
|
2018-02-02 18:39:55 +00: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 11:23:37 +00:00
|
|
|
|
2018-02-02 18:39:55 +00:00
|
|
|
active_db_connection &&
|
2018-03-15 20:12:00 +00:00
|
|
|
Gitlab::Database.cached_table_exists?('application_settings')
|
2018-02-02 18:39:55 +00:00
|
|
|
rescue ActiveRecord::NoDatabaseError
|
|
|
|
false
|
|
|
|
end
|
2015-09-01 10:45:14 +00:00
|
|
|
end
|
2015-01-08 17:53:35 +00:00
|
|
|
end
|
|
|
|
end
|