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, datafile_access_token: nil, proxy_config: 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 datafile with sdk_key is passed in. datafile - Optional JSON string representing the project. If nil, sdk_key is required. 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.

datafile_access_token - access token used to fetch private datafiles proxy_config - Optional proxy config instancea to configure making web requests through a proxy server.

Raises:



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
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 56

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,
  datafile_access_token: nil,
  proxy_config: nil
)
  super()
  @logger = logger || NoOpLogger.new
  @error_handler = error_handler || NoOpErrorHandler.new
  @access_token = datafile_access_token
  @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)
  @optimizely_config = nil
  @config = datafile.nil? ? nil : DatafileProjectConfig.create(datafile, @logger, @error_handler, @skip_json_validation)
  @sdk_key = sdk_key || @config&.sdk_key

  raise MissingSdkKeyError if @sdk_key.nil?

  @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
  @proxy_config = proxy_config
  @stopped = false
end

Instance Attribute Details

#sdk_keyObject (readonly)

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



36
37
38
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 36

def sdk_key
  @sdk_key
end

#stoppedObject (readonly)

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



36
37
38
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 36

def stopped
  @stopped
end

Instance Method Details

#configObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 120

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

#optimizely_configObject



143
144
145
146
147
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 143

def optimizely_config
  @optimizely_config = OptimizelyConfig.new(@config, @logger).config if @optimizely_config.nil?

  @optimizely_config
end

#ready?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 100

def ready?
  !@config.nil?
end

#start!Object



104
105
106
107
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 104

def start!
  @async_scheduler.start!
  @stopped = false
end

#stop!Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/optimizely/config_manager/http_project_config_manager.rb', line 109

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