Class: Fuso::Heartbeat
- Inherits:
-
Object
- Object
- Fuso::Heartbeat
- Defined in:
- lib/fuso/heartbeat.rb
Overview
Writes a timestamp to ~/.fuso/heartbeat every INTERVAL seconds while the timer is running. On each tick, checks whether the last heartbeat is stale (gap > STALE_THRESHOLD), which indicates the system was suspended. Returns the stale timestamp so the app can use it as the real session end time.
Constant Summary collapse
- HEARTBEAT_FILE =
File.join(Config::CONFIG_DIR, "heartbeat")
- INTERVAL =
seconds between file writes
10- STALE_THRESHOLD =
seconds — gap larger than this means suspend
60
Instance Method Summary collapse
-
#clear ⇒ Object
Call when tracking stops or app quits.
-
#initialize ⇒ Heartbeat
constructor
A new instance of Heartbeat.
-
#pulse ⇒ Object
Call on each tick while tracking.
Constructor Details
#initialize ⇒ Heartbeat
Returns a new instance of Heartbeat.
16 17 18 |
# File 'lib/fuso/heartbeat.rb', line 16 def initialize @last_write_at = nil end |
Instance Method Details
#clear ⇒ Object
Call when tracking stops or app quits.
36 37 38 39 |
# File 'lib/fuso/heartbeat.rb', line 36 def clear File.delete(HEARTBEAT_FILE) if File.exist?(HEARTBEAT_FILE) @last_write_at = nil end |
#pulse ⇒ Object
Call on each tick while tracking. Returns the last-known timestamp if a suspend gap was detected, nil otherwise.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fuso/heartbeat.rb', line 22 def pulse now = Time.now stale_time = detect_staleness(now) return stale_time if stale_time if @last_write_at.nil? || (now - @last_write_at) >= INTERVAL write(now) @last_write_at = now end nil end |