Class: ConfigCat::AutoPollingCachePolicy

Inherits:
CachePolicy show all
Defined in:
lib/configcat/autopollingcachepolicy.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_fetcher, config_cache, poll_interval_seconds = 60, max_init_wait_time_seconds = 5, on_configuration_changed_callback = nil) ⇒ AutoPollingCachePolicy

Returns a new instance of AutoPollingCachePolicy.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/configcat/autopollingcachepolicy.rb', line 6

def initialize(config_fetcher, config_cache, poll_interval_seconds=60, max_init_wait_time_seconds=5, on_configuration_changed_callback=nil)
  if poll_interval_seconds < 1
    poll_interval_seconds = 1
  end
  if max_init_wait_time_seconds < 0
    max_init_wait_time_seconds = 0
  end
  @_config_fetcher = config_fetcher
  @_config_cache = config_cache
  @_poll_interval_seconds = poll_interval_seconds
  @_max_init_wait_time_seconds = max_init_wait_time_seconds
  @_on_configuration_changed_callback = on_configuration_changed_callback
  @_initialized = false
  @_is_running = false
  @_start_time = Time.now.utc
  @_lock = Concurrent::ReadWriteLock.new()
  @_is_started = Concurrent::Event.new()
  @thread = Thread.new{_run()}
  @_is_started.wait()
end

Instance Method Details

#_runObject



27
28
29
30
31
32
33
34
35
# File 'lib/configcat/autopollingcachepolicy.rb', line 27

def _run()
  @_is_running = true
  @_is_started.set()
  loop do
    force_refresh()
    sleep(@_poll_interval_seconds)
    break if !@_is_running
  end
end

#force_refreshObject



49
50
51
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/configcat/autopollingcachepolicy.rb', line 49

def force_refresh()
  begin
    configuration = @_config_fetcher.get_configuration_json()

    begin
      @_lock.acquire_read_lock()
      old_configuration = @_config_cache.get()
    ensure
      @_lock.release_read_lock()
    end

    if configuration != old_configuration
      begin
        @_lock.acquire_write_lock()
        @_config_cache.set(configuration)
        @_initialized = true
      ensure
        @_lock.release_write_lock()
      end
      begin
        if !@_on_configuration_changed_callback.equal?(nil)
          @_on_configuration_changed_callback.()
        end
      rescue Exception => e
        ConfigCat.logger.error("Exception in on_configuration_changed_callback: #{e.class}:'#{e}'")
      end
    end

    if !@_initialized && !old_configuration.equal?(nil)
      @_initialized = true
    end
  rescue Exception => e
    ConfigCat.logger.error("Double-check your API KEY at https://app.configcat.com/apikey.")
    ConfigCat.logger.error "threw exception #{e.class}:'#{e}'"
    ConfigCat.logger.error "stacktrace: #{e.backtrace}"
  end
end

#getObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/configcat/autopollingcachepolicy.rb', line 37

def get()
  while !@_initialized && (Time.now.utc < @_start_time + @_max_init_wait_time_seconds)
    sleep(0.5)
  end
  begin
    @_lock.acquire_read_lock()
    return @_config_cache.get()
  ensure
    @_lock.release_read_lock()
  end
end

#stopObject



87
88
89
# File 'lib/configcat/autopollingcachepolicy.rb', line 87

def stop()
  @_is_running = false
end