Class: Mimi::DB::Lock::PostgresqlLock

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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/postgresql_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?
      0
    elsif opts[:timeout] <= 0
      :nowait
    else
      opts[:timeout]
    end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/mimi/db/lock/postgresql_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/postgresql_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/postgresql_lock.rb', line 5

def options
  @options
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Instance Method Details

#execute(&_block) ⇒ Object



27
28
29
30
31
32
# File 'lib/mimi/db/lock/postgresql_lock.rb', line 27

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