## Linear issue https://linear.app/chatwoot/issue/CW-6289/limit-the-number-of-notifications-per-user-to-300 ## Description Limits the number of notifications per user to 300 by introducing an async trim job that runs after each notification creation. This prevents unbounded notification growth that was causing DB CPU spikes. ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] This change requires a documentation update ## How Has This Been Tested? - Added unit tests for TrimUserNotificationsJob ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Implements a dedicated purge job to control notification volume and scheduling. > > - Introduces `Notification::RemoveOldNotificationJob` (queue: `purgable`) to delete notifications older than 1 month and trim each user to the 300 most recent (deterministic by `created_at DESC, id DESC`) > - Adds daily cron (`remove_old_notification_job` at 22:30 UTC, queue `purgable`) in `config/schedule.yml` > - Removes ad-hoc triggering of the purge from `TriggerScheduledItemsJob` > - Adds/updates specs covering enqueue queue, old-notification deletion, per-user trimming, and combined behavior > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 9ea2b48e36df96cd15d4119d1dd7dcf5250695de. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Vishnu Narayanan <iamwishnu@gmail.com> Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
45 lines
1.6 KiB
Ruby
45 lines
1.6 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe TriggerScheduledItemsJob do
|
|
subject(:job) { described_class.perform_later }
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
it 'enqueues the job' do
|
|
expect { job }.to have_enqueued_job(described_class)
|
|
.on_queue('scheduled_jobs')
|
|
end
|
|
|
|
it 'triggers Conversations::ReopenSnoozedConversationsJob' do
|
|
expect(Conversations::ReopenSnoozedConversationsJob).to receive(:perform_later).once
|
|
described_class.perform_now
|
|
end
|
|
|
|
it 'triggers Notification::ReopenSnoozedNotificationsJob' do
|
|
expect(Notification::ReopenSnoozedNotificationsJob).to receive(:perform_later).once
|
|
described_class.perform_now
|
|
end
|
|
|
|
it 'triggers Account::ConversationsResolutionSchedulerJob' do
|
|
expect(Account::ConversationsResolutionSchedulerJob).to receive(:perform_later).once
|
|
described_class.perform_now
|
|
end
|
|
|
|
it 'triggers Channels::Whatsapp::TemplatesSyncSchedulerJob' do
|
|
expect(Channels::Whatsapp::TemplatesSyncSchedulerJob).to receive(:perform_later).once
|
|
described_class.perform_now
|
|
end
|
|
|
|
context 'when unexecuted Scheduled campaign jobs' do
|
|
let!(:twilio_sms) { create(:channel_twilio_sms, account: account) }
|
|
let!(:twilio_inbox) { create(:inbox, channel: twilio_sms, account: account) }
|
|
|
|
it 'triggers Campaigns::TriggerOneoffCampaignJob' do
|
|
campaign = create(:campaign, inbox: twilio_inbox, account: account)
|
|
create(:campaign, inbox: twilio_inbox, account: account, scheduled_at: 10.days.after)
|
|
expect(Campaigns::TriggerOneoffCampaignJob).to receive(:perform_later).with(campaign).once
|
|
described_class.perform_now
|
|
end
|
|
end
|
|
end
|