Sha256: 1c8c33ea88a85c9bc1458d26479a9d1aeb5d125bca91c27c602fb42a61037c8d

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

# vim:fileencoding=utf-8

module Resque
  module 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

        # Releases the lock iff we own it
        def release
          locked? && release!
        end

        private

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

        def hostname
          local_hostname = Socket.gethostname
          Addrinfo.getaddrinfo(local_hostname, 'http').first.getnameinfo.first
        rescue
          local_hostname
        end

        def process_id
          Process.pid
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
resque-scheduler-4.10.2 lib/resque/scheduler/lock/base.rb
resque-scheduler-4.10.1 lib/resque/scheduler/lock/base.rb
resque-scheduler-4.10.0 lib/resque/scheduler/lock/base.rb
resque-scheduler-4.9.0 lib/resque/scheduler/lock/base.rb
resque-scheduler-4.8.0 lib/resque/scheduler/lock/base.rb