Class: Mimi::DB::Lock::MysqlLock

Inherits:
Object
  • Object
show all
Defined in:
lib/mimi/db/lock/mysql_lock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ MysqlLock

Timeout semantics: nil – wait indefinitely 0 – do not wait <s> – wait <s> seconds (can be Float)



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mimi/db/lock/mysql_lock.rb', line 13

def initialize(name, opts = {})
  @name = name
  @name_uint64 = Digest::SHA1.digest(name).unpack('q').first
  @options = opts
  @timeout =
    if opts[:timeout].nil?
      -1
    elsif opts[:timeout] <= 0
      0
    else
      opts[:timeout].to_f.round
    end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/mimi/db/lock/mysql_lock.rb', line 5

def name
  @name
end

#name_uint64Object (readonly)

Returns the value of attribute name_uint64.



5
6
7
# File 'lib/mimi/db/lock/mysql_lock.rb', line 5

def name_uint64
  @name_uint64
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/mimi/db/lock/mysql_lock.rb', line 5

def options
  @options
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



5
6
7
# File 'lib/mimi/db/lock/mysql_lock.rb', line 5

def timeout
  @timeout
end

Instance Method Details

#execute(&_block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/mimi/db/lock/mysql_lock.rb', line 27

def execute(&_block)
  ActiveRecord::Base.transaction(requires_new: true) do
    begin
      acquire_lock_with_timeout!
      yield if block_given?
    ensure
      release_lock!
    end
  end
end