Sha256: b929cc8239ac51300db1e3627200cec3b22749298732d816e187a2cb7935c97f

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

module SplitIoClient
  class ImpressionRouter
    attr_reader :router_thread

    def initialize(config)
      @config = config
      @listener = @config.impression_listener

      return unless @listener

      @queue = Queue.new
      router_thread

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

    def add_bulk(impressions)
      @config.logger.error("### ROUTER.IMPRESSIONS #{impressions.to_s}")
      @config.logger.error("### LISTENER #{@listener.nil?}")
      return unless @listener

      impressions.each do |impression|
        enqueue(
          split_name: impression[:i][:f],
          matching_key: impression[:i][:k],
          bucketing_key: impression[:i][:b],
          time: impression[:i][:m],
          treatment: {
            label: impression[:i][:r],
            treatment: impression[:i][:t],
            change_number: impression[:i][:c]
          },
          previous_time: impression[:i][:pt],
          attributes: impression[:attributes]
        ) unless impression.nil?
      end
    rescue StandardError => error
      @config.log_found_exception(__method__.to_s, error)
    end

    private

    def enqueue(impression)
      @queue.push(impression) if @listener
    end

    def router_thread
      @config.threads[:impression_router] = Thread.new do
        loop do
          begin
            @listener.log(@queue.pop)
          rescue StandardError => error
            @config.log_found_exception(__method__.to_s, error)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
splitclient-rb-7.1.4.pre.rc15-java lib/splitclient-rb/cache/routers/impression_router.rb
splitclient-rb-7.1.4.pre.rc15 lib/splitclient-rb/cache/routers/impression_router.rb