Sha256: 847d524641969cb8804ab2901cb3b6bea312615dc2d5ecd5ebc6573970624157

Contents?: true

Size: 1.87 KB

Versions: 26

Compression:

Stored size: 1.87 KB

Contents

module SplitIoClient
  module Cache
    module Senders
      class ImpressionsSender
        def initialize(impressions_repository, config, api_key)
          @impressions_repository = impressions_repository
          @config = config
          @api_key = api_key
        end

        def call
          # Disable impressions if @config.impressions_queue_size == -1
          if @config.impressions_queue_size < 0
            @config.logger.info('Disabling impressions service by config')
            return
          end

          if ENV['SPLITCLIENT_ENV'] == 'test'
            post_impressions
          else
            impressions_thread

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

        private

        def impressions_thread
          @config.threads[:impressions_sender] = Thread.new do
            begin
              @config.logger.info('Starting impressions service')

              loop do
                post_impressions

                sleep(SplitIoClient::Utilities.randomize_interval(@config.impressions_refresh_rate))
              end
            rescue SplitIoClient::ImpressionShutdownException
              post_impressions

              @impressions_repository.clear
            end
          end
        end

        def post_impressions
          impressions_client.post
        rescue StandardError => error
          @config.log_found_exception(__method__.to_s, error)
        end

        def formatted_impressions(raw_impressions = nil)
          ImpressionsFormatter.new(@impressions_repository).call(raw_impressions)
        end

        def impressions_client
          SplitIoClient::Api::Impressions.new(@api_key, @config, formatted_impressions)
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
splitclient-rb-5.0.2-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.0.2 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.0.2.pre.rc1-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.0.2.pre.rc1 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.0.1-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.0.1 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.0.1.pre.rc1-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.0.1.pre.rc1 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.0.0-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.0.0 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.0.0.pre.rc1-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.0.0.pre.rc1 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-4.5.2-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-4.5.2 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-4.5.2.pre.rc1-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-4.5.2.pre.rc1 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-4.5.1-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-4.5.1.pre.dev2 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-4.5.1.pre.dev lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-4.5.1 lib/splitclient-rb/cache/senders/impressions_sender.rb