Class: GitLab::Monitor::Database::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_monitor/database/base.rb

Overview

An abstract class for interacting with DB

It takes a connection string (e.g. “dbname=test port=5432”)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Base

Returns a new instance of Base.



19
20
21
# File 'lib/gitlab_monitor/database/base.rb', line 19

def initialize(args)
  @connection_string = args[:connection_string]
end

Class Method Details

.connection_poolObject



11
12
13
14
15
16
17
# File 'lib/gitlab_monitor/database/base.rb', line 11

def self.connection_pool
  @connection_pool ||= Hash.new do |h, connection_string|
    h[connection_string] = ConnectionPool.new(size: 3, timeout: 5) do
      PG.connect(connection_string)
    end
  end
end

Instance Method Details

#connection_poolObject



27
28
29
# File 'lib/gitlab_monitor/database/base.rb', line 27

def connection_pool
  self.class.connection_pool[@connection_string]
end

#runObject



23
24
25
# File 'lib/gitlab_monitor/database/base.rb', line 23

def run
  fail NotImplemented
end

#with_connection_poolObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab_monitor/database/base.rb', line 31

def with_connection_pool
  connection_pool.with do |conn|
    begin
      yield conn
    rescue PG::UnableToSend => e
      conn.reset
      raise e
    end
  end
end