Sha256: e257c463d01863265daf68136b999d7c3c534e89002b3f2cffc6072712855f7e

Contents?: true

Size: 1.79 KB

Versions: 8

Compression:

Stored size: 1.79 KB

Contents

module Chillout
  class Worker
    attr_reader :dispatcher, :queue, :logger

    def initialize(max_queue, dispatcher, queue, logger, container_class=CreationsContainer)
      @max_queue = max_queue
      @dispatcher = dispatcher
      @queue = queue
      @logger = logger
      @container_class = container_class
    end

    def get_all_containers_to_process
      logger.debug "Waiting for at least one container."
      all_containers = [queue.pop]
      logger.debug "Received at least one container."
      (@max_queue-1).times do
        begin
          all_containers << queue.pop(true)
        rescue ThreadError
          break
        end
      end
      logger.debug "Received containers to process: #{all_containers.count}"
      all_containers
    end

    def merge_containers(containers_to_merge)
      mergable, unmergable = containers_to_merge.partition{|cont| @container_class === cont }
      creations_container = @container_class.new
      mergable.each do |container|
        creations_container.merge(container)
      end
      unmergable.unshift(creations_container) unless creations_container.empty?
      unmergable
    end

    def send_measurements(measurements)
      logger.debug "Trying to send creations"
      dispatcher.send_measurements(measurements)
      logger.info "Metrics sent"
    rescue Dispatcher::SendCreationsFailed
      logger.error "Sending metrics failed"
    end

    def send_startup_message
      dispatcher.send_startup_message
      logger.debug "Sending startup message"
    end

    def run
      logger.info "Worker started"
      send_startup_message
      loop do
        containers_to_merge = get_all_containers_to_process
        measurements = merge_containers(containers_to_merge)
        send_measurements(measurements)
        sleep 5
      end
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
chillout-0.8.9 lib/chillout/worker.rb
chillout-0.8.5.1 lib/chillout/worker.rb
chillout-0.8.8 lib/chillout/worker.rb
chillout-0.8.7 lib/chillout/worker.rb
chillout-0.8.6 lib/chillout/worker.rb
chillout-0.8.5 lib/chillout/worker.rb
chillout-0.8.4 lib/chillout/worker.rb
chillout-0.8.3 lib/chillout/worker.rb