diff options
author | Gabriel Mazetto <gabriel@gitlab.com> | 2016-05-31 00:13:42 +0300 |
---|---|---|
committer | Gabriel Mazetto <gabriel@gitlab.com> | 2016-08-04 19:55:37 +0300 |
commit | 926cee002d701548b5344e0b93e95beb3802fd54 (patch) | |
tree | 41530bd8f003cca902724a48f8a3b10eb2a91303 /lib/gitlab | |
parent | 96abb19206c6910c8c25f1db77c663baff023d35 (diff) |
Deduplicated resque.yml loading from several places
We will trust redis configuration params loading to Gitlab::RedisConfig.
Diffstat (limited to 'lib/gitlab')
-rw-r--r-- | lib/gitlab/mail_room.rb | 44 | ||||
-rw-r--r-- | lib/gitlab/redis.rb | 84 |
2 files changed, 103 insertions, 25 deletions
diff --git a/lib/gitlab/mail_room.rb b/lib/gitlab/mail_room.rb new file mode 100644 index 00000000000..745cc79a10e --- /dev/null +++ b/lib/gitlab/mail_room.rb @@ -0,0 +1,44 @@ +require 'yaml' +require 'json' +require_relative 'lib/gitlab/redis' unless defined?(Gitlab::Redis) + +module Gitlab + module MailRoom + + class << self + + def enabled? + config[:enabled] && config[:address] + end + + def config + @config ||= fetch_config + end + + private + + def fetch_config + return nil unless File.exists?(config_file) + + rails_env = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' + all_config = YAML.load_file(config_file)[rails_env].deep_symbolize_keys + + config = all_config[:incoming_email] || {} + config[:enabled] = false if config[:enabled].nil? + config[:port] = 143 if config[:port].nil? + config[:ssl] = false if config[:ssl].nil? + config[:start_tls] = false if config[:start_tls].nil? + config[:mailbox] = 'inbox' if config[:mailbox].nil? + + if config[:enabled] && config[:address] + config[:redis_url] = Gitlab::Redis.new(rails_env).url + end + end + + def config_file + file = ENV['MAIL_ROOM_GITLAB_CONFIG_FILE'] || 'config/gitlab.yml' + File.expand_path("../../../#{file}", __FILE__) + end + end + end +end diff --git a/lib/gitlab/redis.rb b/lib/gitlab/redis.rb index 40766f35f77..8050b28f33a 100644 --- a/lib/gitlab/redis.rb +++ b/lib/gitlab/redis.rb @@ -1,50 +1,84 @@ +# This file should not have any direct dependency on Rails environment +# please require all dependencies below: +require 'active_support/core_ext/hash/keys' + module Gitlab class Redis CACHE_NAMESPACE = 'cache:gitlab' SESSION_NAMESPACE = 'session:gitlab' SIDEKIQ_NAMESPACE = 'resque:gitlab' - - attr_reader :url + MAILROOM_NAMESPACE = 'mail_room:gitlab' + DEFAULT_REDIS_URL = 'redis://localhost:6379' # To be thread-safe we must be careful when writing the class instance # variables @url and @pool. Because @pool depends on @url we need two # mutexes to prevent deadlock. - URL_MUTEX = Mutex.new + PARAMS_MUTEX = Mutex.new POOL_MUTEX = Mutex.new - private_constant :URL_MUTEX, :POOL_MUTEX + private_constant :PARAMS_MUTEX, :POOL_MUTEX - def self.url - @url || URL_MUTEX.synchronize { @url = new.url } - end + class << self + def params + @params || PARAMS_MUTEX.synchronize { @params = new.params } + end + + # @deprecated Use .params instead to get sentinel support + def url + raw_config_hash[:url] + end - def self.with - if @pool.nil? - POOL_MUTEX.synchronize do - @pool = ConnectionPool.new { ::Redis.new(url: url) } + def with + if @pool.nil? + POOL_MUTEX.synchronize do + @pool = ConnectionPool.new { ::Redis.new(params) } + end end + @pool.with { |redis| yield redis } end - @pool.with { |redis| yield redis } end - def self.redis_store_options - url = new.url - redis_config_hash = ::Redis::Store::Factory.extract_host_options_from_uri(url) - # Redis::Store does not handle Unix sockets well, so let's do it for them - redis_uri = URI.parse(url) + def initialize(rails_env=nil) + @rails_env = rails_env || Rails.env + end + + def params + redis_store_options + end + + private + + def redis_store_options + config = raw_config_hash + + redis_uri = URI.parse(config[:url]) if redis_uri.scheme == 'unix' - redis_config_hash[:path] = redis_uri.path + # Redis::Store does not handle Unix sockets well, so let's do it for them + config[:path] = redis_uri.path + config.delete(:url) + else + redis_hash = ::Redis::Store::Factory.extract_host_options_from_uri(redis_uri) + config.merge!(redis_hash) end - redis_config_hash + + config end - def initialize(rails_env=nil) - rails_env ||= Rails.env - config_file = File.expand_path('../../../config/resque.yml', __FILE__) + def raw_config_hash + config_data = fetch_config - @url = "redis://localhost:6379" - if File.exist?(config_file) - @url = YAML.load_file(config_file)[rails_env] + if config_data + config_data.is_a?(String) ? { url: config_data } : config_data.deep_symbolize_keys + else + { url: DEFAULT_REDIS_URL } end end + + def fetch_config + File.exists?(config_file) ? YAML.load_file(config_file)[@rails_env] : false + end + + def config_file + File.expand_path('../../../config/resque.yml', __FILE__) + end end end |