diff --git a/app/jobs/base.rb b/app/jobs/base.rb index b69302861a3..fdcd1dd55ed 100644 --- a/app/jobs/base.rb +++ b/app/jobs/base.rb @@ -311,7 +311,7 @@ module Jobs hash['queue'] = queue end - klass.client_push(hash) + DB.after_commit { klass.client_push(hash) } else # Otherwise execute the job right away opts.delete(:delay_for) diff --git a/spec/jobs/jobs_spec.rb b/spec/jobs/jobs_spec.rb index 0f3f8d48ca0..5ee5ff09a77 100644 --- a/spec/jobs/jobs_spec.rb +++ b/spec/jobs/jobs_spec.rb @@ -29,6 +29,28 @@ describe Jobs do end end + it "enqueues the job after the current transaction has committed" do + jobs = Jobs::ProcessPost.jobs + expect(jobs.length).to eq(0) + + Jobs.enqueue(:process_post, post_id: 1) + expect(jobs.length).to eq(1) + + ActiveRecord::Base.transaction do + Jobs.enqueue(:process_post, post_id: 1) + expect(jobs.length).to eq(1) + end + expect(jobs.length).to eq(2) + + # Failed transation + ActiveRecord::Base.transaction do + Jobs.enqueue(:process_post, post_id: 1) + raise ActiveRecord::Rollback + end + + expect(jobs.length).to eq(2) # No change + end + it "does not pass current_site_id when 'all_sites' is present" do Sidekiq::Testing.fake! do jobs = Jobs::ProcessPost.jobs