From ef4aff07a41f1249baade695e68c75a8db3ded58 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 22 May 2014 20:35:02 +0200 Subject: Add exception handling with built-in retry options --- lib/active_job/enqueuing.rb | 12 ++++++++++++ lib/active_job/performing.rb | 9 +++++++++ test/cases/callbacks_test.rb | 1 - test/cases/rescue_test.rb | 16 ++++++++++++++++ test/jobs/rescue_job.rb | 15 +++++++++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/cases/rescue_test.rb create mode 100644 test/jobs/rescue_job.rb diff --git a/lib/active_job/enqueuing.rb b/lib/active_job/enqueuing.rb index 4ff7ec7e7f..0f094fb624 100644 --- a/lib/active_job/enqueuing.rb +++ b/lib/active_job/enqueuing.rb @@ -55,5 +55,17 @@ module ActiveJob def initialize(arguments = nil) @arguments = arguments end + + def retry_now + self.class.enqueue *arguments + end + + def retry_in(interval) + self.class.enqueue_in interval, *arguments + end + + def retry_at(timestamp) + self.class.enqueue_at timestamp, *arguments + end end end diff --git a/lib/active_job/performing.rb b/lib/active_job/performing.rb index ef17a39f85..c3f57873e5 100644 --- a/lib/active_job/performing.rb +++ b/lib/active_job/performing.rb @@ -1,13 +1,22 @@ +require 'active_support/rescuable' require 'active_job/arguments' module ActiveJob module Performing + extend ActiveSupport::Concern + + included do + include ActiveSupport::Rescuable + end + def perform_with_hooks(*serialized_args) self.arguments = Arguments.deserialize(serialized_args) run_callbacks :perform do perform *arguments end + rescue => exception + rescue_with_handler(exception) end def perform(*) diff --git a/test/cases/callbacks_test.rb b/test/cases/callbacks_test.rb index 06e2ea4a2e..104519dcb7 100644 --- a/test/cases/callbacks_test.rb +++ b/test/cases/callbacks_test.rb @@ -1,5 +1,4 @@ require 'helper' -require 'active_job/arguments' require 'jobs/callback_job' require 'active_support/core_ext/object/inclusion' diff --git a/test/cases/rescue_test.rb b/test/cases/rescue_test.rb new file mode 100644 index 0000000000..ca4c69f91b --- /dev/null +++ b/test/cases/rescue_test.rb @@ -0,0 +1,16 @@ +require 'helper' +require 'jobs/rescue_job' + +require 'active_support/core_ext/object/inclusion' + +class RescueTest < ActiveSupport::TestCase + setup do + $BUFFER = [] + end + + test 'rescue perform exception with retry' do + job = RescueJob.new + job.perform_with_hooks("david") + assert_equal [ "rescued from StandardError", "performed beautifully" ], $BUFFER + end +end diff --git a/test/jobs/rescue_job.rb b/test/jobs/rescue_job.rb new file mode 100644 index 0000000000..66b28d768e --- /dev/null +++ b/test/jobs/rescue_job.rb @@ -0,0 +1,15 @@ +class RescueJob < ActiveJob::Base + rescue_from(StandardError) do + $BUFFER << "rescued from StandardError" + arguments[0] = "DIFFERENT!" + retry_now + end + + def perform(person = "david") + if person == "david" + raise "Hair too good" + else + $BUFFER << "performed beautifully" + end + end +end -- cgit v1.2.3