<commit_before>require 'travis/support/instrumentation'

module Travis
  module Addons
    module Pusher

      # Notifies registered clients about various state changes through Pusher.
      class Task < Travis::Task

        def self.chunk_size
          9 * 1024
        end

        def event
          params[:event]
        end

        def version
          params[:version] || 'v1'
        end

        def client_event
          @client_event ||= (event =~ /job:.*/ ? event.gsub(/(test|configure):/, '') : event)
        end

        def channels
          case client_event
          when 'job:log'
            ["job-#{payload[:id]}"]
          else
            ['common']
          end
        end

        private

          def process
            channels.each { |channel| trigger(channel, payload) }
          end

          def trigger(channel, payload)
            prefix = version == 'v1' ? nil : version
            event = [prefix, client_event].compact.join(':')

            parts(payload).each do |part|
              # TODO: the second argument in meter can be removed when we're sure that apps
              #       using this have newest travis-support version
              Travis::Instrumentation.meter('travis.addons.pusher.task.messages', {})

              begin
                Travis.pusher[channel].trigger(event, part)
              rescue ::Pusher::Error => e
                Travis.logger.error("[addons:pusher] Could not send event due to Pusher::Error: #{e.message}, event=#{event}, payload: #{part.inspect}")
                raise
              end
            end
          end

          def parts(payload)
            if client_event == 'job:log' && payload[:_log].present?
              # split payload into 9kB chunks, the limit is 10 for entire request
              # body, 1kB should be enough for headers
              log = payload[:_log]
              chunkifier = Chunkifier.new(log, chunk_size, :json => true)

              if chunkifier.length > 1
                Travis.logger.warn("[addons:pusher] The log part from worker was bigger than 9kB, payload: #{payload.inspect}")<commit_msg>Add comment on chunkifier warning<commit_after>require 'travis/support/instrumentation'

module Travis
  module Addons
    module Pusher

      # Notifies registered clients about various state changes through Pusher.
      class Task < Travis::Task

        def self.chunk_size
          9 * 1024
        end

        def event
          params[:event]
        end

        def version
          params[:version] || 'v1'
        end

        def client_event
          @client_event ||= (event =~ /job:.*/ ? event.gsub(/(test|configure):/, '') : event)
        end

        def channels
          case client_event
          when 'job:log'
            ["job-#{payload[:id]}"]
          else
            ['common']
          end
        end

        private

          def process
            channels.each { |channel| trigger(channel, payload) }
          end

          def trigger(channel, payload)
            prefix = version == 'v1' ? nil : version
            event = [prefix, client_event].compact.join(':')

            parts(payload).each do |part|
              # TODO: the second argument in meter can be removed when we're sure that apps
              #       using this have newest travis-support version
              Travis::Instrumentation.meter('travis.addons.pusher.task.messages', {})

              begin
                Travis.pusher[channel].trigger(event, part)
              rescue ::Pusher::Error => e
                Travis.logger.error("[addons:pusher] Could not send event due to Pusher::Error: #{e.message}, event=#{event}, payload: #{part.inspect}")
                raise
              end
            end
          end

          def parts(payload)
            if client_event == 'job:log' && payload[:_log].present?
              # split payload into 9kB chunks, the limit is 10 for entire request
              # body, 1kB should be enough for headers
              log = payload[:_log]
              chunkifier = Chunkifier.new(log, chunk_size, :json => true)

              if chunkifier.length > 1
                # This should never happen when we update travis-worker to split log parts
                # bigger than 9kB.
                Travis.logger.warn("[addons:pusher] The log part from worker was bigger than 9kB, payload: #{payload.inspect}")
{'fraction_non_alphanumeric': 0.09571106094808127, 'fraction_numerical': 0.006094808126410835, 'mean_word_length': 1.6919805589307413, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4989445', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1147, 'n_words': 348}