Module: LoggedTask

Defined in:
lib/logged_task.rb

Defined Under Namespace

Classes: TaskLogger

Constant Summary collapse

@@task_number =
0
@@root_task_number =
nil
@@loggers =
[]

Class Method Summary collapse

Class Method Details

.define(*args, &block) ⇒ Object

Parameters:

  • args (Array)
  • block (Block)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/logged_task.rb', line 9

def self.define(*args, &block)
  task_number = @@task_number += 1

  task_sym = "__LOGGED_ROOT_TASK_DETECTOR_#{task_number}".to_sym

  args[0] = { args.first => [] } if args.first.is_a?(Symbol)
  args[0].values[0].unshift(task_sym)

  task = Rake::Task.define_task(*args) do
    begin
      logger = TaskLogger.new(task.name)
      @@loggers << logger
      logger.info("Logged task #{task.name} started...")

      yield logger if block_given?

      logger.info("Logged task #{task.name} completed!")
      if task_number == @@root_task_number
        logger.info('All tasks completed. Dumping summary for each task...')
        @@loggers.each { | logger | logger.summary }
      end
    rescue => exception
      logger.summary
      logger.error("Logged task #{task.name} failed.", exception.backtrace)
      raise
    end
  end

  Rake::Task.define_task(task_sym) { @@root_task_number = task_number unless @@root_task_number }
end