From ce5f5a035eb804d7c43adb73a6038d0587e8b649 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 6 Aug 2018 11:43:40 +0200 Subject: [PATCH] Fixed issue #2108 - 30 concurrent agents + each of them 30 overviews - script/scheduler.rb takes 100% CPU time - background jobs cannot be processed. --- app/models/ticket/screen_options.rb | 11 +++++++---- lib/sessions/backend/base.rb | 6 ++++-- lib/sessions/backend/collections.rb | 1 + lib/sessions/backend/collections/base.rb | 3 +++ lib/sessions/backend/ticket_overview_list.rb | 4 +++- test/unit/session_basic_ticket_test.rb | 12 ++++++++++++ 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app/models/ticket/screen_options.rb b/app/models/ticket/screen_options.rb index de78b9015..4e7782516 100644 --- a/app/models/ticket/screen_options.rb +++ b/app/models/ticket/screen_options.rb @@ -115,10 +115,13 @@ returns group_agent_roles_ids = Role.joins(', roles_groups').where("roles.id = roles_groups.role_id AND roles_groups.access = 'full' AND roles_groups.group_id = ? AND roles.id IN (?)", group.id, agent_role_ids).pluck(:id) group_agent_role_user_ids = User.joins(:roles).where(roles: { id: group_agent_roles_ids }).pluck(:id) - User.where(id: group_agent_user_ids.concat(group_agent_role_user_ids).uniq, active: true).each do |user| - dependencies[:group_id][group.id][:owner_id].push user.id - next if agents[user.id] - agents[user.id] = true + User.where(id: group_agent_user_ids.concat(group_agent_role_user_ids).uniq, active: true).pluck(:id).each do |user_id| + dependencies[:group_id][group.id][:owner_id].push user_id + next if agents[user_id] + agents[user_id] = true + next if assets[:User] && assets[:User][user_id] + user = User.lookup(id: user_id) + next if !user assets = user.assets(assets) end diff --git a/lib/sessions/backend/base.rb b/lib/sessions/backend/base.rb index e7e8f9eaf..f84b156e1 100644 --- a/lib/sessions/backend/base.rb +++ b/lib/sessions/backend/base.rb @@ -1,6 +1,7 @@ class Sessions::Backend::Base attr_writer :user + attr_writer :time_now def initialize(user, asset_lookup, client, client_id, ttl = 10) @user = user @@ -9,6 +10,7 @@ class Sessions::Backend::Base @ttl = ttl @asset_lookup = asset_lookup @last_change = nil + @time_now = Time.zone.now.to_i end def asset_push(record, assets) @@ -16,7 +18,7 @@ class Sessions::Backend::Base @asset_lookup[class_name] ||= {} @asset_lookup[class_name][record.id] = { updated_at: record.updated_at, - pushed_at: Time.zone.now, + pushed_at: @time_now, } record.assets(assets) end @@ -32,7 +34,7 @@ class Sessions::Backend::Base return true if @asset_lookup[class_name][record_id].blank? return true if @asset_lookup[class_name][record_id][:updated_at] < updated_at return true if @asset_lookup[class_name][record_id][:pushed_at].blank? - return true if @asset_lookup[class_name][record_id][:pushed_at] < Time.zone.now - 2.hours + return true if @asset_lookup[class_name][record_id][:pushed_at] < @time_now - 7200 false end diff --git a/lib/sessions/backend/collections.rb b/lib/sessions/backend/collections.rb index 1f87923ec..06ccf631d 100644 --- a/lib/sessions/backend/collections.rb +++ b/lib/sessions/backend/collections.rb @@ -7,6 +7,7 @@ class Sessions::Backend::Collections < Sessions::Backend::Base @ttl = ttl @asset_lookup = asset_lookup @backends = backend + @time_now = Time.zone.now.to_i end def push diff --git a/lib/sessions/backend/collections/base.rb b/lib/sessions/backend/collections/base.rb index 1ca5e1bab..c965d8a92 100644 --- a/lib/sessions/backend/collections/base.rb +++ b/lib/sessions/backend/collections/base.rb @@ -2,6 +2,7 @@ class Sessions::Backend::Collections::Base < Sessions::Backend::Base class << self; attr_accessor :model, :permissions end attr_writer :user + attr_writer :time_now def initialize(user, asset_lookup, client, client_id, ttl) @user = user @@ -10,6 +11,7 @@ class Sessions::Backend::Collections::Base < Sessions::Backend::Base @ttl = ttl @asset_lookup = asset_lookup @last_change = nil + @time_now = Time.zone.now.to_i end def load @@ -53,6 +55,7 @@ class Sessions::Backend::Collections::Base < Sessions::Backend::Base end # collect assets + @time_now = Time.zone.now.to_i assets = {} items.each do |item| next if !asset_needed?(item) diff --git a/lib/sessions/backend/ticket_overview_list.rb b/lib/sessions/backend/ticket_overview_list.rb index d786619f2..ac645ff51 100644 --- a/lib/sessions/backend/ticket_overview_list.rb +++ b/lib/sessions/backend/ticket_overview_list.rb @@ -15,6 +15,7 @@ class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base @last_overview = {} @last_overview_change = nil @last_ticket_change = nil + @time_now = Time.zone.now.to_i end def load @@ -87,6 +88,8 @@ class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base ) end + @time_now = Time.zone.now.to_i + # push overviews results = [] index_and_lists.each do |data| @@ -114,7 +117,6 @@ class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base } results.push result else - @client.log "push overview_list #{overview.link} for user #{@user.id}" # send update to browser diff --git a/test/unit/session_basic_ticket_test.rb b/test/unit/session_basic_ticket_test.rb index 20706036e..ddc9ca4c9 100644 --- a/test/unit/session_basic_ticket_test.rb +++ b/test/unit/session_basic_ticket_test.rb @@ -55,20 +55,29 @@ class SessionBasicTicketTest < ActiveSupport::TestCase travel 120.minutes + client1.time_now = Time.zone.now.to_i assert(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at)) client1.asset_push(ticket, {}) + + client2.time_now = Time.zone.now.to_i assert(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at)) client2.asset_push(ticket, {}) + client1.time_now = Time.zone.now.to_i assert_not(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at)) client1.asset_push(ticket, {}) + + client2.time_now = Time.zone.now.to_i assert_not(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at)) client2.asset_push(ticket, {}) ticket.touch + client1.time_now = Time.zone.now.to_i assert(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at)) client1.asset_push(ticket, {}) + + client2.time_now = Time.zone.now.to_i assert(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at)) client2.asset_push(ticket, {}) @@ -77,8 +86,11 @@ class SessionBasicTicketTest < ActiveSupport::TestCase travel 125.minutes + client1.time_now = Time.zone.now.to_i assert(client1.asset_needed?(ticket)) client1.asset_push(ticket, {}) + + client2.time_now = Time.zone.now.to_i assert(client2.asset_needed?(ticket)) client2.asset_push(ticket, {})