FIX: Reviewable counts were not updating properly

Sometimes sidekiq is so fast that it starts jobs before transactions
have comitted. This patch moves the message bus stuff until after things
have comitted.
This commit is contained in:
Robin Ward
2019-04-04 16:07:34 -04:00
parent 2055804e95
commit df48c657fd
2 changed files with 9 additions and 5 deletions

View File

@ -29,9 +29,12 @@ class Reviewable < ActiveRecord::Base
has_many :reviewable_scores has_many :reviewable_scores
after_create do after_create do
log_history(:created, created_by)
end
after_commit do
DiscourseEvent.trigger(:reviewable_created, self) DiscourseEvent.trigger(:reviewable_created, self)
Jobs.enqueue(:notify_reviewable, reviewable_id: self.id) if pending? Jobs.enqueue(:notify_reviewable, reviewable_id: self.id) if pending?
log_history(:created, created_by)
end end
def self.statuses def self.statuses
@ -208,17 +211,20 @@ class Reviewable < ActiveRecord::Base
raise InvalidAction.new(action_id, self.class) unless respond_to?(perform_method) raise InvalidAction.new(action_id, self.class) unless respond_to?(perform_method)
result = nil result = nil
update_count = false
Reviewable.transaction do Reviewable.transaction do
increment_version!(args[:version]) increment_version!(args[:version])
result = send(perform_method, performed_by, args) result = send(perform_method, performed_by, args)
if result.success? if result.success?
transition_to(result.transition_to, performed_by) if result.transition_to update_count = transition_to(result.transition_to, performed_by) if result.transition_to
update_flag_stats(**result.update_flag_stats) if result.update_flag_stats update_flag_stats(**result.update_flag_stats) if result.update_flag_stats
recalculate_score if result.recalculate_score recalculate_score if result.recalculate_score
end end
end end
Jobs.enqueue(:notify_reviewable, reviewable_id: self.id) if update_count
result result
end end
@ -239,7 +245,7 @@ class Reviewable < ActiveRecord::Base
) )
end end
Jobs.enqueue(:notify_reviewable, reviewable_id: self.id) if was_pending was_pending
end end
def post_options def post_options

View File

@ -52,7 +52,6 @@ RSpec.describe ReviewableFlaggedPost, type: :model do
expect(reviewable.actions_for(guardian).has?(:delete_and_ignore_replies)).to eq(true) expect(reviewable.actions_for(guardian).has?(:delete_and_ignore_replies)).to eq(true)
end end
it "returns appropriate actions for a hidden post" do it "returns appropriate actions for a hidden post" do
post.update(hidden: true, hidden_at: Time.now) post.update(hidden: true, hidden_at: Time.now)
expect(reviewable.actions_for(guardian).has?(:agree_and_hide)).to eq(false) expect(reviewable.actions_for(guardian).has?(:agree_and_hide)).to eq(false)
@ -162,7 +161,6 @@ RSpec.describe ReviewableFlaggedPost, type: :model do
expect(reply.reload.deleted_at).to be_present expect(reply.reload.deleted_at).to be_present
end end
it "disagrees with the flags" do it "disagrees with the flags" do
reviewable.perform(moderator, :disagree) reviewable.perform(moderator, :disagree)
expect(reviewable).to be_rejected expect(reviewable).to be_rejected