Module: DWH::Factory
Overview
Manages adapters. This should be the means by which adapters are created, loaded, and pooled.
Instance Method Summary collapse
-
#adapter?(adapter_name) ⇒ Boolean
Check if the given adapter is registered.
-
#adapters ⇒ Object
Get the list of registed adapters.
-
#create(adapter_name, config) ⇒ Object
The canonical way of creating an adapter instance in DWH.
-
#get_adapter(adapter_name) ⇒ Object
Get the adapter.
-
#pool(name, adapter_name, config, timeout: 5, size: 10) ⇒ Object
Create a pool of connections for a given name and adapter.
-
#pools ⇒ Object
Current active pools.
-
#register(adapter_name, adapter_class) ⇒ Object
Register your new adapter.
-
#shutdown(pool = nil) ⇒ Object
Shutdown a specific pool or all pools.
-
#start_reaper(frequency = 300) ⇒ Object
Start reaper that will periodically clean up unused or idle connections.
-
#unregister(adapter_name) ⇒ Object
Remove the given adapter from the registry.
Methods included from Logger
Instance Method Details
#adapter?(adapter_name) ⇒ Boolean
Check if the given adapter is registered
35 36 37 |
# File 'lib/dwh/factory.rb', line 35 def adapter?(adapter_name) adapters.key?(adapter_name.to_sym) end |
#adapters ⇒ Object
Get the list of registed adapters
40 41 42 |
# File 'lib/dwh/factory.rb', line 40 def adapters @adapters ||= {} end |
#create(adapter_name, config) ⇒ Object
The canonical way of creating an adapter instance in DWH.
60 61 62 |
# File 'lib/dwh/factory.rb', line 60 def create(adapter_name, config) get_adapter(adapter_name).new(config) end |
#get_adapter(adapter_name) ⇒ Object
Get the adapter.
27 28 29 30 31 |
# File 'lib/dwh/factory.rb', line 27 def get_adapter(adapter_name) raise "Adapter '#{adapter_name}' not found. Did you forget to register it: DWH.register(MyAdapterClass)" unless adapter?(adapter_name) adapters[adapter_name.to_sym] end |
#pool(name, adapter_name, config, timeout: 5, size: 10) ⇒ Object
Create a pool of connections for a given name and adapter. Returns existing pool if it was already created.
72 73 74 75 76 77 78 79 80 |
# File 'lib/dwh/factory.rb', line 72 def pool(name, adapter_name, config, timeout: 5, size: 10) if pools.key?(name) pools[name] else pools[name] = ConnectionPool.new(size: size, timeout: timeout) do create(adapter_name, config) end end end |
#pools ⇒ Object
Current active pools
45 46 47 |
# File 'lib/dwh/factory.rb', line 45 def pools @pools ||= {} end |
#register(adapter_name, adapter_class) ⇒ Object
Register your new adapter.
13 14 15 16 17 18 |
# File 'lib/dwh/factory.rb', line 13 def register(adapter_name, adapter_class) raise ConfigError, 'adapter_class should be a class' unless adapter_class.is_a?(Class) adapter_class.load_settings adapters[adapter_name.to_sym] = adapter_class end |
#shutdown(pool = nil) ⇒ Object
Shutdown a specific pool or all pools
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/dwh/factory.rb', line 85 def shutdown(pool = nil) case pool.class when String pools[pool].shutdown { it.close } pools.delete(pool) when Symbol pools[pool.to_s].shutdown { it.close } pools[pool.to_s].delete when ConnectionPool pool.shutdown { it.close } pools.delete(pools.key(pool)) else pools.each_value do |val| val.shutdown { c.close } end @pools = {} end end |
#start_reaper(frequency = 300) ⇒ Object
Start reaper that will periodically clean up unused or idle connections.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/dwh/factory.rb', line 107 def start_reaper(frequency = 300) logger.info 'Starting DB Adapter reaper process' Thread.new do loop do pools.each do |name, pool| logger.info "DB POOL FOR #{name} STATS:" pool.with do logger.info "\tSize: #{pool.size}" logger.info "\tIdle: #{pool.available}" logger.info "\tAvailable: #{pool.available}" end pool.reap(frequency) { it.close } end sleep frequency end end end |
#unregister(adapter_name) ⇒ Object
Remove the given adapter from the registry.
21 22 23 |
# File 'lib/dwh/factory.rb', line 21 def unregister(adapter_name) adapters.delete adapter_name.to_sym end |