bc73cf692d
We've seen some cases of deadlock when processing notifications, because locks are gathered on the users in different orders. Let's try sticking to a consistent order instead, which should cause the jobs to serialize rather than deadlock.
33 lines
676 B
Ruby
33 lines
676 B
Ruby
class Notifier
|
|
attr_reader :source
|
|
|
|
class << self
|
|
def for(source)
|
|
case source
|
|
when Event
|
|
"Notifier::#{source.eventable.class}EventNotifier".safe_constantize&.new(source)
|
|
when Mention
|
|
MentionNotifier.new(source)
|
|
end
|
|
end
|
|
end
|
|
|
|
def notify
|
|
if should_notify?
|
|
# Processing recipients in order avoids deadlocks if notifications overlap.
|
|
recipients.sort_by(&:id).map do |recipient|
|
|
Notification.create! user: recipient, source: source, creator: creator
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
def initialize(source)
|
|
@source = source
|
|
end
|
|
|
|
def should_notify?
|
|
!creator.system?
|
|
end
|
|
end
|