Class: ActiveRecord::PgTimeout

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/pgtimeout.rb,
lib/activerecord/pgtimeout/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout) ⇒ PgTimeout

Returns a new instance of PgTimeout.



7
8
9
# File 'lib/activerecord/pgtimeout.rb', line 7

def initialize(timeout)
  @timeout = timeout
end

Instance Attribute Details

#timeoutObject (readonly)

Returns the value of attribute timeout.



5
6
7
# File 'lib/activerecord/pgtimeout.rb', line 5

def timeout
  @timeout
end

Instance Method Details

#execute(timeoutable_action, on_timeout, connection = ActiveRecord::Base.connection) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/activerecord/pgtimeout.rb', line 11

def execute(timeoutable_action, on_timeout, connection = ActiveRecord::Base.connection)
  connection.transaction do
    connection.execute("set local statement_timeout to '#{timeout.to_i}ms'")
    timeoutable_action.()
  end

rescue ActiveRecord::StatementInvalid => e
  if e.original_exception.result.error_field(PG::Result::PG_DIAG_SQLSTATE) == "57014"
    on_timeout.()
  else
    raise e
  end
end