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
RETRY_INTERVAL =

How often a non-owner child re-attempts the shared port. Short enough that probes come back quickly after the owner dies, long enough not to spin. See #start_retry_loop.

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Server.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wurk/health.rb', line 38

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

Instance Attribute Details

#bindObject (readonly)

Returns the value of attribute bind.



36
37
38
# File 'lib/wurk/health.rb', line 36

def bind
  @bind
end

#portObject (readonly)

Returns the value of attribute port.



36
37
38
# File 'lib/wurk/health.rb', line 36

def port
  @port
end

Instance Method Details

#retrying?Boolean

True while a non-owner child is still polling to take the shared port over (see #start_retry_loop). Distinct from #running?, which reports the accept thread specifically.

Returns:

  • (Boolean)


80
81
82
# File 'lib/wurk/health.rb', line 80

def retrying?
  @retry_thread&.alive? == true
end

#running?Boolean

Returns:

  • (Boolean)


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

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

#startObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/wurk/health.rb', line 52

def start
  # Idempotent (see class doc): a second start on a live instance would
  # re-bind the same port, hit EADDRINUSE, and null out @server/@thread —
  # leaking the original listener so stop could never close it.
  return self if running? || retrying?

  @done = false
  bind_and_serve || start_retry_loop
  self
end

#stopObject



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

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