Class: Eye::Checker::Socket

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

Direct Known Subclasses

SslSocket

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 = Regexp.last_match(1)
    @socket_port = Regexp.last_match(2).to_i
  elsif addr =~ %r[\Aunix:(.*)\z]
    @socket_family = :unix
    @socket_path = Regexp.last_match(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
# 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

  return { result: :listen } unless send_data

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

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

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

ensure
  sock.close if sock
end

#good?(value) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
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
# File 'lib/eye/checker/socket.rb', line 66

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

  return true unless expect_data

  if expect_data.is_a?(Proc)
    match = begin
      !!expect_data[value[:result]]
    rescue Object => 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].to_s[0..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].to_s[0..30]}) answer"
  value[:notice] = "missing '#{expect_data}'"

  false
end

#human_value(value) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/eye/checker/socket.rb', line 98

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