lib/octopus.rb in ar-octopus-0.8.5 vs lib/octopus.rb in ar-octopus-0.8.6

- old
+ new

@@ -9,16 +9,16 @@ def self.env @env ||= 'octopus' end def self.rails_env - @rails_env ||= self.rails? ? Rails.env.to_s : 'shards' + @rails_env ||= defined?(::Rails.env) ? Rails.env.to_s : 'shards' end def self.config @config ||= begin - file_name = Octopus.directory + '/config/shards.yml' + file_name = File.join(Octopus.directory, 'config/shards.yml').to_s if File.exist?(file_name) || File.symlink?(file_name) config ||= HashWithIndifferentAccess.new(YAML.load(ERB.new(File.read(file_name)).result))[Octopus.env] else config ||= HashWithIndifferentAccess.new @@ -26,17 +26,29 @@ config end end + def self.load_balancer=(balancer) + @load_balancer = balancer + end + + def self.load_balancer + @load_balancer ||= Octopus::LoadBalancing::RoundRobin + end + + def self.master_shard + ((config && config[:master_shard]) || :master).to_sym + end + # Public: Whether or not Octopus is configured and should hook into the # current environment. Checks the environments config option for the Rails # environment by default. # # Returns a boolean def self.enabled? - if defined?(::Rails) + if defined?(::Rails.env) Octopus.environments.include?(Rails.env.to_s) else # TODO: This doens't feel right but !Octopus.config.blank? is breaking a # test. Also, Octopus.config is always returning a hash. Octopus.config @@ -44,11 +56,11 @@ end # Returns the Rails.root_to_s when you are using rails # Running the current directory in a generic Ruby process def self.directory - @directory ||= defined?(Rails) ? Rails.root.to_s : Dir.pwd + @directory ||= defined?(::Rails.root) ? Rails.root.to_s : Dir.pwd end # This is the default way to do Octopus Setup # Available variables: # :enviroments => the enviroments that octopus will run. default: 'production' @@ -88,18 +100,14 @@ def self.rails41? rails4? && ActiveRecord::VERSION::MINOR >= 1 end - def self.rails? - defined?(Rails) - end - attr_writer :logger def self.logger - if defined?(Rails) + if defined?(Rails.logger) @logger ||= Rails.logger else @logger ||= Logger.new($stderr) end end @@ -117,19 +125,41 @@ else yield end end + def self.using_group(group, &block) + conn = ActiveRecord::Base.connection + + if conn.is_a?(Octopus::Proxy) + conn.send_queries_to_group(group, &block) + else + yield + end + end + + def self.using_all(&block) + conn = ActiveRecord::Base.connection + + if conn.is_a?(Octopus::Proxy) + conn.send_queries_to_all_shards(&block) + else + yield + end + end + def self.fully_replicated(&_block) - old_fully_replicated = Thread.current['octopus.fully_replicated'] - Thread.current['octopus.fully_replicated'] = true + old_fully_replicated = Thread.current[Octopus::Proxy::FULLY_REPLICATED_KEY] + Thread.current[Octopus::Proxy::FULLY_REPLICATED_KEY] = true yield ensure - Thread.current['octopus.fully_replicated'] = old_fully_replicated + Thread.current[Octopus::Proxy::FULLY_REPLICATED_KEY] = old_fully_replicated end end +require 'octopus/exception' + require 'octopus/shard_tracking' require 'octopus/shard_tracking/attribute' require 'octopus/shard_tracking/dynamic' require 'octopus/model' @@ -141,10 +171,10 @@ require 'octopus/persistence' require 'octopus/log_subscriber' require 'octopus/abstract_adapter' require 'octopus/singular_association' -require 'octopus/railtie' if defined?(::Rails) +require 'octopus/railtie' if defined?(::Rails::Railtie) require 'octopus/proxy' require 'octopus/collection_proxy' require 'octopus/relation_proxy' require 'octopus/scope_proxy'