Method: Chore.monitor

Defined in:
lib/chore.rb

.monitor(task, opts = {}, &code) ⇒ Object

Automatically run Chore.start, execute a code block, and automatically run Chore.finish (or Chore.fail in the case of an exception) when the block finishes.

All options from .start, .finish, and .fail may be passed in as options.

In addition to normal opts, :pop => true will automatically remove the task from the store



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/chore.rb', line 75

def self.monitor task, opts={}, &code
  pop = false
  if opts[:pop]
    pop = true
    opts.delete(:pop)
  end
  
  Chore.start(task, opts)
  begin
    code.call()
    if pop
      Chore.pop(task)
    else
      Chore.finish(task)
    end
  rescue Exception => ex
    msg = [ex.class, ex.message]
    msg <<  ([''] + ex.backtrace[0..4]) if ex.backtrace.length > 0
    msg << "..." if ex.backtrace.length > 5
    msg = msg.join("\n")

    Chore.fail(task, :error => "#{msg}")
    raise
  end
end