Class: RobotLab::To::Backoff
- Inherits:
-
Object
- Object
- RobotLab::To::Backoff
- Defined in:
- lib/robot_lab/to/backoff.rb
Overview
Exponential backoff with interruptible sleep.
Formula: 60 * 2^(consecutive_errors - 1) seconds 1 error → 60s 2 errors → 120s 3 errors → 240s
Constant Summary collapse
- BASE_SECONDS =
60
Instance Method Summary collapse
-
#initialize ⇒ Backoff
constructor
A new instance of Backoff.
- #interrupt! ⇒ Object
- #sleep_for(consecutive_errors) ⇒ Object
-
#sleep_seconds(seconds) ⇒ Object
Sleep a fixed number of seconds, interruptibly (1s ticks).
Constructor Details
#initialize ⇒ Backoff
Returns a new instance of Backoff.
14 15 16 |
# File 'lib/robot_lab/to/backoff.rb', line 14 def initialize @interrupted = false end |
Instance Method Details
#interrupt! ⇒ Object
41 42 43 |
# File 'lib/robot_lab/to/backoff.rb', line 41 def interrupt! @interrupted = true end |
#sleep_for(consecutive_errors) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/robot_lab/to/backoff.rb', line 18 def sleep_for(consecutive_errors) @interrupted = false return if consecutive_errors <= 0 duration = (BASE_SECONDS * (2**(consecutive_errors - 1))).to_i duration.times do return if @interrupted sleep(1) end end |
#sleep_seconds(seconds) ⇒ Object
Sleep a fixed number of seconds, interruptibly (1s ticks). Used for polling a blocking decision file. Does NOT reset @interrupted: once a signal fires, subsequent waits return immediately so shutdown is prompt.
33 34 35 36 37 38 39 |
# File 'lib/robot_lab/to/backoff.rb', line 33 def sleep_seconds(seconds) seconds.to_i.times do return if @interrupted sleep(1) end end |