Class: God::Conditions::SocketResponding

Inherits:
PollCondition show all
Defined in:
lib/god/conditions/socket_responding.rb

Instance Attribute Summary collapse

Attributes inherited from PollCondition

#interval

Attributes inherited from God::Condition

#info, #notify, #phase, #transition

Attributes inherited from Behavior

#watch

Instance Method Summary collapse

Methods inherited from PollCondition

#after, #before

Methods inherited from God::Condition

#friendly_name, generate, valid?

Methods inherited from Behavior

#after_restart, #after_start, #after_stop, #before_restart, #before_start, #before_stop, #friendly_name, generate

Methods included from God::Configurable

#base_name, complain, #complain, #friendly_name

Constructor Details

#initializeSocketResponding

Returns a new instance of SocketResponding.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/god/conditions/socket_responding.rb', line 52

def initialize
  super
  # default to tcp on the localhost
  self.family = 'tcp'
  self.addr = '127.0.0.1'
  # Set these to nil/0 values
  self.port = 0
  self.path = nil

  self.times = [1, 1]
end

Instance Attribute Details

#addrObject

Returns the value of attribute addr.



50
51
52
# File 'lib/god/conditions/socket_responding.rb', line 50

def addr
  @addr
end

#familyObject

Returns the value of attribute family.



50
51
52
# File 'lib/god/conditions/socket_responding.rb', line 50

def family
  @family
end

#pathObject

Returns the value of attribute path.



50
51
52
# File 'lib/god/conditions/socket_responding.rb', line 50

def path
  @path
end

#portObject

Returns the value of attribute port.



50
51
52
# File 'lib/god/conditions/socket_responding.rb', line 50

def port
  @port
end

#timesObject

Returns the value of attribute times.



50
51
52
# File 'lib/god/conditions/socket_responding.rb', line 50

def times
  @times
end

Instance Method Details

#prepareObject



64
65
66
67
68
69
70
71
# File 'lib/god/conditions/socket_responding.rb', line 64

def prepare
  if self.times.kind_of?(Integer)
    self.times = [self.times, self.times]
  end

  @timeline = Timeline.new(self.times[1])
  @history = Timeline.new(self.times[1])
end

#resetObject



73
74
75
76
# File 'lib/god/conditions/socket_responding.rb', line 73

def reset
  @timeline.clear
  @history.clear
end

#socket=(s) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/god/conditions/socket_responding.rb', line 78

def socket=(s)
  components = s.split(':')
  if components.size == 3
    @family,@addr,@port = components
    @port = @port.to_i
  elsif components[0] =~ /^tcp$/
    @family = components[0]
    @port = components[1].to_i
  elsif components[0] =~ /^unix$/
    @family = components[0]
    @path = components[1]
  end
end

#testObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/god/conditions/socket_responding.rb', line 104

def test
  self.info = []
  if self.family == 'tcp'
    begin
      s = TCPSocket.new(self.addr, self.port)
    rescue SystemCallError
    end
    status = s.nil?
  elsif self.family == 'unix'
    begin
      s = UNIXSocket.new(self.path)
    rescue SystemCallError
    end
    status = s.nil?
  else
    status = false
  end
  @timeline.push(status)
  history = "[" + @timeline.map {|t| t ? '*' : ''}.join(',') + "]"
  if @timeline.select { |x| x }.size >= self.times.first
    self.info = "socket out of bounds #{history}"
    return true
  else
    return false
  end
end

#valid?Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
# File 'lib/god/conditions/socket_responding.rb', line 92

def valid?
  valid = true
  if self.family == 'tcp' and @port == 0
    valid &= complain("Attribute 'port' must be specified for tcp sockets", self)
  end
  if self.family == 'unix' and self.path.nil?
    valid &= complain("Attribute 'path' must be specified for unix sockets", self)
  end
  valid = false unless %w{tcp unix}.member?(self.family)
  valid
end