Commit ce20628e authored by Jacob Vosmaer's avatar Jacob Vosmaer

Factor out redis store duplication

There was a lot of duplicated code between Redis::Cache, Redis::Queues
and Redis::SharedState. This change replaces it with lookups based on
the class name.
parent 8f509739
......@@ -7,29 +7,10 @@ module Gitlab
module Redis
class Cache < ::Gitlab::Redis::Wrapper
CACHE_NAMESPACE = 'cache:gitlab'
DEFAULT_REDIS_CACHE_URL = 'redis://localhost:6380'
REDIS_CACHE_CONFIG_ENV_VAR_NAME = 'GITLAB_REDIS_CACHE_CONFIG_FILE'
class << self
def default_url
DEFAULT_REDIS_CACHE_URL
end
def config_file_name
# if ENV set for this class, use it even if it points to a file does not exist
file_name = ENV[REDIS_CACHE_CONFIG_ENV_VAR_NAME]
return file_name unless file_name.nil?
# otherwise, if config files exists for this class, use it
file_name = config_file_path('redis.cache.yml')
return file_name if File.file?(file_name)
# this will force use of DEFAULT_REDIS_QUEUES_URL when config file is absent
super
end
def instrumentation_class
::Gitlab::Instrumentation::Redis::Cache
'redis://localhost:6380'
end
end
end
......
......@@ -8,29 +8,10 @@ module Gitlab
class Queues < ::Gitlab::Redis::Wrapper
SIDEKIQ_NAMESPACE = 'resque:gitlab'
MAILROOM_NAMESPACE = 'mail_room:gitlab'
DEFAULT_REDIS_QUEUES_URL = 'redis://localhost:6381'
REDIS_QUEUES_CONFIG_ENV_VAR_NAME = 'GITLAB_REDIS_QUEUES_CONFIG_FILE'
class << self
def default_url
DEFAULT_REDIS_QUEUES_URL
end
def config_file_name
# if ENV set for this class, use it even if it points to a file does not exist
file_name = ENV[REDIS_QUEUES_CONFIG_ENV_VAR_NAME]
return file_name if file_name
# otherwise, if config files exists for this class, use it
file_name = config_file_path('redis.queues.yml')
return file_name if File.file?(file_name)
# this will force use of DEFAULT_REDIS_QUEUES_URL when config file is absent
super
end
def instrumentation_class
::Gitlab::Instrumentation::Redis::Queues
'redis://localhost:6381'
end
end
end
......
......@@ -10,29 +10,10 @@ module Gitlab
USER_SESSIONS_NAMESPACE = 'session:user:gitlab'
USER_SESSIONS_LOOKUP_NAMESPACE = 'session:lookup:user:gitlab'
IP_SESSIONS_LOOKUP_NAMESPACE = 'session:lookup:ip:gitlab2'
DEFAULT_REDIS_SHARED_STATE_URL = 'redis://localhost:6382'
REDIS_SHARED_STATE_CONFIG_ENV_VAR_NAME = 'GITLAB_REDIS_SHARED_STATE_CONFIG_FILE'
class << self
def default_url
DEFAULT_REDIS_SHARED_STATE_URL
end
def config_file_name
# if ENV set for this class, use it even if it points to a file does not exist
file_name = ENV[REDIS_SHARED_STATE_CONFIG_ENV_VAR_NAME]
return file_name if file_name
# otherwise, if config files exists for this class, use it
file_name = config_file_path('redis.shared_state.yml')
return file_name if File.file?(file_name)
# this will force use of DEFAULT_REDIS_SHARED_STATE_URL when config file is absent
super
end
def instrumentation_class
::Gitlab::Instrumentation::Redis::SharedState
'redis://localhost:6382'
end
end
end
......
# frozen_string_literal: true
# This file should only be used by sub-classes, not directly by any clients of the sub-classes
# please require all dependencies below:
# Explicitly load parts of ActiveSupport because MailRoom does not load
# Rails.
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/string/inflections'
module Gitlab
module Redis
class Wrapper
DEFAULT_REDIS_URL = 'redis://localhost:6379'
REDIS_CONFIG_ENV_VAR_NAME = 'GITLAB_REDIS_CONFIG_FILE'
class << self
delegate :params, :url, to: :new
......@@ -52,11 +52,12 @@ module Gitlab
end
def default_url
DEFAULT_REDIS_URL
raise NotImplementedError
end
def config_file_path(filename)
File.join(rails_root, 'config', filename)
path = File.join(rails_root, 'config', filename)
return path if File.file?(path)
end
# We need this local implementation of Rails.root because MailRoom
......@@ -66,20 +67,20 @@ module Gitlab
end
def config_file_name
# if ENV set for wrapper class, use it even if it points to a file does not exist
file_name = ENV[REDIS_CONFIG_ENV_VAR_NAME]
return file_name unless file_name.nil?
# otherwise, if config files exists for wrapper class, use it
file_name = config_file_path('resque.yml')
return file_name if File.file?(file_name)
[
ENV["GITLAB_REDIS_#{store_name.underscore.upcase}_CONFIG_FILE"],
config_file_path("redis.#{store_name.underscore}.yml"),
ENV['GITLAB_REDIS_CONFIG_FILE'],
config_file_path('resque.yml')
].compact.first
end
# nil will force use of DEFAULT_REDIS_URL when config file is absent
nil
def store_name
name.demodulize
end
def instrumentation_class
raise NotImplementedError
"::Gitlab::Instrumentation::Redis::#{store_name}".constantize
end
end
......
......@@ -5,7 +5,7 @@ require 'spec_helper'
RSpec.describe Gitlab::Redis::Cache do
let(:instance_specific_config_file) { "config/redis.cache.yml" }
let(:environment_config_file_name) { "GITLAB_REDIS_CACHE_CONFIG_FILE" }
let(:class_redis_url) { Gitlab::Redis::Cache::DEFAULT_REDIS_CACHE_URL }
let(:class_redis_url) { 'redis://localhost:6380' }
include_examples "redis_shared_examples"
end
......@@ -5,7 +5,7 @@ require 'spec_helper'
RSpec.describe Gitlab::Redis::Queues do
let(:instance_specific_config_file) { "config/redis.queues.yml" }
let(:environment_config_file_name) { "GITLAB_REDIS_QUEUES_CONFIG_FILE" }
let(:class_redis_url) { Gitlab::Redis::Queues::DEFAULT_REDIS_QUEUES_URL }
let(:class_redis_url) { 'redis://localhost:6381' }
include_examples "redis_shared_examples"
end
......@@ -5,7 +5,7 @@ require 'spec_helper'
RSpec.describe Gitlab::Redis::SharedState do
let(:instance_specific_config_file) { "config/redis.shared_state.yml" }
let(:environment_config_file_name) { "GITLAB_REDIS_SHARED_STATE_CONFIG_FILE" }
let(:class_redis_url) { Gitlab::Redis::SharedState::DEFAULT_REDIS_SHARED_STATE_URL }
let(:class_redis_url) { 'redis://localhost:6382' }
include_examples "redis_shared_examples"
end
......@@ -4,8 +4,14 @@ require 'spec_helper'
RSpec.describe Gitlab::Redis::Wrapper do
describe '.instrumentation_class' do
it 'raises a NotImplementedError' do
expect { described_class.instrumentation_class }.to raise_error(NotImplementedError)
it 'raises a NameError' do
expect { described_class.instrumentation_class }.to raise_error(NameError)
end
end
describe '.default_url' do
it 'is not implemented' do
expect { described_class.default_url }.to raise_error(NotImplementedError)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment