lib/pigeon/task.rb in pigeon-0.4.10 vs lib/pigeon/task.rb in pigeon-0.5.0

- old
+ new

@@ -111,18 +111,11 @@ send(state_method) end end rescue Object => e - @exception = e - - handle_exception(e) rescue nil - - transition_to_state(:failed) unless (self.failed?) - - self.after_failed - self.after_terminated + exeption_received(e) ensure after_state(state) if (terminate) self.after_finished @@ -156,10 +149,23 @@ when 1 @callback.call(state) end end + # This allows a block to be executed in the main event thread where it is + # strictly required. Any exceptions generated by the block will be captured + # and will cause the task to transition to the failed state. + def execute_in_main_thread + @engine.schedule do + begin + yield + rescue Object => e + exeption_received(e) + end + end + end + # Called just after the task is initialized. def after_initialized end # Called before a particular state is executed. @@ -191,7 +197,21 @@ # This defines the behaivor of the intialized state. By default this # simply transitions to the finished state. def state_initialized! transition_to_state(:finished) + end + + # This method is used to handle an exception and run through the proper + # failure handling mechanism. The handle_exception method is provided for + # subclasses to customize. + def exception_received(e) + @exception = e + + handle_exception(e) rescue nil + + transition_to_state(:failed) unless (self.failed?) + + self.after_failed + self.after_terminated end end