Class: Wurk::Health::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/wurk/health.rb

Overview

The HTTP listener. Owns one TCPServer + one accept thread. Idempotent start/stop; safe to call from Launcher#run / Launcher#stop.

Constant Summary collapse

ACCEPT_TIMEOUT =
0.2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(launcher, port: DEFAULT_PORT, bind: DEFAULT_BIND, ready_window: DEFAULT_READY_WINDOW) ⇒ Server

Returns a new instance of Server.



34
35
36
37
38
39
40
41
42
43
# File 'lib/wurk/health.rb', line 34

def initialize(launcher, port: DEFAULT_PORT, bind: DEFAULT_BIND, ready_window: DEFAULT_READY_WINDOW)
  @launcher     = launcher
  @config       = launcher.instance_variable_get(:@config)
  @port         = port
  @bind         = bind
  @ready_window = ready_window
  @server       = nil
  @thread       = nil
  @done         = false
end

Instance Attribute Details

#bindObject (readonly)

Returns the value of attribute bind.



32
33
34
# File 'lib/wurk/health.rb', line 32

def bind
  @bind
end

#portObject (readonly)

Returns the value of attribute port.



32
33
34
# File 'lib/wurk/health.rb', line 32

def port
  @port
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/wurk/health.rb', line 72

def running?
  @thread&.alive? == true
end

#startObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wurk/health.rb', line 45

def start
  @server = ::TCPServer.new(@bind, @port)
  # Capture the OS-assigned port when caller passed 0 (test pattern,
  # also lets the kernel pick a free port at boot).
  @port = @server.addr[1]
  @done = false
  @thread = ::Thread.new { run }
  @thread.name = 'wurk-health'
  self
rescue ::Errno::EADDRINUSE => e
  # Swarm children all try to bind the same port — only the first wins.
  # Don't crash the worker; just log and skip.
  logger&.warn { "Wurk::Health: port #{@port} in use; health server NOT started (#{e.message})" }
  @server = nil
  @thread = nil
  self
end

#stopObject



63
64
65
66
67
68
69
70
# File 'lib/wurk/health.rb', line 63

def stop
  @done = true
  srv = @server
  @server = nil
  srv&.close
  @thread&.join(2)
  @thread = nil
end