Class: EppoClient::Poller
- Inherits:
-
Object
- Object
- EppoClient::Poller
- Defined in:
- lib/poller.rb
Overview
The poller class invokes a callback and waits on repeat on a separate thread
Instance Method Summary collapse
- #_wait_for_interval ⇒ Object
-
#initialize(interval_millis, jitter_millis, callback) ⇒ Poller
constructor
A new instance of Poller.
- #poll ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #stopped? ⇒ Boolean
Constructor Details
#initialize(interval_millis, jitter_millis, callback) ⇒ Poller
Returns a new instance of Poller.
10 11 12 13 14 15 16 |
# File 'lib/poller.rb', line 10 def initialize(interval_millis, jitter_millis, callback) @jitter_millis = jitter_millis @interval = interval_millis @stopped = Concurrent::Atom.new(false) @callback = callback @thread = nil end |
Instance Method Details
#_wait_for_interval ⇒ Object
44 45 46 47 |
# File 'lib/poller.rb', line 44 def _wait_for_interval interval_with_jitter = @interval - rand(@jitter_millis) sleep interval_with_jitter / 1000 end |
#poll ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/poller.rb', line 32 def poll until stopped? begin @callback.call rescue StandardError => e EppoClient.logger('err').error("Unexpected error running poll task: #{e}") break end _wait_for_interval end end |
#start ⇒ Object
18 19 20 21 |
# File 'lib/poller.rb', line 18 def start @stopped.reset(false) @thread = Thread.new { poll } end |
#stop ⇒ Object
23 24 25 26 |
# File 'lib/poller.rb', line 23 def stop @stopped.reset(true) Thread.kill(@thread) end |
#stopped? ⇒ Boolean
28 29 30 |
# File 'lib/poller.rb', line 28 def stopped? @stopped.value end |