Sha256: 9e84bc061f7ac8c539462e7cebfcf2488ee70cf21863c5b8e8da29c304865001

Contents?: true

Size: 1.1 KB

Versions: 9

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!
          fail NotImplementedError
        end

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

        # Returns true if you currently hold the lock.
        def locked?
          fail 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

9 entries across 9 versions & 1 rubygems

Version Path
resque-scheduler-2.5.5 lib/resque/scheduler/lock/base.rb
resque-scheduler-2.5.4 lib/resque/scheduler/lock/base.rb
resque-scheduler-2.5.3 lib/resque/scheduler/lock/base.rb
resque-scheduler-2.5.2 lib/resque/scheduler/lock/base.rb
resque-scheduler-2.5.1 lib/resque/scheduler/lock/base.rb
resque-scheduler-2.5.0 lib/resque/scheduler/lock/base.rb
resque-scheduler-2.4.0 lib/resque/scheduler/lock/base.rb
resque-scheduler-2.3.1 lib/resque/scheduler/lock/base.rb
resque-scheduler-2.3.0 lib/resque/scheduler/lock/base.rb