Browse Source

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.

Martin Edenhofer 5 years ago
parent
commit
f57f0486a3
2 changed files with 17 additions and 1 deletions
  1. 9 1
      app/models/transaction/notification.rb
  2. 8 0
      test/unit/ticket_notification_test.rb

+ 9 - 1
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

+ 8 - 0
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!(