Class: KicksLiveness::Monitor Private

Inherits:
Object
  • Object
show all
Defined in:
lib/kicks_liveness/monitor.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The thread that publishes the liveness mark. Created inside the fork, because threads do not survive fork.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(slot:, processes:, consumers:, config:, heartbeat: nil) ⇒ Monitor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Monitor.

Parameters:

  • slot (Integer)

    supervisor slot of this fork; names the mark file

  • processes (Integer)

    how many forks the probe must wait for

  • consumers (Integer)

    how many workers must subscribe in this process

  • config (Configuration)
  • heartbeat (Heartbeat, nil) (defaults to: nil)

    injected in specs; Attempts follows its directory, so injecting it separately is not needed



14
15
16
17
18
19
20
21
# File 'lib/kicks_liveness/monitor.rb', line 14

def initialize(slot:, processes:, consumers:, config:, heartbeat: nil)
  @slot = slot
  @processes = processes
  @consumers = consumers
  @config = config
  @heartbeat = heartbeat || Heartbeat.new
  @attempts = Attempts.new(@heartbeat.dir)
end

Instance Method Details

#start!Thread

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Declares the expected fork count and starts the tick thread.

Returns:

  • (Thread)


25
26
27
28
29
30
31
32
33
34
# File 'lib/kicks_liveness/monitor.rb', line 25

def start!
  attempt, elapsed = @attempts.record!(@slot)
  @heartbeat.declare!(@processes)
  attempt > 1 ? report_respawn(attempt, elapsed) : report_first_start

  Thread.new do
    Thread.current.name = 'kicks-liveness'
    run_loop
  end
end

#tick!Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A single step: check, mark if healthy, log any transition. Public so that specs do not have to drive the thread.

Returns:

  • (Boolean)

    whether the process is healthy at this moment



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kicks_liveness/monitor.rb', line 40

def tick!
  # Re-declared on every tick, not only at startup. This file is the probe's
  # only source for how many forks to expect, and nothing else restores it:
  # if the directory is wiped, `touch!` brings back the slot marks while
  # `expected` stays missing, and the probe reports "worker has not started"
  # for the rest of the pod's life. It also lets a respawned set of forks
  # correct the count after the supervisor was told to run fewer of them.
  @heartbeat.declare!(@processes)

  if Registry.stopping?
    shutdown_tick!
    return true
  end

  healthy = Registry.healthy?(@consumers)
  if healthy
    @heartbeat.touch!(@slot)
    clear_attempt_once!
  end
  @unhealthy_ticks = healthy ? 0 : unhealthy_ticks + 1
  report(healthy)
  healthy
end