Class: Optimizely::HTTPProjectConfigManager

Inherits:
ProjectConfigManager show all
Defined in:
lib/optimizely/config_manager/http_project_config_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key: nil, url: nil, url_template: nil, polling_interval: nil, blocking_timeout: nil, auto_update: true, start_by_default: true, datafile: nil, logger: nil, error_handler: nil, skip_json_validation: false, notification_center: nil) ⇒ HTTPProjectConfigManager

Initialize config manager. One of sdk_key or url has to be set to be able to use.

sdk_key - Optional string uniquely identifying the datafile. It’s required unless a URL is passed in. datafile: Optional JSON string representing the project. polling_interval - Optional floating point number representing time interval in seconds

at which to request datafile and set ProjectConfig.

blocking_timeout - Optional Time in seconds to block the config call until config object has been initialized. auto_update - Boolean indicates to run infinitely or only once. start_by_default - Boolean indicates to start by default AsyncScheduler. url - Optional string representing URL from where to fetch the datafile. If set it supersedes the sdk_key. url_template - Optional string template which in conjunction with sdk_key

determines URL from where to fetch the datafile.

logger - Provides a logger instance. error_handler - Provides a handle_error method to handle exceptions. skip_json_validation - Optional boolean param which allows skipping JSON schema

validation upon object invocation. By default JSON schema validation will be performed.


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 52

def initialize(
  sdk_key: nil,
  url: nil,
  url_template: nil,
  polling_interval: nil,
  blocking_timeout: nil,
  auto_update: true,
  start_by_default: true,
  datafile: nil,
  logger: nil,
  error_handler: nil,
  skip_json_validation: false,
  notification_center: nil
)
  @logger = logger || NoOpLogger.new
  @error_handler = error_handler || NoOpErrorHandler.new
  @datafile_url = get_datafile_url(sdk_key, url, url_template)
  @polling_interval = nil
  polling_interval(polling_interval)
  @blocking_timeout = nil
  blocking_timeout(blocking_timeout)
  @last_modified = nil
  @skip_json_validation = skip_json_validation
  @notification_center = notification_center.is_a?(Optimizely::NotificationCenter) ? notification_center : NotificationCenter.new(@logger, @error_handler)
  @config = datafile.nil? ? nil : DatafileProjectConfig.create(datafile, @logger, @error_handler, @skip_json_validation)
  @optimizely_config = @config.nil? ? nil : OptimizelyConfig.new(@config).config
  @mutex = Mutex.new
  @resource = ConditionVariable.new
  @async_scheduler = AsyncScheduler.new(method(:fetch_datafile_config), @polling_interval, auto_update, @logger)
  # Start async scheduler in the end to avoid race condition where scheduler executes
  # callback which makes use of variables not yet initialized by the main thread.
  @async_scheduler.start! if start_by_default == true
  @stopped = false
end

Instance Attribute Details

#optimizely_configObject (readonly)

Config manager that polls for the datafile and updated ProjectConfig based on an update interval.



34
35
36
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 34

def optimizely_config
  @optimizely_config
end

#stoppedObject (readonly)

Config manager that polls for the datafile and updated ProjectConfig based on an update interval.



34
35
36
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 34

def stopped
  @stopped
end

Instance Method Details

#configObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 112

def config
  # Get Project Config.

  # if stopped is true, then simply return @config.
  # If the background datafile polling thread is running. and config has been initalized,
  # we simply return @config.
  # If it is not, we wait and block maximum for @blocking_timeout.
  # If thread is not running, we fetch the datafile and update config.
  return @config if @stopped

  if @async_scheduler.running
    return @config if ready?

    @mutex.synchronize do
      @resource.wait(@mutex, @blocking_timeout)
      return @config
    end
  end

  fetch_datafile_config
  @config
end

#ready?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 87

def ready?
  !@config.nil?
end

#start!Object



91
92
93
94
95
96
97
98
99
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 91

def start!
  if @stopped
    @logger.log(Logger::WARN, 'Not starting. Already stopped.')
    return
  end

  @async_scheduler.start!
  @stopped = false
end

#stop!Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 101

def stop!
  if @stopped
    @logger.log(Logger::WARN, 'Not pausing. Manager has not been started.')
    return
  end

  @async_scheduler.stop!
  @config = nil
  @stopped = true
end