Sha256: 2fb92240cec0b98bd821c11731e1ad4aa5fbe6ae98cc96ca49c5ef880172d0de

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

module Resque
  class Scheduler
    module Lock
      class Base
        attr_reader :key
        attr_accessor :timeout

        def initialize(key, options = {})
          @key = key

          # 3 minute default timeout
          @timeout = options[:timeout] || 60 * 3
        end

        # Attempts to acquire the lock. Returns true if successfully acquired.
        def acquire!
          raise NotImplementedError
        end

        def value
          @value ||= [hostname, process_id].join(':')
        end

        # Returns true if you currently hold the lock.
        def locked?
          raise NotImplementedError
        end

        # Releases the lock.
        def release!
          Resque.redis.del(key) == 1
        end

      private

        # Extends the lock by `timeout` seconds.
        def extend_lock!
          Resque.redis.expire(key, timeout)
        end

        def hostname
          local_hostname = Socket.gethostname
          Socket.gethostbyname(local_hostname).first rescue local_hostname
        end

        def process_id
          Process.pid
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
resque-scheduler-2.2.0 lib/resque/scheduler/lock/base.rb
resque-scheduler-2.1.1 lib/resque/scheduler/lock/base.rb
resque-scheduler-2.1.0 lib/resque/scheduler/lock/base.rb
resque-scheduler-2.0.1 lib/resque/scheduler/lock/base.rb