Class: Mimi::DB::Lock::SqliteLock

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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
26
27
28
29
# File 'lib/mimi/db/lock/sqlite_lock.rb', line 13

def initialize(name, opts = {})
  @name = name
  @name_digest = Digest::SHA1.hexdigest(name).first(16)
  @options = opts
  @timeout =
    if opts[:timeout].nil?
      -1
    elsif opts[:timeout] <= 0
      0.100
    else
      opts[:timeout].to_f.round
    end
  db_filename = Pathname.new(Mimi::DB.module_options[:db_database]).expand_path
  @lock_filename = "#{db_filename}.lock-#{name_digest}"
  @lock_acquired = nil
  @file = nil
end

Instance Attribute Details

#lock_filenameObject (readonly)

Returns the value of attribute lock_filename.



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

def lock_filename
  @lock_filename
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#name_digestObject (readonly)

Returns the value of attribute name_digest.



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

def name_digest
  @name_digest
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Instance Method Details

#execute(&_block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/mimi/db/lock/sqlite_lock.rb', line 31

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