Class: Querrel::Querreller

Inherits:
Object
  • Object
show all
Includes:
MapReduce
Defined in:
lib/querrel/querreller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MapReduce

#map, #query, #reduce

Constructor Details

#initialize(dbs, options = {}) ⇒ Querreller

Returns a new instance of Querreller.



11
12
13
14
# File 'lib/querrel/querreller.rb', line 11

def initialize(dbs, options = {})
  @connection_resolver = ConnectionResolver.new(dbs, options.delete(:db_names))
  @options = options
end

Instance Attribute Details

#connection_resolverObject

Returns the value of attribute connection_resolver.



9
10
11
# File 'lib/querrel/querreller.rb', line 9

def connection_resolver
  @connection_resolver
end

Instance Method Details

#retrieve_connection_spec(db, resolver) ⇒ Object



45
46
47
# File 'lib/querrel/querreller.rb', line 45

def retrieve_connection_spec(db, resolver)
  resolver.spec(db.to_sym)
end

#run(options = {}, &blk) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/querrel/querreller.rb', line 16

def run(options = {}, &blk)
  options = @options.merge(options)
  if options.key?(:on)
    resolver = ConnectionResolver.new(options[:on], !!options[:db_names])
    dbs = resolver.configurations.keys
  else
    resolver = @connection_resolver
    dbs = @connection_resolver.configurations.keys
  end

  pool = StaticPool.new(options[:threads] || 20)
  dbs.each do |db|
    pool.enqueue do
      begin
        Thread.current[:querrel_connected_models] = []
        con_spec = retrieve_connection_spec(db, resolver)
        Thread.current[:querrel_con_spec] = con_spec
        yield(ConnectedModelFactory)
      ensure
        Thread.current[:querrel_connected_models].each do |m|
          m.connection_pool.release_connection
        end
        Thread.current[:querrel_connected_models] = nil
      end
    end
  end
  pool.do_your_thang!
end

#while_connected_to(db, resolver, &b) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/querrel/querreller.rb', line 49

def while_connected_to(db, resolver, &b)
  conf = resolver.spec(db.to_sym)
  pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(conf)
  pool.with_connection(&b)
ensure
  pool.disconnect!
end