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(dispatcher, queue, logger, container_class = CreationsContainer) ⇒ Worker
constructor
A new instance of Worker.
- #merge_containers(containers_to_merge) ⇒ Object
- #run ⇒ Object
- #send_creations(creations_container) ⇒ Object
- #send_startup_message ⇒ Object
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
#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
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 |
#run ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/chillout/worker.rb', line 49 def run logger.info "Worker started" 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_message ⇒ Object
44 45 46 47 |
# File 'lib/chillout/worker.rb', line 44 def dispatcher. logger.info "Sending startup message" end |