Sha256: 6389ce3c674eb97871d23d327d77df112d74a0dbe29849186c2a08af32731754

Contents?: true

Size: 551 Bytes

Versions: 20

Compression:

Stored size: 551 Bytes

Contents

require 'diplomat'

module Shipitron
  module ConsulLock
    extend self

    class UnableToLock < StandardError; end

    def with_lock(key:)
      sessionid = nil
      locked = false
      sessionid = Diplomat::Session.create(Name: "#{key}.lock")
      locked = Diplomat::Lock.acquire(key, sessionid)

      if locked
        yield
      else
        raise UnableToLock
      end
    ensure
      if sessionid != nil
        Diplomat::Lock.release(key, sessionid) if locked
        Diplomat::Session.destroy(sessionid)
      end
    end

  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
shipitron-1.4.1 lib/shipitron/consul_lock.rb
shipitron-1.4.0 lib/shipitron/consul_lock.rb
shipitron-1.3.2 lib/shipitron/consul_lock.rb
shipitron-1.3.1 lib/shipitron/consul_lock.rb
shipitron-1.3.0 lib/shipitron/consul_lock.rb
shipitron-1.2.1 lib/shipitron/consul_lock.rb
shipitron-1.2.0 lib/shipitron/consul_lock.rb
shipitron-1.1.0 lib/shipitron/consul_lock.rb
shipitron-1.0.1 lib/shipitron/consul_lock.rb
shipitron-1.0.0 lib/shipitron/consul_lock.rb
shipitron-0.4.1 lib/shipitron/consul_lock.rb
shipitron-0.4.0 lib/shipitron/consul_lock.rb
shipitron-0.3.5 lib/shipitron/consul_lock.rb
shipitron-0.3.4 lib/shipitron/consul_lock.rb
shipitron-0.3.3 lib/shipitron/consul_lock.rb
shipitron-0.3.1 lib/shipitron/consul_lock.rb
shipitron-0.3.0 lib/shipitron/consul_lock.rb
shipitron-0.2.2 lib/shipitron/consul_lock.rb
shipitron-0.2.1 lib/shipitron/consul_lock.rb
shipitron-0.2.0 lib/shipitron/consul_lock.rb