Skip to content
Permalink
Browse files

Follow up - 2c8825b - ActiveJobLock lock update fails due to ActiveRe…

…cord::Deadlocked "Mysql2::Error: Deadlock found when trying to get lock; try restarting transaction" exception.
  • Loading branch information
thorsteneckel committed Nov 29, 2019
1 parent 4e8cf20 commit 47778f2d2fb27745d52c1221739ecc5d2f8af667
Showing with 25 additions and 0 deletions.
  1. +4 −0 app/jobs/concerns/has_active_job_lock.rb
  2. +21 −0 spec/jobs/concerns/has_active_job_lock_spec.rb
@@ -95,6 +95,10 @@ def in_active_job_lock_transaction
# but it's safe to retry as described in the docs:
# https://www.postgresql.org/docs/10/transaction-iso.html
e.message.include?('PG::TRSerializationFailure') ? retry : raise
rescue ActiveRecord::Deadlocked => e
# MySQL handles lock race condition differently and raises a Deadlock exception:
# Mysql2::Error: Deadlock found when trying to get lock; try restarting transaction
e.message.include?('Mysql2::Error: Deadlock found when trying to get lock') ? retry : raise
rescue ActiveRecord::RecordNotUnique
existing_active_job_lock!
end
@@ -121,6 +121,27 @@ def lock_key
expect(exception_raised).to be true
end
end

context "when ActiveRecord::Deadlocked 'Mysql2::Error: Deadlock found when trying to get lock; try restarting transaction' is raised" do

it 'retries execution until succeed' do
allow(ActiveRecord::Base.connection).to receive(:open_transactions).and_return(0)
allow(ActiveJobLock).to receive(:transaction).and_call_original
exception_raised = false
allow(ActiveJobLock).to receive(:transaction).with(isolation: :serializable) do |&block|

if !exception_raised
exception_raised = true
raise ActiveRecord::Deadlocked, 'Mysql2::Error: Deadlock found when trying to get lock; try restarting transaction'
end

block.call
end

expect { job_class.perform_later }.to have_enqueued_job(job_class).exactly(:once)
expect(exception_raised).to be true
end
end
end

include_examples 'handle locking of jobs'

0 comments on commit 47778f2

Please sign in to comment.
You can’t perform that action at this time.