Sha256: a0c1c91cd44f8ecbdb8f8ac9bfff16de9b8d0cea206fe3e5062c5b19f1c700f6

Contents?: true

Size: 1.04 KB

Versions: 39

Compression:

Stored size: 1.04 KB

Contents

require 'securerandom'

module Honeybadger
  class Agent
    class Batch
      def initialize(config, name, opts = {})
        @id = SecureRandom.uuid
        @config = config
        @name = name
        @max = opts.fetch(:max, 100)
        @interval = opts.fetch(:interval, 60)
        @future = opts.fetch(:now, now()) + interval
        @values = opts.fetch(:collection, Array.new)
        @mutex = Mutex.new
      end

      attr_reader :id

      def push(val)
        mutex.synchronize { values.push(val) }
      end

      def empty?
        mutex.synchronize { values.empty? }
      end

      def size
        mutex.synchronize { values.size }
      end

      def flush?
        size >= max || now >= future
      end

      def as_json(*args)
        mutex.synchronize do
          { name => values.map(&:to_h), :environment => config[:env], :hostname => config[:hostname] }
        end
      end

      private

      attr_reader :config, :name, :max, :interval, :values, :future, :mutex

      def now
        Time.now.to_i
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 2 rubygems

Version Path
honeybadger-2.7.2 lib/honeybadger/agent/batch.rb
honeybadger-2.7.1 lib/honeybadger/agent/batch.rb
honeybadger-2.7.0 lib/honeybadger/agent/batch.rb
honeybadger-2.6.1 lib/honeybadger/agent/batch.rb
honeybadger-2.6.0 lib/honeybadger/agent/batch.rb
honeybadger-2.5.3 lib/honeybadger/agent/batch.rb
honeybadger-2.5.2 lib/honeybadger/agent/batch.rb
honeybadger-2.5.1 lib/honeybadger/agent/batch.rb
honeybadger-2.5.0 lib/honeybadger/agent/batch.rb
honeybadger-2.4.1 lib/honeybadger/agent/batch.rb
honeybadger-2.4.0 lib/honeybadger/agent/batch.rb
honeybadger-2.3.3 lib/honeybadger/agent/batch.rb
honeybadger-2.3.2 lib/honeybadger/agent/batch.rb
honeybadger-2.3.2.beta.1 lib/honeybadger/agent/batch.rb
honeybadger-2.3.1 lib/honeybadger/agent/batch.rb
honeybadger-2.3.0 lib/honeybadger/agent/batch.rb
honeybadger-2.2.0 lib/honeybadger/agent/batch.rb
honeybadger-2.2.0.beta.1 lib/honeybadger/agent/batch.rb
honeybadger-2.1.6.beta.1 lib/honeybadger/agent/batch.rb
honeybadger-2.1.5 lib/honeybadger/agent/batch.rb