Sha256: 20d18554a83f2380deee247e14637783eda6a4eafa51425db4840e4abc671a7b

Contents?: true

Size: 1.47 KB

Versions: 12

Compression:

Stored size: 1.47 KB

Contents

require 'csv'

module LitmusPaper
  module Dependency
    class HaproxyBackends
      def initialize(domain_socket, cluster, options = {})
        @domain_socket = domain_socket
        @cluster = cluster
        @timeout = options.fetch(:timeout_seconds, 2)
      end

      def available?
        stats = _parse_stats(_fetch_stats)
        backend = _find_backend(stats, @cluster)

        if backend['status'] != 'UP'
          LitmusPaper.logger.info("HAProxy available check failed, #{@cluster} backend is #{backend['status']}")
          return false
        end
        return true
      rescue Timeout::Error
        LitmusPaper.logger.info("HAProxy available check timed out for #{@cluster}")
        false
      rescue => e
        LitmusPaper.logger.info("HAProxy available check failed for #{@cluster} with #{e.message}")
        false
      end

      def to_s
        "Dependency::HaproxyBackends(#{@domain_socket}, #{@cluster})"
      end

      def _find_backend(stats, cluster)
        stats.detect do |line|
          line['# pxname'] == cluster && line['svname'] == 'BACKEND'
        end
      end

      def _parse_stats(csv)
        stats = CSV.parse(csv)
        headers = stats.shift
        stats.map { |stat| Hash[headers.zip(stat)] }
      end

      def _fetch_stats
        Timeout.timeout(@timeout) do
          UNIXSocket.open(@domain_socket) do |socket|
            socket.send("show stat\n", 0)
            socket.read
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
litmus_paper-0.9.7 lib/litmus_paper/dependency/haproxy_backends.rb
litmus_paper-0.9.6 lib/litmus_paper/dependency/haproxy_backends.rb
litmus_paper-0.9.5 lib/litmus_paper/dependency/haproxy_backends.rb
litmus_paper-0.9.4 lib/litmus_paper/dependency/haproxy_backends.rb
litmus_paper-0.9.3 lib/litmus_paper/dependency/haproxy_backends.rb
litmus_paper-0.9.2 lib/litmus_paper/dependency/haproxy_backends.rb
litmus_paper-0.9.1 lib/litmus_paper/dependency/haproxy_backends.rb
litmus_paper-0.9.0 lib/litmus_paper/dependency/haproxy_backends.rb
litmus_paper-0.8.9 lib/litmus_paper/dependency/haproxy_backends.rb
litmus_paper-0.8.8 lib/litmus_paper/dependency/haproxy_backends.rb
litmus_paper-0.8.7 lib/litmus_paper/dependency/haproxy_backends.rb
litmus_paper-0.8.6 lib/litmus_paper/dependency/haproxy_backends.rb