Module: LitmusPaper::AgentCheckServer

Included in:
MultiPortAgentServer, SinglePortAgentServer
Defined in:
lib/litmus_paper/agent_check_server.rb

Constant Summary collapse

CRLF =
"\r\n".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#control_socketsObject (readonly)

Returns the value of attribute control_sockets.



11
12
13
# File 'lib/litmus_paper/agent_check_server.rb', line 11

def control_sockets
  @control_sockets
end

#pid_fileObject (readonly)

Returns the value of attribute pid_file.



11
12
13
# File 'lib/litmus_paper/agent_check_server.rb', line 11

def pid_file
  @pid_file
end

#workersObject (readonly)

Returns the value of attribute workers.



11
12
13
# File 'lib/litmus_paper/agent_check_server.rb', line 11

def workers
  @workers
end

Instance Method Details

#daemonize?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/litmus_paper/agent_check_server.rb', line 23

def daemonize?
  !!@daemonize
end

#initialize(litmus_paper_config, daemonize, pid_file, workers) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/litmus_paper/agent_check_server.rb', line 13

def initialize(litmus_paper_config, daemonize, pid_file, workers)
  LitmusPaper.configure(litmus_paper_config)
  @daemonize = daemonize
  @pid_file = pid_file
  @workers = workers

  trap(:INT) { exit }
  trap(:TERM) { exit }
end

#respond(sock, message) ⇒ Object



32
33
34
35
# File 'lib/litmus_paper/agent_check_server.rb', line 32

def respond(sock, message)
  sock.write(message)
  sock.write(CRLF)
end

#runObject



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
70
71
72
73
74
# File 'lib/litmus_paper/agent_check_server.rb', line 43

def run
  if daemonize?
    Process.daemon
  end
  write_pid(pid_file)
  child_pids = []

  workers.times do
    child_pids << spawn_child
  end

  kill_children = Proc.new { |signo|
    child_pids.each do |cpid|
      begin
        Process.kill(:INT, cpid)
      rescue Errno::ESRCH
      end
    end
    File.delete(pid_file) if File.exists?(pid_file)
    exit
  }

  trap(:INT, &kill_children)
  trap(:TERM, &kill_children)

  loop do
    pid = Process.wait
    LitmusPaper.logger.error("Process #{pid} quit unexpectedly")
    child_pids.delete(pid)
    child_pids << spawn_child
  end
end

#service_for_socket(socket) ⇒ Object



28
29
30
# File 'lib/litmus_paper/agent_check_server.rb', line 28

def service_for_socket(socket)
  raise "Consumers must implemented service_for_socket(socket)"
end

#spawn_childObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/litmus_paper/agent_check_server.rb', line 76

def spawn_child
  fork do
    Socket.accept_loop(control_sockets) do |sock, addr|
      _, remote_port, _, remote_ip = sock.peeraddr(:numeric)

      begin
        service = service_for_socket(sock)
        respond(sock, AgentCheckHandler.handle(service))
        sock.close
      rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ENOTCONN
        LitmusPaper.logger.debug "Received request from #{remote_ip}:#{remote_port}, but client hung up."
      end
    end
  end
end

#write_pid(pid_file) ⇒ Object



37
38
39
40
41
# File 'lib/litmus_paper/agent_check_server.rb', line 37

def write_pid(pid_file)
  File.open(pid_file, 'w') do |f|
    f.write(Process.pid)
  end
end