FIX: thread safety for active automation tracking (#27044)

This commit is contained in:
Sam 2024-05-16 13:34:24 +10:00 committed by GitHub
parent 7442cf3f62
commit e90e6e8f86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View File

@ -30,11 +30,11 @@ module ::DiscourseAutomation
USER_GROUP_MEMBERSHIP_THROUGH_BADGE_BULK_MODIFY_START_COUNT = 1000 USER_GROUP_MEMBERSHIP_THROUGH_BADGE_BULK_MODIFY_START_COUNT = 1000
def self.set_active_automation(id) def self.set_active_automation(id)
@active_automation_id = id Thread.current[:active_automation_id] = id
end end
def self.get_active_automation def self.get_active_automation
@active_automation_id Thread.current[:active_automation_id]
end end
end end

View File

@ -16,6 +16,20 @@ describe DiscourseAutomation::Triggerable do
fab!(:automation) { Fabricate(:automation, trigger: "foo") } fab!(:automation) { Fabricate(:automation, trigger: "foo") }
describe "active automation thread safety" do
after { DiscourseAutomation.set_active_automation(nil) }
it "ensurese thread safety when setting automation id" do
DiscourseAutomation.set_active_automation(10)
thread = Thread.new { DiscourseAutomation.get_active_automation }
thread.join
expect(thread.value).to eq(nil)
expect(DiscourseAutomation.get_active_automation).to eq(10)
end
end
describe "#setting" do describe "#setting" do
before { DiscourseAutomation::Triggerable.add("foo") { setting :bar, :baz } } before { DiscourseAutomation::Triggerable.add("foo") { setting :bar, :baz } }