PERF: Call web hook events in a new thread (#7451)

This commit is contained in:
Vinoth Kannan 2019-04-29 14:20:12 +05:30 committed by Sam
parent 82a6cf8afd
commit 09ae12bd5d
1 changed files with 15 additions and 0 deletions

View File

@ -1,6 +1,13 @@
# This is meant to be used by plugins to trigger and listen to events
# So we can execute code when things happen.
class DiscourseEvent
def self.async
@async ||= !Rails.env.test?
end
def self.async=(val)
@async = val
end
# Defaults to a hash where default values are empty sets.
def self.events
@ -8,6 +15,14 @@ class DiscourseEvent
end
def self.trigger(event_name, *params)
if async
Thread.new { call_events(event_name, *params) }
else
call_events(event_name, *params)
end
end
def self.call_events(event_name, *params)
events[event_name].each do |event|
event.call(*params)
end