Class: Exekutor::Internal::StatusServer

Inherits:
Object
  • Object
show all
Includes:
Executable, Logger
Defined in:
lib/exekutor/internal/status_server.rb

Overview

Serves a simple health check app. The app provides 4 endpoints:

  • /, which lists the other endpoints;

  • /ready, which indicates whether the worker is ready to start work;

  • /live, which indicates whether the worker is ready and whether the worker is still alive;

  • /threads, which indicated the thread usage of the worker.

Please note that this server uses webrick by default, which is no longer a default gem from ruby 3.0 onwards.

Example requests

$ curl localhost:9000/ready
[OK] ID: f1a2ee6a-cdac-459c-a4b8-de7c6a8bbae6; State: started
$ curl localhost:9000/live
[OK] ID: f1a2ee6a-cdac-459c-a4b8-de7c6a8bbae6; State: started; Heartbeat: 2023-04-05T16:27:00Z
$ curl localhost:9000/threads
{"minimum":1,"maximum":10,"available":4,"usage_percent":60.0}

Defined Under Namespace

Classes: App

Constant Summary collapse

DEFAULT_HANDLER =
"webrick"

Constants included from Executable

Executable::STATES

Instance Method Summary collapse

Methods included from Executable

#consecutive_errors, #restart_delay, #state

Constructor Details

#initialize(worker:, pool:, port:, handler: DEFAULT_HANDLER, heartbeat_timeout: 30.minutes) ⇒ StatusServer

Returns a new instance of StatusServer.



26
27
28
29
30
31
32
33
34
35
# File 'lib/exekutor/internal/status_server.rb', line 26

def initialize(worker:, pool:, port:, handler: DEFAULT_HANDLER, heartbeat_timeout: 30.minutes)
  super()
  @worker = worker
  @pool = pool
  @port = port
  @handler = Rack::Handler.get(handler)
  @heartbeat_timeout = heartbeat_timeout
  @thread_running = Concurrent::AtomicBoolean.new false
  @server = Concurrent::AtomicReference.new
end

Instance Method Details

#running?Boolean

Returns whether the web server is active.

Returns:

  • (Boolean)

    whether the web server is active



45
46
47
# File 'lib/exekutor/internal/status_server.rb', line 45

def running?
  super && @thread_running.value
end

#startObject

Starts the web server



38
39
40
41
42
# File 'lib/exekutor/internal/status_server.rb', line 38

def start
  return false unless compare_and_set_state :pending, :started

  start_thread
end

#stopObject

Stops the web server



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/exekutor/internal/status_server.rb', line 50

def stop
  self.state = :stopped
  return unless @thread_running.value

  server = @server.value
  shutdown_method = %i[shutdown stop].find { |method| server.respond_to? method }
  if shutdown_method
    server.send(shutdown_method)
    Exekutor.say "Status server stopped"
  elsif server
    Exekutor.print_error "Cannot shutdown status server, " \
                         "#{server.class.name} does not respond to `shutdown` or `stop`"
  end
end