Class: Whatsapp::Heartbeat
- Inherits:
-
Object
- Object
- Whatsapp::Heartbeat
- 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
Instance Attribute Summary collapse
-
#consecutive_failures ⇒ Integer
readonly
Number of consecutive failed checks.
-
#interval ⇒ Integer
readonly
Check interval in seconds.
-
#last_check ⇒ Time?
readonly
Timestamp of the last health check.
-
#last_status ⇒ Symbol?
readonly
Result of the last check (:ok, :bridge_down, :whatsapp_disconnected, etc.).
-
#session ⇒ Session
readonly
The session being monitored.
Instance Method Summary collapse
-
#check_now ⇒ Symbol
Perform a health check immediately (outside the loop).
-
#healthy? ⇒ Boolean
Whether the last check was successful.
-
#initialize(session, interval: 30) ⇒ Heartbeat
constructor
A new instance of Heartbeat.
-
#running? ⇒ Boolean
Whether the health check loop is running.
-
#start ⇒ self
Start the health check loop in a background thread.
-
#stop ⇒ self
Stop the health check loop.
-
#to_h ⇒ Hash
Health status as a hash.
Constructor Details
#initialize(session, interval: 30) ⇒ Heartbeat
Returns a new instance of Heartbeat.
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_failures ⇒ Integer (readonly)
Returns number of consecutive failed checks.
32 33 34 |
# File 'lib/whatsapp/heartbeat.rb', line 32 def consecutive_failures @consecutive_failures end |
#interval ⇒ Integer (readonly)
Returns check interval in seconds.
23 24 25 |
# File 'lib/whatsapp/heartbeat.rb', line 23 def interval @interval end |
#last_check ⇒ Time? (readonly)
Returns timestamp of the last health check.
26 27 28 |
# File 'lib/whatsapp/heartbeat.rb', line 26 def last_check @last_check end |
#last_status ⇒ Symbol? (readonly)
Returns 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 |
#session ⇒ Session (readonly)
Returns the session being monitored.
20 21 22 |
# File 'lib/whatsapp/heartbeat.rb', line 20 def session @session end |
Instance Method Details
#check_now ⇒ Symbol
Perform a health check immediately (outside the loop).
82 83 84 |
# File 'lib/whatsapp/heartbeat.rb', line 82 def check_now perform_check end |
#healthy? ⇒ Boolean
Whether the last check was successful.
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.
75 76 77 |
# File 'lib/whatsapp/heartbeat.rb', line 75 def running? @mutex.synchronize { @running } end |
#start ⇒ self
Start the health check loop in a background thread.
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 |
#stop ⇒ self
Stop the health check loop.
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_h ⇒ Hash
Health status as a hash.
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 |