Module: XBar::Mapper::ClassMethods
- Included in:
- XBar::Mapper
- Defined in:
- lib/xbar/mapper.rb
Constant Summary collapse
- @@cached_config =
nil
HashWithIndifferentAccess.new
- @@connections =
HashWithIndifferentAccess.new
- @@proxies =
[]
- @@adapters =
Set.new
- @@config =
nil
- @@app_env =
nil
- @@xbar_env =
nil
Instance Method Summary collapse
- #app_env ⇒ Object
- #config ⇒ Object
- #config_file_name ⇒ Object
- #config_from_file ⇒ Object
- #connection_file_name ⇒ Object
-
#environments ⇒ Object
This needs to be reconciled with the ‘environments’ method in the XBar module.
- #initialize_options(aconfig) ⇒ Object
- #register(proxy) ⇒ Object
-
#reset(options = {}) ⇒ Object
When we switch the XBar env or the Rails env (either of which changes the set of available shards, we have to find all the connection proxies and reset their current shard to :master.).
-
#shards=(shards) ⇒ Object
Alter the configuration in-memory for the current XBar envirnoment.
- #xbar_env ⇒ Object
Instance Method Details
#app_env ⇒ Object
185 186 187 |
# File 'lib/xbar/mapper.rb', line 185 def app_env @@app_env = XBar.rails_env || @@app_env end |
#config ⇒ Object
88 89 90 |
# File 'lib/xbar/mapper.rb', line 88 def config @@cached_config ||= config_from_file end |
#config_file_name ⇒ Object
67 68 69 70 |
# File 'lib/xbar/mapper.rb', line 67 def config_file_name file = "#{}.json" "#{XBar.directory}/config/#{file}" end |
#config_from_file ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/xbar/mapper.rb', line 77 def config_from_file file_name = config_file_name puts "XBar::Mapper, reading configuration from file #{file_name}" if File.exists? file_name config = JSON.parse(ERB.new(File.read(file_name)).result) else config = {} end HashWithIndifferentAccess.new(config) end |
#connection_file_name ⇒ Object
72 73 74 75 |
# File 'lib/xbar/mapper.rb', line 72 def connection_file_name file = "connection.rb" "#{XBar.directory}/config/#{file}" end |
#environments ⇒ Object
This needs to be reconciled with the ‘environments’ method in the XBar module. That method specifies the environments that XBar should be enabled for. The present method returns the environments that the current config file contains. XXX
101 102 103 |
# File 'lib/xbar/mapper.rb', line 101 def environments config['environments'].keys end |
#initialize_options(aconfig) ⇒ Object
168 169 170 171 172 173 174 175 |
# File 'lib/xbar/mapper.rb', line 168 def (aconfig) @@options = aconfig["environments"][app_env].dup @@options.delete("shards") rescue @@options = {} ensure @@options[:verify_connection] ||= false end |
#register(proxy) ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/xbar/mapper.rb', line 177 def register(proxy) @@proxies << proxy # If we hang on to a reference to proxies here, the proxy will # never be garbage collected, even when the thread that it was # assigned to goes away. Find a way to fix this. XXX end |
#reset(options = {}) ⇒ Object
When we switch the XBar env or the Rails env (either of which changes the set of available shards, we have to find all the connection proxies and reset their current shard to :master.)
Q1. Are all the connection proxies pointed to by model classes findable through Thread.current? We’ll have to loop over all threads. XXX
Alternatively, we can register each XBar::Proxy.new call to a hash in the XBar module.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/xbar/mapper.rb', line 117 def reset( = {}) = [:xbar_env] || if ( != ) || ([:clear_cache]) || (!@@cached_config.nil? && @@cached_config.empty?) @@cached_config = nil end self. = self.app_env = [:app_env] if [:app_env] # puts "XBar::Mapper#reset, xbar_env=#{xbar_env}, app_env=#{app_env}" initialize_shards(config) (config) # If Rails or some other entity has not assigned a native connection # for ActiveRecord, we will try to do something sensible. This is only # needed if you have some enviroments for which XBar is not enabled. # However, it's not likely you'll want to enable XBar for only some # environments. (What would be a use case?) The first # choice is that if we have a shard called 'master', we will use its # connection specification. The second choice is to include a Ruby # file that contains a call to 'establish connection'. In this case, # we will create a shard called master with the same connection # specification. Thus there will always be a 'master' shard. # # Also, there is the case where there is a connection, but the config # document didn't specify a master shard. begin connection_pool = ActiveRecord::Base. rescue if @@shards.keys.include? "master" ActiveRecord::Base.establish_connection( XBar::Mapper.shards[:master][0].spec.config) else # The config file didn't exist or didn't specify a master shard. Or # app_env wasn't specified (as an argument option). require connection_file_name connection_pool = ActiveRecord::Base. end end if !@@shards.keys.include?("master") && connection_pool @@shards[:master] = Array(connection_pool) @@adapters << connection_pool.spec.config end @@proxies.each do |proxy| proxy.reset_proxy end self end |
#shards=(shards) ⇒ Object
Alter the configuration in-memory for the current XBar envirnoment.
93 94 95 |
# File 'lib/xbar/mapper.rb', line 93 def shards=(shards) cached_config["environments"][app_env] = shards end |
#xbar_env ⇒ Object
189 190 191 |
# File 'lib/xbar/mapper.rb', line 189 def @env ||= 'default' end |