chore: Limit conversation resolution Job (#6433)

We will be adding a limit to the resolution job so that it is performed at a spaced interval. This will ensure there won't be a sudden spike in resource usage

fixes: chatwoot/product#707
This commit is contained in:
Sojan Jose
2023-02-13 14:00:52 +05:30
committed by GitHub
parent a06a5a574a
commit 5cbfcfbfa0
4 changed files with 16 additions and 4 deletions

View File

@@ -1,8 +1,9 @@
class Conversations::ResolutionJob < ApplicationJob class Conversations::ResolutionJob < ApplicationJob
queue_as :medium queue_as :low
def perform(account:) 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) resolvable_conversations.each(&:toggle_status)
end end
end end

View File

@@ -63,7 +63,7 @@ class Conversation < ApplicationRecord
scope :assigned, -> { where.not(assignee_id: nil) } scope :assigned, -> { where.not(assignee_id: nil) }
scope :assigned_to, ->(agent) { where(assignee_id: agent.id) } scope :assigned_to, ->(agent) { where(assignee_id: agent.id) }
scope :resolvable, lambda { |auto_resolve_duration| 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) open.where('last_activity_at < ? ', Time.now.utc - auto_resolve_duration.days)
} }

3
lib/limits.rb Normal file
View File

@@ -0,0 +1,3 @@
module Limits
BULK_ACTIONS_LIMIT = 100
end

View File

@@ -9,7 +9,7 @@ RSpec.describe Conversations::ResolutionJob, type: :job do
it 'enqueues the job' do it 'enqueues the job' do
expect { job }.to have_enqueued_job(described_class) expect { job }.to have_enqueued_job(described_class)
.with(account) .with(account)
.on_queue('medium') .on_queue('low')
end end
it 'does nothing when there is no auto resolve duration' do 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) described_class.perform_now(account: account)
expect(conversation.reload.status).to eq('resolved') expect(conversation.reload.status).to eq('resolved')
end 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 end