Class: AnyCable::HealthServer

Inherits:
Object
  • Object
show all
Defined in:
lib/anycable/health_server.rb

Overview

Server for HTTP healthchecks.

Basic usage:

# create a new healthcheck server for a specified
# server listening on the port
health_server = AnyCable::HealthServer.new(server, port)

# start health server in background
health_server.start

# stop health server
health_server.stop

Constant Summary collapse

SUCCESS_RESPONSE =
[200, "Ready"].freeze
FAILURE_RESPONSE =
[503, "Not Ready"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, port:, logger: nil, path: "/health") ⇒ HealthServer

Returns a new instance of HealthServer.



23
24
25
26
27
28
29
# File 'lib/anycable/health_server.rb', line 23

def initialize(server, port:, logger: nil, path: "/health")
  @server = server
  @port = port
  @path = path
  @logger = logger
  @http_server = build_server
end

Instance Attribute Details

#http_serverObject (readonly)

Returns the value of attribute http_server.



21
22
23
# File 'lib/anycable/health_server.rb', line 21

def http_server
  @http_server
end

#pathObject (readonly)

Returns the value of attribute path.



21
22
23
# File 'lib/anycable/health_server.rb', line 21

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



21
22
23
# File 'lib/anycable/health_server.rb', line 21

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



21
22
23
# File 'lib/anycable/health_server.rb', line 21

def server
  @server
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/anycable/health_server.rb', line 45

def running?
  http_server.status == :Running
end

#startObject



31
32
33
34
35
36
37
# File 'lib/anycable/health_server.rb', line 31

def start
  return if running?

  Thread.new { http_server.start }

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

#stopObject



39
40
41
42
43
# File 'lib/anycable/health_server.rb', line 39

def stop
  return unless running?

  http_server.shutdown
end