Sha256: 392b9206ce24fad0e7a80a76f1ec68a50fc50b9bdbd1261e58fbf0e4277f8aa5

Contents?: true

Size: 1.05 KB

Versions: 11

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

11 entries across 11 versions & 1 rubygems

Version Path
hermes_messenger_of_the_gods-2.4.0.pre2 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-2.4.0.pre1 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-2.3.4 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-2.3.3 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-2.3.2 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-2.3.1 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-2.3.0 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-2.2.0 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-2.1.1 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-2.1.0 lib/hermes_messenger_of_the_gods/status_server.rb
hermes_messenger_of_the_gods-2.0.0 lib/hermes_messenger_of_the_gods/status_server.rb