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
readonly
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.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/flipper/poller.rb', line 25 def initialize( = {}) @thread = nil @pid = Process.pid @mutex = Mutex.new @instrumenter = .fetch(:instrumenter, Instrumenters::Noop) @remote_adapter = .fetch(:remote_adapter) @interval = .fetch(:interval, 10).to_f @last_synced_at = Concurrent::AtomicFixnum.new(0) @adapter = Adapters::Memory.new(nil, threadsafe: true) if @interval < MINIMUM_POLL_INTERVAL warn "Flipper::Cloud poll interval must be greater than or equal to #{MINIMUM_POLL_INTERVAL} but was #{@interval}. Setting @interval to #{MINIMUM_POLL_INTERVAL}." @interval = MINIMUM_POLL_INTERVAL end @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.
8 9 10 |
# File 'lib/flipper/poller.rb', line 8 def adapter @adapter end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
8 9 10 |
# File 'lib/flipper/poller.rb', line 8 def interval @interval end |
#last_synced_at ⇒ Object (readonly)
Returns the value of attribute last_synced_at.
8 9 10 |
# File 'lib/flipper/poller.rb', line 8 def last_synced_at @last_synced_at end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
8 9 10 |
# File 'lib/flipper/poller.rb', line 8 def mutex @mutex end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
8 9 10 |
# File 'lib/flipper/poller.rb', line 8 def pid @pid end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
8 9 10 |
# File 'lib/flipper/poller.rb', line 8 def thread @thread end |
Class Method Details
.get(key, options = {}) ⇒ Object
15 16 17 |
# File 'lib/flipper/poller.rb', line 15 def self.get(key, = {}) instances.compute_if_absent(key) { new() } end |
.reset ⇒ Object
19 20 21 |
# File 'lib/flipper/poller.rb', line 19 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 start = Concurrent.monotonic_time begin sync rescue => exception # you can instrument these using poller.flipper end sleep interval end end |
#start ⇒ Object
47 48 49 50 |
# File 'lib/flipper/poller.rb', line 47 def start reset if forked? 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 |
# File 'lib/flipper/poller.rb', line 73 def sync @instrumenter.instrument("poller.#{InstrumentationNamespace}", operation: :poll) do @adapter.import @remote_adapter @last_synced_at.update { |time| Concurrent.monotonic_time } end end |