1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Increment execution count before deserialize arguments

Currently, the execution count increments after deserializes arguments.
Therefore, if an error occurs with deserialize, it retries indefinitely.

In order to prevent this, the count is moved before deserialize.
Fixes #33344.
This commit is contained in:
yuuji.yaginuma 2018-07-25 17:30:38 +09:00 committed by Yuji Yaginuma
parent d6688892f6
commit e49fb7921e
3 changed files with 12 additions and 3 deletions

View file

@ -31,11 +31,11 @@ module ActiveJob
# #
# MyJob.new(*args).perform_now # MyJob.new(*args).perform_now
def perform_now def perform_now
# Guard against jobs that were persisted before we started counting executions by zeroing out nil counters
self.executions = (executions || 0) + 1
deserialize_arguments_if_needed deserialize_arguments_if_needed
run_callbacks :perform do run_callbacks :perform do
# Guard against jobs that were persisted before we started counting executions by zeroing out nil counters
self.executions = (executions || 0) + 1
perform(*arguments) perform(*arguments)
end end
rescue => exception rescue => exception

View file

@ -2,6 +2,7 @@
require "helper" require "helper"
require "jobs/retry_job" require "jobs/retry_job"
require "models/person"
class ExceptionsTest < ActiveJob::TestCase class ExceptionsTest < ActiveJob::TestCase
setup do setup do
@ -131,4 +132,11 @@ class ExceptionsTest < ActiveJob::TestCase
assert_equal [ "Raised SecondDiscardableErrorOfTwo for the 1st time" ], JobBuffer.values assert_equal [ "Raised SecondDiscardableErrorOfTwo for the 1st time" ], JobBuffer.values
end end
end end
test "successfully retry job throwing DeserializationError" do
perform_enqueued_jobs do
RetryJob.perform_later Person.new(404), 5
assert_equal ["Raised ActiveJob::DeserializationError for the 5 time"], JobBuffer.values
end
end
end end

View file

@ -24,6 +24,7 @@ class RetryJob < ActiveJob::Base
retry_on ExponentialWaitTenAttemptsError, wait: :exponentially_longer, attempts: 10 retry_on ExponentialWaitTenAttemptsError, wait: :exponentially_longer, attempts: 10
retry_on CustomWaitTenAttemptsError, wait: ->(executions) { executions * 2 }, attempts: 10 retry_on CustomWaitTenAttemptsError, wait: ->(executions) { executions * 2 }, attempts: 10
retry_on(CustomCatchError) { |job, error| JobBuffer.add("Dealt with a job that failed to retry in a custom way after #{job.arguments.second} attempts. Message: #{error.message}") } retry_on(CustomCatchError) { |job, error| JobBuffer.add("Dealt with a job that failed to retry in a custom way after #{job.arguments.second} attempts. Message: #{error.message}") }
retry_on(ActiveJob::DeserializationError) { |job, error| JobBuffer.add("Raised #{error.class} for the #{job.executions} time") }
discard_on DiscardableError discard_on DiscardableError
discard_on FirstDiscardableErrorOfTwo, SecondDiscardableErrorOfTwo discard_on FirstDiscardableErrorOfTwo, SecondDiscardableErrorOfTwo