Class: GitLab::Exporter::Database::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_exporter/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, logger: nil) ⇒ Base

Returns a new instance of Base.



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

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

Class Method Details

.connection_pool ⇒ Object



11
12
13
14
15
16
17
# File 'lib/gitlab_exporter/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_pool ⇒ Object



28
29
30
# File 'lib/gitlab_exporter/database/base.rb', line 28

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

#run ⇒ Object



24
25
26
# File 'lib/gitlab_exporter/database/base.rb', line 24

def run
  fail NotImplemented
end

#with_connection_pool ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gitlab_exporter/database/base.rb', line 32

def with_connection_pool
  connection_pool.with do |conn|
    begin
      yield conn
    rescue PG::UnableToSend => e
      @logger.warn "Error sending to the database: #{e}" if @logger
      conn.reset
      raise e
    end
  end
rescue PG::Error => e
  @logger.error "Error connecting to the database: #{e}" if @logger
  raise e
end