Sha256: 2aa13ed98566af180a8ad6b4e9892cfd8ef0345410ea1519c234746674cc578b

Contents?: true

Size: 1.05 KB

Versions: 8

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

require 'socket'

module HermesMessengerOfTheGods
  class WorkerStatusServer
    def self.start!(worker:, port: nil)
      new(worker: worker, port: port).start!
    end

    attr_accessor :worker, :port

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

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

    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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hermes_messenger_of_the_gods-3.0.0 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-3.0.0.rc5 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-3.0.0.rc4 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-3.0.0.rc3 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-3.0.0.rc2 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-3.0.0.rc1 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-2.4.2 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-2.4.1 lib/hermes_messenger_of_the_gods/status_server.rb