Class: Chillout::Worker
- Inherits:
-
Object
- Object
- Chillout::Worker
- Defined in:
- lib/chillout/worker.rb
Instance Attribute Summary collapse
-
#dispatcher ⇒ Object
readonly
Returns the value of attribute dispatcher.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
Instance Method Summary collapse
- #get_all_containers_to_process ⇒ Object
-
#initialize(max_queue, dispatcher, queue, logger, container_class = CreationsContainer) ⇒ Worker
constructor
A new instance of Worker.
- #merge_containers(containers_to_merge) ⇒ Object
- #run ⇒ Object
- #send_measurements(measurements) ⇒ Object
- #send_startup_message ⇒ Object
Constructor Details
#initialize(max_queue, dispatcher, queue, logger, container_class = CreationsContainer) ⇒ Worker
Returns a new instance of Worker.
5 6 7 8 9 10 11 |
# File 'lib/chillout/worker.rb', line 5 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 |
Instance Attribute Details
#dispatcher ⇒ Object (readonly)
Returns the value of attribute dispatcher.
3 4 5 |
# File 'lib/chillout/worker.rb', line 3 def dispatcher @dispatcher end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
3 4 5 |
# File 'lib/chillout/worker.rb', line 3 def logger @logger end |
#queue ⇒ Object (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_process ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/chillout/worker.rb', line 13 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 |
#merge_containers(containers_to_merge) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/chillout/worker.rb', line 28 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 |
#run ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/chillout/worker.rb', line 51 def run logger.info "Worker started" loop do containers_to_merge = get_all_containers_to_process measurements = merge_containers(containers_to_merge) send_measurements(measurements) sleep 5 end end |
#send_measurements(measurements) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/chillout/worker.rb', line 38 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 |
#send_startup_message ⇒ Object
46 47 48 49 |
# File 'lib/chillout/worker.rb', line 46 def dispatcher. logger.debug "Sending startup message" end |