Class: Optimizely::AsyncScheduler
- Inherits:
-
Object
- Object
- Optimizely::AsyncScheduler
- Defined in:
- lib/optimizely/config_manager/async_scheduler.rb
Instance Attribute Summary collapse
-
#running ⇒ Object
readonly
Returns the value of attribute running.
Instance Method Summary collapse
-
#initialize(callback, interval, auto_update, logger = nil) ⇒ AsyncScheduler
constructor
A new instance of AsyncScheduler.
- #start! ⇒ Object
- #stop! ⇒ Object
Constructor Details
#initialize(callback, interval, auto_update, logger = nil) ⇒ AsyncScheduler
Returns a new instance of AsyncScheduler.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/optimizely/config_manager/async_scheduler.rb', line 22 def initialize(callback, interval, auto_update, logger = nil) # Sets up AsyncScheduler to execute a callback periodically. # # callback - Main function to be executed periodically. # interval - How many seconds to wait between executions. # auto_update - boolean indicates to run infinitely or only once. # logger - Optional Provides a logger instance. @callback = callback @interval = interval @auto_update = auto_update @running = false @thread = nil @logger = logger || NoOpLogger.new end |
Instance Attribute Details
#running ⇒ Object (readonly)
Returns the value of attribute running.
20 21 22 |
# File 'lib/optimizely/config_manager/async_scheduler.rb', line 20 def running @running end |
Instance Method Details
#start! ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/optimizely/config_manager/async_scheduler.rb', line 38 def start! # Starts the async scheduler. if @running @logger.log( Logger::WARN, 'Scheduler is already running. Ignoring .start() call.' ) return end begin @running = true @thread = Thread.new { execution_wrapper(@callback) } rescue StandardError => e @logger.log( Logger::ERROR, "Couldn't create a new thread for async scheduler. #{e.message}" ) end end |
#stop! ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/optimizely/config_manager/async_scheduler.rb', line 60 def stop! # Stops the async scheduler. # If the scheduler is not running do nothing. return unless @running @running = false @thread.exit end |