class Hookit::Resource::Socket

Public Class Methods

new(name) click to toggle source
Calls superclass method Hookit::Resource::Base::new
# File lib/hookit/resource/socket.rb, line 13
def initialize(name)
  service name unless service
  max_checks 3 unless max_checks
  super

  case platform.os
  when 'sun'
    @active_com   = "netstat -an | egrep '\*\.#{port}' | grep LISTEN"
    @inactive_com = "netstat -an | grep 'ESTABLISHED' | awk '{ print $1 }' | grep \"$(ifconfig #{interface} | grep inet | awk '{ print $2 }')\.#{port}\""
  else
    @active_com   = "netstat -an | egrep ':#{port}' | grep LISTEN"
    @inactive_com = "netstat -an | grep 'ESTABLISHED' | awk '{ print $4 }' | grep \"$(ifconfig #{interface} | awk '/inet addr/ { print $2}' | cut -f2 -d':' | tr -d '\n'):#{port}\""
  end
end

Public Instance Methods

run(action) click to toggle source
# File lib/hookit/resource/socket.rb, line 28
def run(action)
  case action
  when :listening
    check_listening!
  when :no_connections
    check_no_connections!
  when :reset
    reset!
  end
end

Protected Instance Methods

check_listening!() click to toggle source
# File lib/hookit/resource/socket.rb, line 41
def check_listening!
  # increment check
  registry("#{service}.listening", registry("#{service}.listening").to_i + 1)

  if `#{@active_com}`.empty?
    count = registry("#{service}.listening").to_i
    if count <= max_checks
      sleep 1
      exit(count + 10)
    else
      $stderr.puts "ERROR: timed out waiting for #{service} to listen"
      exit(Hookit::Exit::ERROR)
    end
  end

end
check_no_connections!() click to toggle source
# File lib/hookit/resource/socket.rb, line 58
def check_no_connections!
  # increment check
  registry("#{service}.no_connections", registry("#{service}.no_connections").to_i + 1)

  unless `#{@inactive_com}`.empty?
    count = registry("#{service}.no_connections").to_i
    sleep 1
    if count <= max_checks
      exit(count + 10)
    end
  end
end
reset!() click to toggle source
# File lib/hookit/resource/socket.rb, line 71
def reset!
  registry("#{service}.listening", 0)
  registry("#{service}.no_connections", 0)
end