Sha256: ba633a71a4e2e6a0e24c822a47c58a9d7c2dbab4691e538361084a7ce93ea787

Contents?: true

Size: 1.99 KB

Versions: 32

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module SplitIoClient
  module Cache
    module Senders
      class ImpressionsCountSender
        COUNTER_REFRESH_RATE_SECONDS = 1800

        def initialize(config, impression_counter, impressions_api)
          @config = config
          @impression_counter = impression_counter
          @impressions_api = impressions_api
        end

        def call
          impressions_count_thread

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

        private

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

              loop do
                post_impressions_count

                sleep(COUNTER_REFRESH_RATE_SECONDS)
              end
            rescue SplitIoClient::SDKShutdownException
              post_impressions_count

              @config.logger.info('Posting impressions count due to shutdown')
            end
          end

          def post_impressions_count
            @impressions_api.post_count(formatter(@impression_counter.pop_all))
          rescue StandardError => error
            @config.log_found_exception(__method__.to_s, error)
          end

          def formatter(counts)
            return if counts.empty?

            formated_counts = {pf: []}

            counts.each do |key, value|              
              key_splited = key.split('::')
              
              formated_counts[:pf] << {
                f: key_splited[0].to_s, # feature name
                m: key_splited[1].to_i, # time frame
                rc: value # count
              }
            end

            formated_counts
          rescue StandardError => error
            @config.log_found_exception(__method__.to_s, error)
          end
        end
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
splitclient-rb-7.2.1-java lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.2.1 lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.2.1.pre.rc1-java lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.2.1.pre.rc1 lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.2.0.pre.rc1-java lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.2.0.pre.rc1 lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.2.0-java lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.2.0 lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.1.4.pre.rc19-java lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.1.4.pre.rc19 lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.1.4.pre.rc18-java lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.1.4.pre.rc18 lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.1.4.pre.rc17-java lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.1.4.pre.rc17 lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.1.4.pre.rc16-java lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.1.4.pre.rc16 lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.1.4.pre.rc15-java lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.1.4.pre.rc15 lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.1.4.pre.rc14-java lib/splitclient-rb/cache/senders/impressions_count_sender.rb
splitclient-rb-7.1.4.pre.rc14 lib/splitclient-rb/cache/senders/impressions_count_sender.rb