Module: Mumukit::Nuntius::TaskConsumer

Included in:
EventConsumer, JobConsumer
Defined in:
lib/mumukit/nuntius/task_consumer.rb

Defined Under Namespace

Classes: Builder

Instance Method Summary collapse

Instance Method Details

#handle(&block) ⇒ Object



33
34
35
# File 'lib/mumukit/nuntius/task_consumer.rb', line 33

def handle(&block)
  register_handlers! builder.new.tap { |it| it.instance_eval(&block) }.build
end

#handle_tasks!(properties, body) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mumukit/nuntius/task_consumer.rb', line 56

def handle_tasks!(properties, body)
  return if skip_process? body
  task = properties[:type]
  if handles? task
    handlers[task].call body[:data].except(:sender)
  else
    log_unknown_task task
  end
rescue => e
  log_exception(task, e, body[:data])
end

#handled_tasksObject



37
38
39
# File 'lib/mumukit/nuntius/task_consumer.rb', line 37

def handled_tasks
  handlers.keys
end

#handlersObject



29
30
31
# File 'lib/mumukit/nuntius/task_consumer.rb', line 29

def handlers
  @@handlers ||= {}
end

#handles?(task) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/mumukit/nuntius/task_consumer.rb', line 52

def handles?(task)
  handlers.include? task
end

#log_exception(task, e, body) ⇒ Object



72
73
74
# File 'lib/mumukit/nuntius/task_consumer.rb', line 72

def log_exception(task, e, body)
  Mumukit::Nuntius::Logger.error "Failed to proccess #{task}, error was: #{e}, body was: #{body}"
end

#log_unknown_task(task) ⇒ Object



68
69
70
# File 'lib/mumukit/nuntius/task_consumer.rb', line 68

def log_unknown_task(task)
  Mumukit::Nuntius::Logger.info "Unhandled #{task_type}: #{task} does not exists."
end

#register_handlers!(handlers) ⇒ Object



41
42
43
# File 'lib/mumukit/nuntius/task_consumer.rb', line 41

def register_handlers!(handlers)
  @@handlers = handlers
end

#start!Object



45
46
47
48
49
50
# File 'lib/mumukit/nuntius/task_consumer.rb', line 45

def start!
  queue_name = "#{Mumukit::Nuntius.config.app_name}-#{tasks_type}"
  Mumukit::Nuntius::Consumer.start queue_name, tasks_type do |_delivery_info, properties, body|
    handle_tasks!(properties, body)
  end
end