Class: ConfigCat::ConfigService

Inherits:
Object
  • Object
show all
Defined in:
lib/configcat/configservice.rb

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key, polling_mode, hooks, config_fetcher, log, config_cache, is_offline) ⇒ ConfigService

Returns a new instance of ConfigService.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/configcat/configservice.rb', line 9

def initialize(sdk_key, polling_mode, hooks, config_fetcher, log, config_cache, is_offline)
  @sdk_key = sdk_key
  @cached_entry = ConfigEntry::EMPTY
  @cached_entry_string = ''
  @polling_mode = polling_mode
  @log = log
  @config_cache = config_cache
  @hooks = hooks
  @cache_key = Digest::SHA1.hexdigest("ruby_#{CONFIG_FILE_NAME}_#{@sdk_key}")
  @config_fetcher = config_fetcher
  @is_offline = is_offline
  @response_future = nil
  @initialized = Concurrent::Event.new
  @lock = Mutex.new
  @ongoing_fetch = false
  @fetch_finished = Concurrent::Event.new
  @start_time = Utils.get_utc_now_seconds_since_epoch

  if @polling_mode.is_a?(AutoPollingMode)
    start_poll
  else
    set_initialized
  end
end

Instance Method Details

#closeObject



95
96
97
98
99
# File 'lib/configcat/configservice.rb', line 95

def close
  if @polling_mode.is_a?(AutoPollingMode)
    @stopped.set
  end
end

#get_settingsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/configcat/configservice.rb', line 34

def get_settings
  if @polling_mode.is_a?(LazyLoadingMode)
    entry, _ = fetch_if_older(Utils.get_utc_now_seconds_since_epoch - @polling_mode.cache_refresh_interval_seconds)
    return entry.config[FEATURE_FLAGS], entry.fetch_time
  elsif @polling_mode.is_a?(AutoPollingMode) && !@initialized.set?
    elapsed_time = Utils.get_utc_now_seconds_since_epoch - @start_time # Elapsed time in seconds
    if elapsed_time < @polling_mode.max_init_wait_time_seconds
      @initialized.wait(@polling_mode.max_init_wait_time_seconds - elapsed_time)

      # Max wait time expired without result, notify subscribers with the cached config.
      if !@initialized.set?
        set_initialized
        return @cached_entry.config[FEATURE_FLAGS], @cached_entry.fetch_time
      end
    end
  end

  entry, _ = fetch_if_older(Utils::DISTANT_PAST, prefer_cache: true)
  return entry.config[FEATURE_FLAGS], entry.fetch_time
end

#offline?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/configcat/configservice.rb', line 91

def offline?
  return @is_offline
end

#refreshObject

:return [RefreshResult]



56
57
58
59
# File 'lib/configcat/configservice.rb', line 56

def refresh
  _, error = fetch_if_older(Utils::DISTANT_FUTURE)
  return RefreshResult.new(success = error.nil?, error = error)
end

#set_offlineObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/configcat/configservice.rb', line 75

def set_offline
  @lock.synchronize do
    if @is_offline
      return
    end

    @is_offline = true
    if @polling_mode.is_a?(AutoPollingMode)
      @stopped.set
      @thread.join
    end

    @log.debug('Switched to OFFLINE mode.')
  end
end

#set_onlineObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/configcat/configservice.rb', line 61

def set_online
  @lock.synchronize do
    if !@is_offline
      return
    end

    @is_offline = false
    if @polling_mode.is_a?(AutoPollingMode)
      start_poll
    end
    @log.debug('Switched to ONLINE mode.')
  end
end