Class: Idempo::ActiveRecordBackend::MysqlLock

Inherits:
Object
  • Object
show all
Defined in:
lib/idempo/active_record_backend.rb

Instance Method Summary collapse

Instance Method Details

#acquire(connection, based_on_str) ⇒ Object



46
47
48
49
# File 'lib/idempo/active_record_backend.rb', line 46

def acquire(connection, based_on_str)
  did_acquire = connection.select_value("SELECT GET_LOCK(%s, %d)" % [connection.quote(derive_lock_name(based_on_str)), 0])
  did_acquire == 1
end

#derive_lock_name(from_str) ⇒ Object



55
56
57
58
# File 'lib/idempo/active_record_backend.rb', line 55

def derive_lock_name(from_str)
  db_safe_key = Base64.strict_encode64(from_str)
  "idempo_%s" % db_safe_key[0...57] # Note there is a limit of 64 bytes on the lock name
end

#release(connection, based_on_str) ⇒ Object



51
52
53
# File 'lib/idempo/active_record_backend.rb', line 51

def release(connection, based_on_str)
  connection.select_value("SELECT RELEASE_LOCK(%s)" % connection.quote(derive_lock_name(based_on_str)))
end