Class: SplitIoClient::Cache::Fetchers::SegmentFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/splitclient-rb/cache/fetchers/segment_fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segments_repository, api_key, metrics, config, sdk_blocker = nil) ⇒ SegmentFetcher



7
8
9
10
11
12
13
14
# File 'lib/splitclient-rb/cache/fetchers/segment_fetcher.rb', line 7

def initialize(segments_repository, api_key, metrics, config, sdk_blocker = nil)
  @segments_repository = segments_repository
  @api_key = api_key
  @metrics = metrics
  @config = config
  @sdk_blocker = sdk_blocker
  @semaphore = Mutex.new
end

Instance Attribute Details

#segments_repositoryObject (readonly)

Returns the value of attribute segments_repository.



5
6
7
# File 'lib/splitclient-rb/cache/fetchers/segment_fetcher.rb', line 5

def segments_repository
  @segments_repository
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/splitclient-rb/cache/fetchers/segment_fetcher.rb', line 16

def call
  if ENV['SPLITCLIENT_ENV'] == 'test'
    fetch_segments
  else
    segments_thread

    if defined?(PhusionPassenger)
      PhusionPassenger.on_event(:starting_worker_process) do |forked|
        segments_thread if forked
      end
    end
  end
end

#fetch_segment(name) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/splitclient-rb/cache/fetchers/segment_fetcher.rb', line 40

def fetch_segment(name)
  @semaphore.synchronize do
    segments_api.fetch_segments_by_names([name])
  end
rescue StandardError => error
  @config.log_found_exception(__method__.to_s, error)
end

#fetch_segmentsObject



48
49
50
51
52
53
54
55
56
# File 'lib/splitclient-rb/cache/fetchers/segment_fetcher.rb', line 48

def fetch_segments
  @semaphore.synchronize do
    segments_api.fetch_segments_by_names(@segments_repository.used_segment_names)

    @sdk_blocker.segments_ready!
  end
rescue StandardError => error
  @config.log_found_exception(__method__.to_s, error)
end

#fetch_segments_if_not_exists(names) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/splitclient-rb/cache/fetchers/segment_fetcher.rb', line 30

def fetch_segments_if_not_exists(names)
  names.each do |name|
    change_number = @segments_repository.get_change_number(name)

    fetch_segment(name) if change_number == -1
  end
rescue StandardError => error
  @config.log_found_exception(__method__.to_s, error)
end

#stop_segments_threadObject



58
59
60
# File 'lib/splitclient-rb/cache/fetchers/segment_fetcher.rb', line 58

def stop_segments_thread
  SplitIoClient::Helpers::ThreadHelper.stop(:segment_fetcher, @config)
end