Class: Whatsapp::Heartbeat

Inherits:
Object
  • Object
show all
Defined in:
lib/whatsapp/heartbeat.rb

Overview

Periodic health check for the bridge and WhatsApp connection.

Runs in a background thread, pinging +GET /health+ and +GET /status+ on the bridge at regular intervals. Emits events for health changes.

Events

  • +health.ok+ — bridge and WhatsApp are healthy
  • +health.failure+ — check failed (bridge down, WhatsApp disconnected, etc.)
  • +health.recovered+ — recovered after one or more failures

Examples:

heartbeat = Whatsapp::Heartbeat.new(session, interval: 30)
heartbeat.start
heartbeat.healthy?  # => true/false
heartbeat.stop

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, interval: 30) ⇒ Heartbeat

Returns a new instance of Heartbeat.

Parameters:

  • session (Session)

    the session to monitor

  • interval (Integer) (defaults to: 30)

    seconds between health checks



36
37
38
39
40
41
42
43
44
45
# File 'lib/whatsapp/heartbeat.rb', line 36

def initialize(session, interval: 30)
  @session = session
  @interval = interval
  @running = false
  @thread = nil
  @last_check = nil
  @last_status = nil
  @consecutive_failures = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#consecutive_failuresInteger (readonly)

Returns number of consecutive failed checks.

Returns:

  • (Integer)

    number of consecutive failed checks



32
33
34
# File 'lib/whatsapp/heartbeat.rb', line 32

def consecutive_failures
  @consecutive_failures
end

#intervalInteger (readonly)

Returns check interval in seconds.

Returns:

  • (Integer)

    check interval in seconds



23
24
25
# File 'lib/whatsapp/heartbeat.rb', line 23

def interval
  @interval
end

#last_checkTime? (readonly)

Returns timestamp of the last health check.

Returns:

  • (Time, nil)

    timestamp of the last health check



26
27
28
# File 'lib/whatsapp/heartbeat.rb', line 26

def last_check
  @last_check
end

#last_statusSymbol? (readonly)

Returns result of the last check (:ok, :bridge_down, :whatsapp_disconnected, etc.).

Returns:

  • (Symbol, nil)

    result of the last check (:ok, :bridge_down, :whatsapp_disconnected, etc.)



29
30
31
# File 'lib/whatsapp/heartbeat.rb', line 29

def last_status
  @last_status
end

#sessionSession (readonly)

Returns the session being monitored.

Returns:

  • (Session)

    the session being monitored



20
21
22
# File 'lib/whatsapp/heartbeat.rb', line 20

def session
  @session
end

Instance Method Details

#check_nowSymbol

Perform a health check immediately (outside the loop).

Returns:

  • (Symbol)

    the check result



82
83
84
# File 'lib/whatsapp/heartbeat.rb', line 82

def check_now
  perform_check
end

#healthy?Boolean

Whether the last check was successful.

Returns:

  • (Boolean)


89
90
91
# File 'lib/whatsapp/heartbeat.rb', line 89

def healthy?
  @last_status == :ok
end

#running?Boolean

Whether the health check loop is running.

Returns:

  • (Boolean)


75
76
77
# File 'lib/whatsapp/heartbeat.rb', line 75

def running?
  @mutex.synchronize { @running }
end

#startself

Start the health check loop in a background thread.

Returns:

  • (self)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/whatsapp/heartbeat.rb', line 50

def start
  @mutex.synchronize do
    return if @running

    @running = true
  end

  @thread = Thread.new { run_loop }
  @thread.abort_on_exception = false
  self
end

#stopself

Stop the health check loop.

Returns:

  • (self)


65
66
67
68
69
70
# File 'lib/whatsapp/heartbeat.rb', line 65

def stop
  @mutex.synchronize { @running = false }
  @thread&.join(5)
  @thread = nil
  self
end

#to_hHash

Health status as a hash.

Returns:

  • (Hash)

    +{ running:, healthy:, last_check:, last_status:, consecutive_failures: }+



96
97
98
99
100
101
102
103
104
# File 'lib/whatsapp/heartbeat.rb', line 96

def to_h
  {
    running: running?,
    healthy: healthy?,
    last_check: @last_check,
    last_status: @last_status,
    consecutive_failures: @consecutive_failures
  }
end