class Elos::Lock

Public Class Methods

lock(key, &block) click to toggle source
# 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) click to toggle source
# 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