Class: Isono::Rack::Sequel

Inherits:
Decorator show all
Includes:
Logger
Defined in:
lib/isono/rack/sequel.rb

Overview

Middleware for Sequel transaction.

Instance Attribute Summary

Attributes inherited from Decorator

#app

Instance Method Summary collapse

Methods included from Logger

included, initialize

Constructor Details

#initialize(app, retry_count = 10) ⇒ Sequel

Returns a new instance of Sequel.



11
12
13
14
# File 'lib/isono/rack/sequel.rb', line 11

def initialize(app, retry_count=10)
  @app = app
  @retry_count = retry_count
end

Instance Method Details

#call(req, res) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/isono/rack/sequel.rb', line 16

def call(req, res)
  retry_count=0
  begin
    ::Sequel::DATABASES.first.transaction do
      @app.call(req, res)
      res.response(ret) unless res.responded?
    end
  rescue ::Sequel::DatabaseError, ::Sequel::DatabaseConnectionError => e
    retry_count += 1
    if retry_count < @retry_count
      logger.error("Database Error: #{e.message} retrying #{retry_count}/#{@retry_count}")
      retry
    end
    
    res.response(e) unless res.responded?
    raise e
  end
end