Sha256: 6f8cbb4afccb8f22f682476045f330a9b48f9e9b789808b1e017ee3b9b61d00d

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 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

          # 10 seconds default timeout
          @timeout = options[:timeout] || 10
        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
          Socket.gethostbyname(local_hostname).first
        rescue
          local_hostname
        end

        def process_id
          Process.pid
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
istox-resque-scheduler-1.0.0.pre lib/resque/scheduler/lock/base.rb