Module: XBar::Mapper::ClassMethods

Included in:
XBar::Mapper
Defined in:
lib/xbar/mapper.rb

Constant Summary collapse

@@cached_config =
nil
@@shards =
HashWithIndifferentAccess.new
@@connections =
HashWithIndifferentAccess.new
@@proxies =
{}
@@adapters =
Set.new
@@config =
nil
@@app_env =
nil
@@xbar_env =
nil

Instance Method Summary collapse

Instance Method Details

#app_envObject



221
222
223
# File 'lib/xbar/mapper.rb', line 221

def app_env
  @@app_env = XBar.rails_env || @@app_env
end

#configObject



95
96
97
# File 'lib/xbar/mapper.rb', line 95

def config
 @@cached_config ||= config_from_file
end

#config_file_nameObject



67
68
69
70
# File 'lib/xbar/mapper.rb', line 67

def config_file_name
  file = "#{xbar_env}.json"
  "#{XBar.directory}/config/#{file}"
end

#config_from_fileObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/xbar/mapper.rb', line 77

def config_from_file
  file_name = config_file_name
 
  if File.exists? file_name
    if XBar.debug
      puts "XBar::Mapper, reading configuration from file #{file_name}"
    end
    config = JSON.parse(ERB.new(File.read(file_name)).result)
  else
    if XBar.debug
      puts("XBar::Mapper: No config file #{file_name} -- " +
           "Deriving defaults.")
    end
    config = {}
  end
  HashWithIndifferentAccess.new(config)
end

#connection_file_nameObject



72
73
74
75
# File 'lib/xbar/mapper.rb', line 72

def connection_file_name
  file = "connection.rb"
  "#{XBar.directory}/config/#{file}"
end

#disconnect_all!Object



211
212
213
214
215
216
217
218
219
# File 'lib/xbar/mapper.rb', line 211

def disconnect_all!
  shards.each do |name, pool_list|
    pool_list.each_with_index do |p, i|
      if p.connected?
        p.disconnect!
      end
    end
  end
end

#environmentsObject

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



108
109
110
# File 'lib/xbar/mapper.rb', line 108

def environments
  config['environments'].keys
end

#initialize_options(aconfig) ⇒ Object



183
184
185
186
187
188
189
190
# File 'lib/xbar/mapper.rb', line 183

def initialize_options(aconfig)
  @@options = aconfig["environments"][app_env].dup
  @@options.delete("shards")
rescue
  @@options = {}
ensure
  @@options[:verify_connection] ||= false
end

#register(proxy) ⇒ Object

Register a proxy on behalf of the current thread. This is only called by Proxy#new. It is up to the calling thread to assign Thread.current = <new proxy> if it wishes.



195
196
197
198
199
200
201
# File 'lib/xbar/mapper.rb', line 195

def register(proxy)
	reset if shards.empty?
  if @@proxies[Thread.current.object_id]
    raise RuntimeError, "Mapper: already registered this proxy"
  end
  @@proxies[Thread.current.object_id] = proxy
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.



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

#shards=(shards) ⇒ Object

Alter the configuration in-memory for the current XBar envirnoment.



100
101
102
# File 'lib/xbar/mapper.rb', line 100

def shards=(shards)
  cached_config["environments"][app_env] = shards
end

#unregister(thread_spec = Thread.current) ⇒ Object

Unregister the proxy for the current thread, or for the specified thread. A thread can be specified by passing a Thread instance or its object_id.



206
207
208
209
# File 'lib/xbar/mapper.rb', line 206

def unregister(thread_spec = Thread.current)
  thread_spec = thread_spec.object_id if thread_spec.instance_of?(Thread)
  XBar::Mapper.proxies.delete(thread_spec)
end

#xbar_envObject



225
226
227
# File 'lib/xbar/mapper.rb', line 225

def xbar_env
  @env ||= 'default'
end