Sha256: 80c87cc19c792dc9af34dca749276457c291a1a9152fd8e118fac66a1b1ca36b

Contents?: true

Size: 628 Bytes

Versions: 1

Compression:

Stored size: 628 Bytes

Contents

class RedisLock
  class Semaphore
    attr_reader :lock, :args

    def initialize(lock, args = {})
      @lock = lock
      @args = args
    end

    def call(&block)
      ttl = args[:ttl] || lock.config.default_ttl
      set_opts = args[:set_opts] || {}
      while lock.locked?
        sleep (args[:wait] || 1)
      end
      lock.set(ttl, set_opts)
      out = _perform(&block)
      lock.unlock!
      out
    end

    private

    def _perform(&block)
      yield lock
    rescue => e
      lock.config.logger.error "[#{self.class}] key: `#{key}` error:"
      lock.config.logger.error e
      raise e
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis_lock-0.4.1 lib/redis_lock/semaphore.rb