Sha256: d21642691eccc1c4b14ee0f2e5c7383930dcfd50a5586b51fc2c90b6fb5e7e1c

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module RedisFailover
  # NodeWatcher periodically monitors a specific redis node for its availability.
  # NodeWatcher instances periodically report a redis node's current state
  # to the NodeManager for proper handling.
  class NodeWatcher
    include Util

    # Time to sleep before checking on the monitored node's status.
    WATCHER_SLEEP_TIME = 2

    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(@node, state)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redis_failover-0.7.0 lib/redis_failover/node_watcher.rb
redis_failover-0.6.0 lib/redis_failover/node_watcher.rb