Class: ActiveRecord::ConnectionAdapters::Mysql2Adapter

Inherits:
AbstractMysqlAdapter
  • Object
show all
Defined in:
lib/non_blocking_lock/mysql2_adapter.rb

Instance Method Summary collapse

Instance Method Details

#non_blocking_lock(name, wait_time = 1.0) ⇒ Object

name - lock name wait_time - default 1.second &block to execute if lock obtained return false if failed to get lock

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/non_blocking_lock/mysql2_adapter.rb', line 10

def non_blocking_lock( name, wait_time=1.0 )
  raise ArgumentError.new("name can't be empty") if name.blank?
  raise ArgumentError.new("wait_time can't be less then 0") if wait_time.to_f <= 0

  name = quote_string( name )

  begin
    mysql_result = select_value( "SELECT GET_LOCK('#{name}', #{wait_time.to_f})" )
    mysql_get_lock_obtained = mysql_result.to_s == "1"
    yield if mysql_get_lock_obtained
    return mysql_get_lock_obtained
  ensure
    select_value("SELECT RELEASE_LOCK('#{name}')") if mysql_get_lock_obtained
  end
end