FIX: mutex to avoid race condition with double notify

This commit is contained in:
Sam 2017-05-26 17:04:40 -04:00
parent 81c18fc0bd
commit a7e65d98a9

View File

@ -1,3 +1,5 @@
require_dependency 'distributed_mutex'
class PostAlerter class PostAlerter
def self.post_created(post) def self.post_created(post)
@ -222,20 +224,22 @@ class PostAlerter
notification_type = Notification.types[:group_message_summary] notification_type = Notification.types[:group_message_summary]
Notification.where(notification_type: notification_type, user_id: user.id).each do |n| DistributedMutex.synchronize("group_message_notify_#{user.id}") do
n.destroy if n.data_hash[:group_id] == stat[:group_id] Notification.where(notification_type: notification_type, user_id: user.id).each do |n|
end n.destroy if n.data_hash[:group_id] == stat[:group_id]
end
Notification.create( Notification.create(
notification_type: notification_type, notification_type: notification_type,
user_id: user.id, user_id: user.id,
data: { data: {
group_id: stat[:group_id], group_id: stat[:group_id],
group_name: stat[:group_name], group_name: stat[:group_name],
inbox_count: stat[:inbox_count], inbox_count: stat[:inbox_count],
username: user.username_lower username: user.username_lower
}.to_json }.to_json
) )
end
# TODO decide if it makes sense to also publish a desktop notification # TODO decide if it makes sense to also publish a desktop notification
end end