diff --git a/app/jobs/conversations/resolution_job.rb b/app/jobs/conversations/resolution_job.rb index 957c8bace..164a82265 100644 --- a/app/jobs/conversations/resolution_job.rb +++ b/app/jobs/conversations/resolution_job.rb @@ -1,8 +1,9 @@ class Conversations::ResolutionJob < ApplicationJob - queue_as :medium + queue_as :low def perform(account:) - resolvable_conversations = account.conversations.resolvable(account.auto_resolve_duration) + # limiting the number of conversations to be resolved to avoid any performance issues + resolvable_conversations = account.conversations.resolvable(account.auto_resolve_duration).limit(Limits::BULK_ACTIONS_LIMIT) resolvable_conversations.each(&:toggle_status) end end diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 6b6c2dc8d..e2c1d43a4 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -63,7 +63,7 @@ class Conversation < ApplicationRecord scope :assigned, -> { where.not(assignee_id: nil) } scope :assigned_to, ->(agent) { where(assignee_id: agent.id) } scope :resolvable, lambda { |auto_resolve_duration| - return [] if auto_resolve_duration.to_i.zero? + return none if auto_resolve_duration.to_i.zero? open.where('last_activity_at < ? ', Time.now.utc - auto_resolve_duration.days) } diff --git a/lib/limits.rb b/lib/limits.rb new file mode 100644 index 000000000..1e2d36447 --- /dev/null +++ b/lib/limits.rb @@ -0,0 +1,3 @@ +module Limits + BULK_ACTIONS_LIMIT = 100 +end diff --git a/spec/jobs/conversations/resolution_job_spec.rb b/spec/jobs/conversations/resolution_job_spec.rb index 0482f8bbf..4ed26c1d0 100644 --- a/spec/jobs/conversations/resolution_job_spec.rb +++ b/spec/jobs/conversations/resolution_job_spec.rb @@ -9,7 +9,7 @@ RSpec.describe Conversations::ResolutionJob, type: :job do it 'enqueues the job' do expect { job }.to have_enqueued_job(described_class) .with(account) - .on_queue('medium') + .on_queue('low') end it 'does nothing when there is no auto resolve duration' do @@ -23,4 +23,12 @@ RSpec.describe Conversations::ResolutionJob, type: :job do described_class.perform_now(account: account) expect(conversation.reload.status).to eq('resolved') end + + it 'resolved only a limited number of conversations in a single execution' do + stub_const('Limits::BULK_ACTIONS_LIMIT', 2) + account.update(auto_resolve_duration: 10) + create_list(:conversation, 3, account: account, last_activity_at: 13.days.ago) + described_class.perform_now(account: account) + expect(account.conversations.resolved.count).to eq(Limits::BULK_ACTIONS_LIMIT) + end end