class Sumologic::Metrics::Worker

Public Class Methods

new(queue, collector_uri, options = {}) click to toggle source

public: Creates a new worker

The worker continuously takes metrics off the queue and makes requests to the Sumologic api

queue - Queue synchronized between client and worker collector_uri - String of the unique collector URI options - Hash of worker options

batch_size - Fixnum of how many items to send in a batch
on_error   - Proc of what to do on an error
# File lib/sumologic/metrics/worker.rb, line 23
def initialize(queue, collector_uri, options = {})
  symbolize_keys! options
  @queue = queue
  @collector_uri = collector_uri
  @on_error = options[:on_error] || proc { |status, message| }
  batch_size = options[:batch_size] || Defaults::MetricBatch::MAX_SIZE
  @batch = MetricBatch.new(batch_size)
  @lock = Mutex.new
  @request = options[:request] || Request.new(uri: collector_uri)
end

Public Instance Methods

is_requesting?() click to toggle source

public: Check whether we have outstanding requests.

# File lib/sumologic/metrics/worker.rb, line 55
def is_requesting?
  @lock.synchronize { !@batch.empty? }
end
run() click to toggle source

public: Continuously runs the loop to check for new events

# File lib/sumologic/metrics/worker.rb, line 36
def run
  until Thread.current[:should_exit]
    return if @queue.empty?

    @lock.synchronize do
      until @batch.full? || @queue.empty?
        @batch << Metric.new(@queue.pop)
      end
    end

    res = @request.post(@batch)
    @on_error.call(res.status, res.message) unless res.status == 200

    @lock.synchronize { @batch.clear }
  end
end