Class: Simple::Sharding::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/simple/sharding/core.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config(env = Rails.env) ⇒ Object

configuration



25
26
27
28
29
30
31
32
# File 'lib/simple/sharding/core.rb', line 25

def self.config(env=Rails.env)
  @@db_config ||= YAML.load_file(Config.config(:shard_config_file))
  env_config = @@db_config[env]
  return env_config if env_config

  # error
  raise "No correct config for env: #{env}."
end

.sharding(shard_id) ⇒ Object

block support



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/simple/sharding/core.rb', line 9

def self.sharding(shard_id)
  begin
    raise "no shard group #{shard_id} found in pool." if ConnectionHandler.check(shard_id)
    ShardThreadRegistry.push_current_shard(shard_id)
    yield
  ensure
    # Releases connections in case user left some connection in the reserved state
    # (by calling retrieve_connection instead of with_connection). Also, using
    # normal activerecord queries leaves a connection in the reserved state
    # Obs: don't do this with a master database connection
    ConnectionHandler.connection_pool(shard_id).release_connection if !shard_id.blank?
  end

end

Instance Method Details

#setupObject



34
35
36
37
38
39
40
41
# File 'lib/simple/sharding/core.rb', line 34

def setup
  if block_given?
    yield Config
  end

  ConnectionHandler.connect_all
  ActiveRecordExtensions.extend_active_record_scope
end