module SplitIoClient module Cache module Stores class SplitStore attr_reader :splits_repository def initialize(splits_repository, api_key, metrics, sdk_blocker = nil) @splits_repository = splits_repository @api_key = api_key @metrics = metrics @sdk_blocker = sdk_blocker end def call if ENV['SPLITCLIENT_ENV'] == 'test' store_splits else splits_thread if defined?(PhusionPassenger) PhusionPassenger.on_event(:starting_worker_process) do |forked| splits_thread if forked end end end end private def splits_thread SplitIoClient.configuration.threads[:split_store] = Thread.new do SplitIoClient.configuration.logger.info('Starting splits fetcher service') loop do store_splits sleep(random_interval(SplitIoClient.configuration.features_refresh_rate)) end end end def store_splits data = splits_since(@splits_repository.get_change_number) data[:splits] && data[:splits].each do |split| add_split_unless_archived(split) end @splits_repository.set_segment_names(data[:segment_names]) @splits_repository.set_change_number(data[:till]) SplitIoClient.configuration.logger.debug("segments seen(#{data[:segment_names].length}): #{data[:segment_names].to_a}") if SplitIoClient.configuration.debug_enabled if SplitIoClient.configuration.block_until_ready > 0 && !@sdk_blocker.ready? @sdk_blocker.splits_ready! SplitIoClient.configuration.logger.info('splits are ready') end rescue StandardError => error SplitIoClient.configuration.log_found_exception(__method__.to_s, error) end def random_interval(interval) random_factor = Random.new.rand(50..100) / 100.0 interval * random_factor end def splits_since(since) splits_api.since(since) end def add_split_unless_archived(split) if Engine::Models::Split.archived?(split) SplitIoClient.configuration.logger.debug("Seeing archived split #{split[:name]}") if SplitIoClient.configuration.debug_enabled remove_archived_split(split) else store_split(split) end end def remove_archived_split(split) SplitIoClient.configuration.logger.debug("removing split from store(#{split})") if SplitIoClient.configuration.debug_enabled @splits_repository.remove_split(split[:name]) end def store_split(split) SplitIoClient.configuration.logger.debug("storing split (#{split[:name]})") if SplitIoClient.configuration.debug_enabled @splits_repository.add_split(split) end private def splits_api @splits_api ||= SplitIoClient::Api::Splits.new(@api_key, @metrics) end end end end end