Class: HermesMessengerOfTheGods::WorkerStatusServer

Inherits:
Object
  • Object
show all
Defined in:
lib/hermes_messenger_of_the_gods/status_server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker:, port: nil) ⇒ WorkerStatusServer

Returns a new instance of WorkerStatusServer.



13
14
15
16
# File 'lib/hermes_messenger_of_the_gods/status_server.rb', line 13

def initialize(worker:, port: nil)
  self.worker = worker
  self.port = port || ENV.fetch('HERMES_WORKER_STATUS_PORT', 4242).to_i
end

Instance Attribute Details

#portObject

Returns the value of attribute port.



11
12
13
# File 'lib/hermes_messenger_of_the_gods/status_server.rb', line 11

def port
  @port
end

#workerObject

Returns the value of attribute worker.



11
12
13
# File 'lib/hermes_messenger_of_the_gods/status_server.rb', line 11

def worker
  @worker
end

Class Method Details

.start!(worker:, port: nil) ⇒ Object



7
8
9
# File 'lib/hermes_messenger_of_the_gods/status_server.rb', line 7

def self.start!(worker:, port: nil)
  new(worker: worker, port: port).start!
end

Instance Method Details

#construct_responseObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hermes_messenger_of_the_gods/status_server.rb', line 29

def construct_response
  if worker.healthy?
    resp = 'OKAY'
    status_code = '200 OK'
  else
    resp = 'ERROR'
    status_code = '500 Internal Server Error'
  end

  <<~RESPONSE
    HTTP/1.1 #{status_code}
    Date: #{Time.now.utc.strftime('%a, %d %b %Y %T GMT')}
    Server: Ruby
    Content-Type: text/html; charset=iso-8859-1
    Content-Length: #{resp.length}

    #{resp}
  RESPONSE
end

#start!Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/hermes_messenger_of_the_gods/status_server.rb', line 18

def start!
  Thread.new do
    server = TCPServer.open port
    loop do
      client = server.accept
      client.puts construct_response
      client&.close
    end
  end
end