Skip to content

Commit

Permalink
Fixed issue #2108 - 30 concurrent agents + each of them 30 overviews …
Browse files Browse the repository at this point in the history
…- script/scheduler.rb takes 100% CPU time - background jobs cannot be processed.
  • Loading branch information
martini committed Aug 6, 2018
1 parent fb0ef19 commit ce5f5a0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
11 changes: 7 additions & 4 deletions app/models/ticket/screen_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,13 @@ def self.attributes_to_change(params)
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

Expand Down
6 changes: 4 additions & 2 deletions lib/sessions/backend/base.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,14 +10,15 @@ def initialize(user, asset_lookup, client, client_id, ttl = 10)
@ttl = ttl
@asset_lookup = asset_lookup
@last_change = nil
@time_now = Time.zone.now.to_i
end

def asset_push(record, assets)
class_name = record.class.to_s
@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
Expand All @@ -32,7 +34,7 @@ def asset_needed_by_updated_at?(class_name, record_id, updated_at)
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

Expand Down
1 change: 1 addition & 0 deletions lib/sessions/backend/collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize(user, asset_lookup, client, client_id, ttl = 10)
@ttl = ttl
@asset_lookup = asset_lookup
@backends = backend
@time_now = Time.zone.now.to_i
end

def push
Expand Down
3 changes: 3 additions & 0 deletions lib/sessions/backend/collections/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -10,6 +11,7 @@ def initialize(user, asset_lookup, client, client_id, ttl)
@ttl = ttl
@asset_lookup = asset_lookup
@last_change = nil
@time_now = Time.zone.now.to_i
end

def load
Expand Down Expand Up @@ -53,6 +55,7 @@ def push
end

# collect assets
@time_now = Time.zone.now.to_i
assets = {}
items.each do |item|
next if !asset_needed?(item)
Expand Down
4 changes: 3 additions & 1 deletion lib/sessions/backend/ticket_overview_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 8)
@last_overview = {}
@last_overview_change = nil
@last_ticket_change = nil
@time_now = Time.zone.now.to_i
end

def load
Expand Down Expand Up @@ -87,6 +88,8 @@ def push
)
end

@time_now = Time.zone.now.to_i

# push overviews
results = []
index_and_lists.each do |data|
Expand Down Expand Up @@ -114,7 +117,6 @@ def push
}
results.push result
else

@client.log "push overview_list #{overview.link} for user #{@user.id}"

# send update to browser
Expand Down
12 changes: 12 additions & 0 deletions test/unit/session_basic_ticket_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})

Expand All @@ -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, {})

Expand Down

0 comments on commit ce5f5a0

Please sign in to comment.