Class: God::Conditions::SocketResponding

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

Overview

Condition Symbol :socket_running Type: Poll

Trigger when a TCP or UNIX socket is running or not

Parameters Required

+family+ is the family of socket: either 'tcp' or 'unix'
--one of port or path--
+port+ is the port (required if +family+ is 'tcp')
+path+ is the path (required if +family+ is 'unix')

Optional

+responding+ is the boolean specifying whether you want to trigger if the socket is responding (true)
             or if it is not responding (false) (default false)

Examples

Trigger if the TCP socket on port 80 is not responding or the connection is refused

on.condition(:socket_responding) do |c|

c.family = 'tcp'
c.port = '80'

end

Trigger if the socket is not responding or the connection is refused (use alternate compact socket interface)

on.condition(:socket_responding) do |c|

c.socket = 'tcp:80'

end

Trigger if the socket is responding

on.condition(:socket_responding) do |c|

c.socket = 'tcp:80'
c.responding = true

end

Trigger if the socket is not responding or the connection is refused 5 times in a row

on.condition(:socket_responding) do |c|

c.socket = 'tcp:80'
c.times = 5

end

Trigger if the Unix socket on path ‘/tmp/sock’ is not responding or non-existent

on.condition(:socket_responding) do |c|

c.family = 'unix'
c.path = '/tmp/sock'

end

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.



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

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.responding = false

  self.times = [1, 1]
end

Instance Attribute Details

#addrObject

Returns the value of attribute addr.



61
62
63
# File 'lib/god/conditions/socket_responding.rb', line 61

def addr
  @addr
end

#familyObject

Returns the value of attribute family.



61
62
63
# File 'lib/god/conditions/socket_responding.rb', line 61

def family
  @family
end

#pathObject

Returns the value of attribute path.



61
62
63
# File 'lib/god/conditions/socket_responding.rb', line 61

def path
  @path
end

#portObject

Returns the value of attribute port.



61
62
63
# File 'lib/god/conditions/socket_responding.rb', line 61

def port
  @port
end

#respondingObject

Returns the value of attribute responding.



61
62
63
# File 'lib/god/conditions/socket_responding.rb', line 61

def responding
  @responding
end

#timesObject

Returns the value of attribute times.



61
62
63
# File 'lib/god/conditions/socket_responding.rb', line 61

def times
  @times
end

Instance Method Details

#prepareObject



76
77
78
79
80
81
# File 'lib/god/conditions/socket_responding.rb', line 76

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

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

#resetObject



83
84
85
86
# File 'lib/god/conditions/socket_responding.rb', line 83

def reset
  @timeline.clear
  @history.clear
end

#socket=(socket) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/god/conditions/socket_responding.rb', line 88

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

#testObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/god/conditions/socket_responding.rb', line 110

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

#valid?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
# File 'lib/god/conditions/socket_responding.rb', line 102

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