Class: HunkReviewChanges::Lifecycle
- Inherits:
-
Object
- Object
- HunkReviewChanges::Lifecycle
- Defined in:
- lib/hunk_review_changes/lifecycle.rb
Overview
Tracks the browser tab's liveness so the server can shut itself down shortly after the tab closes and never leak a process. The app records events here; the server's watchdog thread reads them.
Constant Summary collapse
- BYE_GRACE =
seconds to wait after a tab says it is closing
8- SILENT_LIMIT =
a live tab that goes quiet this long is treated as crashed
900- CONNECT_LIMIT =
give up if no tab ever connects
180
Instance Method Summary collapse
- #bye! ⇒ Object
-
#expired?(boot_time) ⇒ Boolean
Whether the server should exit now, given when it booted.
-
#heartbeat! ⇒ Object
A fresh heartbeat (e.g. from a reload) also cancels a pending goodbye.
-
#initialize(clock: -> { Time.now }) ⇒ Lifecycle
constructor
A new instance of Lifecycle.
Constructor Details
#initialize(clock: -> { Time.now }) ⇒ Lifecycle
Returns a new instance of Lifecycle.
12 13 14 15 16 17 |
# File 'lib/hunk_review_changes/lifecycle.rb', line 12 def initialize(clock: -> { Time.now }) @clock = clock @mutex = Mutex.new @last_heartbeat = nil @bye_at = nil end |
Instance Method Details
#bye! ⇒ Object
27 28 29 |
# File 'lib/hunk_review_changes/lifecycle.rb', line 27 def bye! @mutex.synchronize { @bye_at = @clock.call + BYE_GRACE } end |
#expired?(boot_time) ⇒ Boolean
Whether the server should exit now, given when it booted.
32 33 34 35 36 37 38 39 40 |
# File 'lib/hunk_review_changes/lifecycle.rb', line 32 def expired?(boot_time) now = @clock.call last, bye = @mutex.synchronize { [@last_heartbeat, @bye_at] } return true if bye && now >= bye return true if last && now - last > SILENT_LIMIT return true if last.nil? && now - boot_time > CONNECT_LIMIT false end |
#heartbeat! ⇒ Object
A fresh heartbeat (e.g. from a reload) also cancels a pending goodbye.
20 21 22 23 24 25 |
# File 'lib/hunk_review_changes/lifecycle.rb', line 20 def heartbeat! @mutex.synchronize do @last_heartbeat = @clock.call @bye_at = nil end end |