Module: NoBrainer::ConnectionManager

Extended by:
ConnectionManager
Included in:
ConnectionManager
Defined in:
lib/no_brainer/connection_manager.rb

Instance Method Summary collapse

Instance Method Details

#_disconnectObject



64
65
66
67
# File 'lib/no_brainer/connection_manager.rb', line 64

def _disconnect
  c, self.current_connection = self.current_connection, nil
  c.try(:close, :noreply_wait => false) rescue nil
end

#connectionObject



55
56
57
58
59
60
61
62
# File 'lib/no_brainer/connection_manager.rb', line 55

def connection
  c = self.current_connection
  return c if c

  synchronize do
    self.current_connection ||= get_new_connection
  end
end

#current_connectionObject



33
34
35
36
37
38
39
# File 'lib/no_brainer/connection_manager.rb', line 33

def current_connection
  if NoBrainer::Config.per_thread_connection
    Thread.current[:nobrainer_connection]
  else
    @connection
  end
end

#current_connection=(value) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/no_brainer/connection_manager.rb', line 41

def current_connection=(value)
  if NoBrainer::Config.per_thread_connection
    Thread.current[:nobrainer_connection] = value
  else
    @connection = value
  end
end

#disconnectObject



69
70
71
72
# File 'lib/no_brainer/connection_manager.rb', line 69

def disconnect
  return unless self.current_connection
  synchronize { _disconnect }
end

#get_new_connectionObject



24
25
26
27
28
29
30
31
# File 'lib/no_brainer/connection_manager.rb', line 24

def get_new_connection
  # We don't want to warn on "rails g nobrainer:install", but because it's
  # hard to check when the generator is running because of spring as it wipes
  # ARGV. So we check for other ORMs during the connection instantiation.
  warn_for_other_orms

  NoBrainer::Connection.new(get_next_url)
end

#get_next_urlObject



49
50
51
52
53
# File 'lib/no_brainer/connection_manager.rb', line 49

def get_next_url
  @urls ||= NoBrainer::Config.rethinkdb_urls.shuffle
  @cycle_index = (@cycle_index || 0) + 1
  @urls[@cycle_index % @urls.size] # not using .cycle due to threading issues
end

#notify_url_changeObject



74
75
76
77
78
79
80
# File 'lib/no_brainer/connection_manager.rb', line 74

def notify_url_change
  synchronize do
    @urls = nil
    c = current_connection
    _disconnect if c && !NoBrainer::Config.rethinkdb_urls.include?(c.orig_uri)
  end
end

#synchronize(&block) ⇒ Object



6
7
8
# File 'lib/no_brainer/connection_manager.rb', line 6

def synchronize(&block)
  @lock.synchronize { block.call }
end

#warn_for_other_ormsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/no_brainer/connection_manager.rb', line 10

def warn_for_other_orms
  if defined?(ActiveRecord) && NoBrainer::Config.warn_on_active_record
    STDERR.puts "[NoBrainer] ActiveRecord is loaded which is probably not what you want."
    STDERR.puts "[NoBrainer] Follow the instructions on http://nobrainer.io/docs/configuration/#removing_activerecord"
    STDERR.puts "[NoBrainer] Configure NoBrainer with 'config.warn_on_active_record = false' to disable with warning."
  end

  if defined?(Mongoid)
    STDERR.puts "[NoBrainer] WARNING: Mongoid is loaded, and we conflict on the symbol decorations"
    STDERR.puts "[NoBrainer] They are used in queries such as Model.where(:tags.in => ['fun', 'stuff'])"
    STDERR.puts "[NoBrainer] This is a problem!"
  end
end