Class: Chillout::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/chillout/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher, queue, logger, container_class = CreationsContainer) ⇒ Worker

Returns a new instance of Worker.



5
6
7
8
9
10
# File 'lib/chillout/worker.rb', line 5

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

Instance Attribute Details

#dispatcherObject (readonly)

Returns the value of attribute dispatcher.



3
4
5
# File 'lib/chillout/worker.rb', line 3

def dispatcher
  @dispatcher
end

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/chillout/worker.rb', line 3

def logger
  @logger
end

#queueObject (readonly)

Returns the value of attribute queue.



3
4
5
# File 'lib/chillout/worker.rb', line 3

def queue
  @queue
end

Instance Method Details

#get_all_containers_to_processObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/chillout/worker.rb', line 12

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

#merge_containers(containers_to_merge) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/chillout/worker.rb', line 27

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

#runObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chillout/worker.rb', line 49

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

#send_creations(creations_container) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/chillout/worker.rb', line 35

def send_creations(creations_container)
  logger.info "Trying to send creations"
  dispatcher.send_creations(creations_container)
  logger.info "Metrics sent"
rescue Dispatcher::SendCreationsFailed
  queue << creations_container
  logger.error "Sending metrics failed"
end

#send_startup_messageObject



44
45
46
47
# File 'lib/chillout/worker.rb', line 44

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