Class: Flipper::Poller
- Inherits:
-
Object
- Object
- Flipper::Poller
- Defined in:
- lib/flipper/poller.rb
Constant Summary collapse
- MINIMUM_POLL_INTERVAL =
10
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#last_synced_at ⇒ Object
readonly
Returns the value of attribute last_synced_at.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Poller
constructor
A new instance of Poller.
- #run ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #sync ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Poller
Returns a new instance of Poller.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/flipper/poller.rb', line 26 def initialize( = {}) @thread = nil @pid = Process.pid @mutex = Mutex.new @instrumenter = .fetch(:instrumenter, Instrumenters::Noop) @remote_adapter = .fetch(:remote_adapter) @last_synced_at = Concurrent::AtomicFixnum.new(0) @adapter = Adapters::Memory.new(nil, threadsafe: true) @shutdown_requested = Concurrent::AtomicBoolean.new(false) self.interval = .fetch(:interval, 10) @initial_interval = @interval @start_automatically = .fetch(:start_automatically, true) if .fetch(:shutdown_automatically, true) at_exit { stop } end end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
9 10 11 |
# File 'lib/flipper/poller.rb', line 9 def adapter @adapter end |
#interval ⇒ Object
Returns the value of attribute interval.
9 10 11 |
# File 'lib/flipper/poller.rb', line 9 def interval @interval end |
#last_synced_at ⇒ Object (readonly)
Returns the value of attribute last_synced_at.
9 10 11 |
# File 'lib/flipper/poller.rb', line 9 def last_synced_at @last_synced_at end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
9 10 11 |
# File 'lib/flipper/poller.rb', line 9 def mutex @mutex end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
9 10 11 |
# File 'lib/flipper/poller.rb', line 9 def pid @pid end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
9 10 11 |
# File 'lib/flipper/poller.rb', line 9 def thread @thread end |
Class Method Details
.get(key, options = {}) ⇒ Object
16 17 18 |
# File 'lib/flipper/poller.rb', line 16 def self.get(key, = {}) instances.compute_if_absent(key) { new() } end |
.reset ⇒ Object
20 21 22 |
# File 'lib/flipper/poller.rb', line 20 def self.reset instances.each {|_, instance| instance.stop }.clear end |
Instance Method Details
#run ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/flipper/poller.rb', line 59 def run loop do sleep jitter begin sync rescue # you can instrument these using poller.flipper end sleep interval end end |
#start ⇒ Object
46 47 48 49 50 |
# File 'lib/flipper/poller.rb', line 46 def start reset if forked? return if @shutdown_requested.true? ensure_worker_running end |
#stop ⇒ Object
52 53 54 55 56 57 |
# File 'lib/flipper/poller.rb', line 52 def stop @instrumenter.instrument("poller.#{InstrumentationNamespace}", { operation: :stop, }) @thread&.kill end |
#sync ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/flipper/poller.rb', line 73 def sync @instrumenter.instrument("poller.#{InstrumentationNamespace}", operation: :poll) do begin @adapter.import @remote_adapter @last_synced_at.update { |time| Concurrent.monotonic_time } ensure apply_response_headers end end end |