Class: Elos::Lock

Inherits:
Object
  • Object
show all
Includes:
Index::Attributes, Index::Core, Index::Mappings, Index::Properties
Defined in:
lib/elos/lock.rb

Class Method Summary collapse

Class Method Details

.lock(key, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/elos/lock.rb', line 15

def self.lock(key, &block)
  create_index(index_name)
  id = SecureRandom.uuid
  body = { doc: {}, upsert: { lock: id } }
  client.update(index: index_name, type: type_name, id: key, body: body)
  if client.get(index: index_name, type: type_name, id: key)['_source']['lock'] == id
    if block
      begin
        ret = block.()
      ensure
        unlock(key)
      end
    else
      true
    end
  end
end

.unlock(key) ⇒ Object



33
34
35
36
# File 'lib/elos/lock.rb', line 33

def self.unlock(key)
  client.delete(index: index_name, type: type_name, id: key)
rescue Elasticsearch::Transport::Transport::Errors::NotFound
end