Sha256: 62fa9f0de37579c35b074b4b5115689ebd5143b7e642af4e812703e1c299f276

Contents?: true

Size: 1.91 KB

Versions: 16

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

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

        def call
          if SplitIoClient.configuration.disable_impressions
            SplitIoClient.configuration.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
          SplitIoClient.configuration.threads[:impressions_sender] = Thread.new do
            begin
              SplitIoClient.configuration.logger.info('Starting impressions service')

              loop do
                post_impressions

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

              @impressions_repository.clear
            end
          end
        end

        def post_impressions
          impressions_api.post(formatted_impressions)
        rescue StandardError => error
          SplitIoClient.configuration.log_found_exception(__method__.to_s, error)
        end

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

        def impressions_api
          @impressions_api ||= SplitIoClient::Api::Impressions.new(@api_key)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
splitclient-rb-6.1.0-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.1.0 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.1.0.pre.rc2-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.1.0.pre.rc2 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.1.0.pre.rc1-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.1.0.pre.rc1 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.0.1-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.0.1 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.0.1.pre.rc2-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.0.1.pre.rc2 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.0.1.pre.rc1-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.0.1.pre.rc1 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.0.0-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-6.0.0 lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.1.3.pre.rc4-java lib/splitclient-rb/cache/senders/impressions_sender.rb
splitclient-rb-5.1.3.pre.rc4 lib/splitclient-rb/cache/senders/impressions_sender.rb