From f57f0486a3ec3c0d32544658189ee77782e19178 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 4 Oct 2019 11:42:44 +0200 Subject: [PATCH] Performance: When many tickets have reached the reminder or are escalated (more than 1400) processing of those causes a high CPU load and long blocking of background processing. Caching Group permission lookup for users reduces this load. --- app/models/transaction/notification.rb | 10 +++++++++- test/unit/ticket_notification_test.rb | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/models/transaction/notification.rb b/app/models/transaction/notification.rb index 409bb6515..f6593a36f 100644 --- a/app/models/transaction/notification.rb +++ b/app/models/transaction/notification.rb @@ -51,7 +51,7 @@ class Transaction::Notification recipients_reason = {} # loop through all users - possible_recipients = User.group_access(ticket.group_id, 'full').sort_by(&:login) + possible_recipients = possible_recipients_of_group(ticket.group_id) # apply owner if ticket.owner_id != 1 @@ -347,4 +347,12 @@ class Transaction::Notification ) end + def possible_recipients_of_group(group_id) + cache = Cache.get("Transaction::Notification.group_access.full::#{group_id}") + return cache if cache + + possible_recipients = User.group_access(group_id, 'full').sort_by(&:login) + Cache.write("Transaction::Notification.group_access.full::#{group_id}", possible_recipients, expires_in: 20.seconds) + possible_recipients + end end diff --git a/test/unit/ticket_notification_test.rb b/test/unit/ticket_notification_test.rb index 1934dabe0..9b3c1b030 100644 --- a/test/unit/ticket_notification_test.rb +++ b/test/unit/ticket_notification_test.rb @@ -663,6 +663,8 @@ class TicketNotificationTest < ActiveSupport::TestCase @agent1.preferences['notification_config']['group_ids'] = ['-'] @agent2.save! + travel 1.minute # to skip loopup cache in Transaction::Notification + # create ticket in group ApplicationHandleInfo.current = 'scheduler.postmaster' ticket4 = Ticket.create!( @@ -727,6 +729,8 @@ class TicketNotificationTest < ActiveSupport::TestCase @agent2.preferences['notification_config']['group_ids'] = [99] @agent2.save! + travel 1.minute # to skip loopup cache in Transaction::Notification + # create ticket in group ApplicationHandleInfo.current = 'scheduler.postmaster' ticket5 = Ticket.create!( @@ -791,6 +795,8 @@ class TicketNotificationTest < ActiveSupport::TestCase @agent2.preferences['notification_config']['group_ids'] = [999] @agent2.save! + travel 1.minute # to skip loopup cache in Transaction::Notification + # create ticket in group ApplicationHandleInfo.current = 'scheduler.postmaster' ticket6 = Ticket.create!( @@ -868,6 +874,8 @@ class TicketNotificationTest < ActiveSupport::TestCase @agent2.preferences['notification_config']['group_ids'] = [999] @agent2.save! + travel 1.minute # to skip loopup cache in Transaction::Notification + # create ticket in group ApplicationHandleInfo.current = 'scheduler.postmaster' ticket7 = Ticket.create!(