FIX: Avoid race condition when enqueuing job.

This commit is contained in:
Guo Xiang Tan 2018-09-20 11:24:01 +08:00
parent dfbc93aa2e
commit 195bd02fce
2 changed files with 26 additions and 1 deletions

View File

@ -25,7 +25,6 @@ class Group < ActiveRecord::Base
before_save :cook_bio before_save :cook_bio
after_save :destroy_deletions after_save :destroy_deletions
after_save :automatic_group_membership
after_save :update_primary_group after_save :update_primary_group
after_save :update_title after_save :update_title
@ -35,6 +34,7 @@ class Group < ActiveRecord::Base
after_save :expire_cache after_save :expire_cache
after_destroy :expire_cache after_destroy :expire_cache
after_commit :automatic_group_membership, on: [:create, :update]
after_commit :trigger_group_created_event, on: :create after_commit :trigger_group_created_event, on: :create
after_commit :trigger_group_updated_event, on: :update after_commit :trigger_group_updated_event, on: :update
after_commit :trigger_group_destroyed_event, on: :destroy after_commit :trigger_group_destroyed_event, on: :destroy

View File

@ -786,4 +786,29 @@ describe Group do
group.reload group.reload
expect(group.has_messages?).to eq true expect(group.has_messages?).to eq true
end end
describe '#automatic_group_membership' do
describe 'for a automatic_membership_retroactive group' do
let(:group) { Fabricate(:group, automatic_membership_retroactive: true) }
it "should be triggered on create and update" do
expect { group }
.to change { Jobs::AutomaticGroupMembership.jobs.size }.by(1)
job = Jobs::AutomaticGroupMembership.jobs.last
expect(job["args"].first["group_id"]).to eq(group.id)
Jobs::AutomaticGroupMembership.jobs.clear
expect do
group.update!(name: 'asdiaksjdias')
end.to change { Jobs::AutomaticGroupMembership.jobs.size }.by(1)
job = Jobs::AutomaticGroupMembership.jobs.last
expect(job["args"].first["group_id"]).to eq(group.id)
end
end
end
end end