Class: Eye::Checker::Socket

Inherits:
Defer show all
Defined in:
lib/eye/checker/socket.rb

Constant Summary

Constants inherited from Eye::Checker

TYPES

Instance Attribute Summary

Attributes inherited from Eye::Checker

#check_count, #options, #pid, #process, #type, #value, #values

Instance Method Summary collapse

Methods inherited from Defer

#get_value_safe

Methods inherited from Eye::Checker

#check, #check_name, create, #defer, #fire, get_class, #get_value_safe, #inspect, #last_human_values, #logger_sub_tag, #logger_tag, #max_tries, #min_tries, name_and_class, #previous_value, register, requires, #run_in_process_context, validate!

Methods included from Dsl::Validation

included

Constructor Details

#initialize(*args) ⇒ Socket

Returns a new instance of Socket.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/eye/checker/socket.rb', line 23

def initialize(*args)
  super
  @open_timeout = (open_timeout || 1).to_f
  @read_timeout = (read_timeout || timeout || 5).to_f

  if addr =~ %r[\Atcp://(.*?):(.*?)\z]
    @socket_family = :tcp
    @socket_addr = $1
    @socket_port = $2.to_i
  elsif addr =~ %r[\Aunix:(.*)\z]
    @socket_family = :unix
    @socket_path = $1
  end
end

Instance Method Details

#get_valueObject


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eye/checker/socket.rb', line 38

def get_value
  sock = begin
    Timeout::timeout(@open_timeout){ open_socket }
  rescue Timeout::Error
    return { :exception => "OpenTimeout<#{@open_timeout}>" }
  end

  if send_data
    begin
      Timeout::timeout(@read_timeout) do
        _write_data(sock, send_data)
        result = _read_data(sock)

        { :result => result }
      end
    rescue Timeout::Error
      if protocol == :raw
        return { :result => @buffer }
      else
        return { :exception => "ReadTimeout<#{@read_timeout}>" }
      end
    end
  else
    { :result => :listen }
  end

rescue Exception => e
  { :exception => "Error<#{e.message}>" }

ensure
  sock.close if sock
end

#good?(value) ⇒ Boolean

Returns:

  • (Boolean)

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/eye/checker/socket.rb', line 71

def good?(value)
  return false if !value[:result]

  if expect_data
    if expect_data.is_a?(Proc)
      match = begin
        !!expect_data[value[:result]]
      rescue Timeout::Error, Exception => ex
        mes = "proc match failed with '#{ex.message}'"
        error(mes)
        value[:notice] = mes
        return false
      end

      unless match
        warn "proc #{expect_data} not matched (#{value[:result].truncate(30)}) answer"
        value[:notice] = 'missing proc validation'
      end

      return match
    end

    return true if expect_data.is_a?(Regexp) && expect_data.match(value[:result])
    return true if value[:result].to_s == expect_data.to_s

    warn "#{expect_data} not matched (#{value[:result].truncate(30)}) answer"
    value[:notice] = "missing '#{expect_data.to_s}'"
    return false
  end

  return true
end

#human_value(value) ⇒ Object


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/eye/checker/socket.rb', line 104

def human_value(value)
  if !value.is_a?(Hash)
    '-'
  elsif value[:exception]
    value[:exception]
  else
    if value[:result] == :listen
      'listen'
    else
      res = "#{value[:result].to_s.size}b"
      res += "<#{value[:notice]}>" if value[:notice]
      res
    end
  end
end