Class: KicksLiveness::Attempts Private

Inherits:
Object
  • Object
show all
Defined in:
lib/kicks_liveness/attempts.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.

Counts starts of a supervisor slot that have not reached healthy yet.

Deliberately separate from Heartbeat. Heartbeat is the contract shared with the probe and is loaded by it alone, so nothing the probe does not need belongs there — including the fileutils this class requires freely.

The count lives in the marks directory rather than in memory because a respawn storm destroys process state every few hundred milliseconds: the supervisor brings a failing fork back after start_worker_delay, and every incarnation of the monitor believes it is the first. That is how an outage can produce thousands of identical INFO lines and not one ERROR. The directory is the only state that survives the fork.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Attempts

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 Attempts.



21
22
23
# File 'lib/kicks_liveness/attempts.rb', line 21

def initialize(dir)
  @dir = dir
end

Instance Method Details

#clear!(slot) ⇒ void

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.

This method returns an undefined value.

Called once the slot is healthy: the next start of it is a fresh one.



53
54
55
56
57
# File 'lib/kicks_liveness/attempts.rb', line 53

def clear!(slot)
  File.unlink(path(slot))
rescue StandardError
  nil
end

#record!(slot, now: Time.now.utc) ⇒ Array(Integer, Float)

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.

Records a start of this slot.



31
32
33
34
35
36
# File 'lib/kicks_liveness/attempts.rb', line 31

def record!(slot, now: Time.now.utc)
  count, first = read(slot)
  first ||= now
  write(slot, count + 1, first)
  [count + 1, now - first]
end

#restart_window!(slot, now: Time.now.utc) ⇒ void

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.

This method returns an undefined value.

Resets the elapsed-time window while keeping the count, so a respawn loop reports once per window instead of once per respawn.



44
45
46
47
# File 'lib/kicks_liveness/attempts.rb', line 44

def restart_window!(slot, now: Time.now.utc)
  count, = read(slot)
  write(slot, count, now)
end