Sha256: 8275a32589bc219da7ae8a3520418647fd2d20a2b8dbb570dca835defeb71f96

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module AnyCable
  # Server for HTTP healthchecks.
  #
  # Basic usage:
  #
  #  # create a new healthcheck server for a specified
  #  # gRPC server lisening on the port
  #  health_server = AnyCable::HealthServer.new(grpc_server, port)
  #
  #  # start health server in background
  #  health_server.start
  #
  #  # stop health server
  #  health_server.stop
  class HealthServer
    SUCCESS_RESPONSE = [200, "Ready"].freeze
    FAILURE_RESPONSE = [503, "Not Ready"].freeze

    attr_reader :grpc_server, :port, :path, :server

    def initialize(grpc_server, port:, path: "/health", logger: AnyCable.logger)
      @grpc_server = grpc_server
      @port = port
      @path = path
      @logger = logger
      @server = build_server
    end

    def start
      return if running?

      Thread.new { server.start }

      logger.info "HTTP health server is listening on localhost:#{port} and mounted at \"#{path}\""
    end

    def stop
      return unless running?

      server.shutdown
    end

    def running?
      server.status == :Running
    end

    private

    attr_reader :logger

    def build_server
      require "webrick"

      WEBrick::HTTPServer.new(
        Port: port,
        Logger: logger,
        AccessLog: []
      ).tap do |server|
        server.mount_proc path do |_, res|
          res.status, res.body = grpc_server.running? ? SUCCESS_RESPONSE : FAILURE_RESPONSE
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
anycable-1.0.0.preview2 lib/anycable/health_server.rb
anycable-0.6.5 lib/anycable/health_server.rb
anycable-1.0.0.preview1 lib/anycable/health_server.rb
anycable-0.6.4 lib/anycable/health_server.rb