Class: Tootsie::TaskManager

Inherits:
Object
  • Object
show all
Defined in:
lib/tootsie/task_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ TaskManager

Returns a new instance of TaskManager.



9
10
11
12
# File 'lib/tootsie/task_manager.rb', line 9

def initialize(queue)
  @queue = queue
  @logger = Application.get.logger
end

Instance Method Details

#run!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tootsie/task_manager.rb', line 21

def run!
  @logger.info "Ready to process tasks"
  loop do
    begin
      task = @queue.pop(:wait => true)
      if task
        task = task.with_indifferent_access
        type, data = task[:task], task[:data]
        @logger.info("Processing task #{type.inspect}: #{data.inspect}")
        begin
          task_class = Tasks.const_get("#{type.camelcase}Task")
        rescue NameError
          @logger.error("Invalid task encountered on queue: #{task.inspect}")
        else
          task = task_class.new(data)
          task.execute!
        end
      end
    rescue Interrupt, SignalException, SystemExit
      raise
    rescue Exception => exception
      backtrace = exception.backtrace.map { |s| "  #{s}\n" }.join
      @logger.error "Task manager exception: #{exception.class}: #{exception}\n#{backtrace}"
    end
  end
  @logger.info "Task manager done"
end

#schedule(task) ⇒ Object



14
15
16
17
18
19
# File 'lib/tootsie/task_manager.rb', line 14

def schedule(task)
  type = task.class.name.gsub(/^(?:[^:]+::)*(.*?)Task$/, '\1').underscore
  data = task.attributes
  @logger.info("Scheduling task #{type.inspect}: #{data.inspect}")
  @queue.push({:task => type, :data => data})
end