Class: Optimizely::AsyncScheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/config_manager/async_scheduler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callback, interval, auto_update, logger = nil, error_handler = nil) ⇒ AsyncScheduler

Returns a new instance of AsyncScheduler.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/optimizely/config_manager/async_scheduler.rb', line 22

def initialize(callback, interval, auto_update, logger = nil, error_handler = 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.
  # error_handler - Optional Provides a handle_error method to handle exceptions.

  @callback = callback
  @interval = interval
  @auto_update = auto_update
  @running = false
  @thread = nil
  @logger = logger || NoOpLogger.new
  @error_handler = error_handler || NoOpErrorHandler.new
end

Instance Attribute Details

#runningObject (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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/optimizely/config_manager/async_scheduler.rb', line 40

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}"
    )
    @error_handler.handle_error(e)
  end
end

#stop!Object



63
64
65
66
67
68
69
70
71
# File 'lib/optimizely/config_manager/async_scheduler.rb', line 63

def stop!
  # Stops the async scheduler.

  # If the scheduler is not running do nothing.
  return unless @running

  @running = false
  @thread.exit
end