Module: Tina4::Health

Defined in:
lib/tina4/health.rb

Constant Summary collapse

START_TIME =
Process.clock_gettime(Process::CLOCK_MONOTONIC)

Class Method Summary collapse

Class Method Details

.handle(_request, response) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tina4/health.rb', line 35

def handle(_request, response)
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  uptime = (now - START_TIME).round(2)

  payload = {
    status: "ok",
    version: Tina4::VERSION,
    uptime: uptime,
    framework: "tina4-ruby"
  }

  response.json(payload)
end

.pathObject

Return the configured health endpoint path. TINA4_HEALTH_PATH overrides the default "/__health" — kept consistent across all 4 frameworks.



12
13
14
15
16
# File 'lib/tina4/health.rb', line 12

def path
  configured = ENV["TINA4_HEALTH_PATH"]
  return "/__health" if configured.nil? || configured.empty?
  configured.start_with?("/") ? configured : "/#{configured}"
end

.register!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tina4/health.rb', line 18

def register!
  # Idempotent by ASKING THE ROUTER, not by remembering. run! and the CLI
  # both call this, and it can run more than once per process. A boolean
  # flag looked equivalent and was not: Router.clear! (every spec, and
  # any re-init) wipes the routes while the flag stays true, so /health
  # silently vanishes for the rest of the run. Querying the router is
  # self-healing -- cleared routes re-register, present ones don't
  # duplicate.
  return if Tina4::Router.find_route("/health", "GET")

  # Register at the configured path. The legacy "/health" path stays
  # registered for backward-compat.
  Tina4::Router.add("GET", path, method(:handle))
  Tina4::Router.add("GET", "/health", method(:handle)) unless path == "/health"
end

.statusObject



49
50
51
52
53
54
55
56
57
# File 'lib/tina4/health.rb', line 49

def status
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  {
    status: "ok",
    version: Tina4::VERSION,
    uptime: (now - START_TIME).round(2),
    framework: "tina4-ruby"
  }
end