Method: XBar::Mapper::ClassMethods#reset

Defined in:
lib/xbar/mapper.rb

#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.



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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/xbar/mapper.rb', line 124

def reset(options = {})
  force = options.delete(:force)
  new_xbar_env = options[:xbar_env] || xbar_env
  if (new_xbar_env != xbar_env) || (options[:clear_cache]) ||
    (!@@cached_config.nil? && @@cached_config.empty?)
    @@cached_config = nil
  end
  self.xbar_env = new_xbar_env
  self.app_env = options[:app_env] if options[:app_env]
      
  if XBar.debug
    puts "XBar::Mapper#reset, xbar_env=#{xbar_env}, app_env=#{app_env}"
  end
  initialize_shards(config)
  initialize_options(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.connection_pool_without_xbar
  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.connection_pool_without_xbar
    end  
  end
  if !@@shards.keys.include?("master") && connection_pool
    @@shards[:master] = Array(connection_pool)
    @@adapters << connection_pool.spec.config
  end
  
  @@proxies.values.each do |proxy|
    if force
      proxy.do_reset
    else
      proxy.request_reset
    end
  end

  self
end