Sha256: ed0f8c5a708daf9991c1444bf289b9d9c0c0bb7db6cb98e3a59eae8e018b8e3f

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

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

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

    def get_all_containers_to_process
      all_containers = [queue.pop]
      loop do
        begin
          all_containers << queue.pop(true)
        rescue ThreadError
          break
        end
      end
      all_containers
    end

    def merge_containers(containers_to_merge)
      creations_container = @container_class.new
      for container in containers_to_merge
        creations_container.merge(container)
      end
      creations_container
    end

    def send_creations(creations_container)
      dispatcher.send_creations(creations_container)
      logger.info "Sending metrics"
    rescue Dispatcher::SendCreationsFailed
      queue << creations_container
      logger.error "Sending metrics failed"
    end

    def run
      loop do
        containers_to_merge = get_all_containers_to_process
        creations_container = merge_containers(containers_to_merge)
        send_creations(creations_container)
        sleep 5
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chillout-0.4.1 lib/chillout/worker.rb
chillout-0.4.0 lib/chillout/worker.rb
chillout-0.3.0 lib/chillout/worker.rb
chillout-0.2.3 lib/chillout/worker.rb