From f92cea144cb7bf156593dfbe07f855a867223cef Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Tue, 20 Feb 2024 21:54:37 +0530 Subject: [PATCH] feat(perf): sla-9 improve perf of TriggerSlasForAccountsJob (#8953) * feat: improve perf of TriggerSlasForAccountsJob --- .../jobs/sla/trigger_slas_for_accounts_job.rb | 2 +- .../sla/trigger_slas_for_accounts_job_spec.rb | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/enterprise/app/jobs/sla/trigger_slas_for_accounts_job.rb b/enterprise/app/jobs/sla/trigger_slas_for_accounts_job.rb index 212c44d94..a2a142430 100644 --- a/enterprise/app/jobs/sla/trigger_slas_for_accounts_job.rb +++ b/enterprise/app/jobs/sla/trigger_slas_for_accounts_job.rb @@ -2,7 +2,7 @@ class Sla::TriggerSlasForAccountsJob < ApplicationJob queue_as :scheduled_jobs def perform - Account.find_each do |account| + Account.joins(:sla_policies).distinct.find_each do |account| Rails.logger.info "Enqueuing ProcessAccountAppliedSlasJob for account #{account.id}" Sla::ProcessAccountAppliedSlasJob.perform_later(account) end diff --git a/spec/enterprise/jobs/sla/trigger_slas_for_accounts_job_spec.rb b/spec/enterprise/jobs/sla/trigger_slas_for_accounts_job_spec.rb index 95650748a..d02e543ba 100644 --- a/spec/enterprise/jobs/sla/trigger_slas_for_accounts_job_spec.rb +++ b/spec/enterprise/jobs/sla/trigger_slas_for_accounts_job_spec.rb @@ -1,16 +1,25 @@ require 'rails_helper' - RSpec.describe Sla::TriggerSlasForAccountsJob do context 'when perform is called' do - let(:account) { create(:account) } + let(:account_with_sla) { create(:account) } + let(:account_without_sla) { create(:account) } + + before do + create(:sla_policy, account: account_with_sla) + end it 'enqueues the job' do expect { described_class.perform_later }.to have_enqueued_job(described_class) .on_queue('scheduled_jobs') end - it 'calls the ProcessAccountAppliedSlasJob' do - expect(Sla::ProcessAccountAppliedSlasJob).to receive(:perform_later).with(account).and_call_original + it 'calls the ProcessAccountAppliedSlasJob for accounts with SLA' do + expect(Sla::ProcessAccountAppliedSlasJob).to receive(:perform_later).with(account_with_sla).and_call_original + described_class.perform_now + end + + it 'does not call the ProcessAccountAppliedSlasJob for accounts without SLA' do + expect(Sla::ProcessAccountAppliedSlasJob).not_to receive(:perform_later).with(account_without_sla) described_class.perform_now end end