Module: Rubee::DBTools

Defined in:
lib/rubee/models/db_tools.rb

Constant Summary collapse

MAX_RETRIES =
Rubee::Configuration.get_db_max_retries || 3
DELAY =
Rubee::Configuration.get_db_retry_delay || 0.1
BUSY_TIMEOUT =
Rubee::Configuration.get_db_busy_timeout || 2000

Class Method Summary collapse

Class Method Details

.set_prerequisites!Object



25
26
27
28
29
30
31
32
33
# File 'lib/rubee/models/db_tools.rb', line 25

def set_prerequisites!
  # Necessary changes to make sqlite be none blocking
  if Rubee::SequelObject::DB.adapter_scheme == :sqlite
    # WAL mode allows concurrent reads and non-blocking writes.
    Rubee::SequelObject::DB.execute("PRAGMA journal_mode = WAL")
    # Wait 2000ms for a write lock.
    Rubee::SequelObject::DB.execute("PRAGMA busy_timeout = #{BUSY_TIMEOUT}")
  end
end

.with_retryObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubee/models/db_tools.rb', line 8

def with_retry
  retries = 0
  begin
    yield
  rescue Sequel::DatabaseError => e
    # Applicable for msqlite only, however it can be extended in the future
    if Rubee::SequelObject::DB.adapter_scheme == :sqlite &&
        e.cause.is_a?(SQLite3::BusyException) && retries < MAX_RETRIES
      retries += 1
      sleep(DELAY)
      retry
    else
      raise e
    end
  end
end