Sha256: 88bd4b71f5abf8aaebe50dca6ae2e0a3b3fe8fc3d2025c102dbff7bfad2bd180

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

module RedisFailover
  # Watches a specific redis node for its availability.
  class NodeWatcher
    include Util

    WATCHER_SLEEP_TIME = 3

    def initialize(manager, node, max_failures)
      @manager = manager
      @node = node
      @max_failures = max_failures
      @monitor_thread = nil
      @done = false
    end

    def watch
      @monitor_thread = Thread.new { monitor_node }
      self
    end

    def shutdown
      @done = true
      @node.wakeup
      @monitor_thread.join if @monitor_thread
    rescue
      # best effort
    end

    private

    def monitor_node
      failures = 0

      loop do
        begin
          return if @done
          sleep(WATCHER_SLEEP_TIME)
          @node.ping
          failures = 0

          if @node.syncing_with_master?
            notify(:syncing)
          else
            notify(:available)
            @node.wait
          end
        rescue NodeUnavailableError
          failures += 1
          if failures >= @max_failures
            notify(:unavailable)
            failures = 0
          end
        end
      end
    end

    def notify(state)
      @manager.notify_state_change(@node, state)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
redis_failover-0.5.2 lib/redis_failover/node_watcher.rb
redis_failover-0.5.1 lib/redis_failover/node_watcher.rb
redis_failover-0.5.0 lib/redis_failover/node_watcher.rb
redis_failover-0.4.0 lib/redis_failover/node_watcher.rb