Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add job to remove stale contact inboxes (#8096)
- Loading branch information
1 parent
abbb418
commit 677888b
Showing
6 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# housekeeping | ||
# remove contact inboxes that does not have any conversations | ||
# and are older than 3 months | ||
|
||
class Internal::RemoveStaleContactInboxesJob < ApplicationJob | ||
queue_as :scheduled_jobs | ||
|
||
def perform | ||
Internal::RemoveStaleContactInboxesService.new.perform | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
app/services/internal/remove_stale_contact_inboxes_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class Internal::RemoveStaleContactInboxesService | ||
def perform | ||
return unless remove_stale_contact_inbox_job_enabled? | ||
|
||
time_period = 90.days.ago | ||
contact_inboxes_to_delete = stale_contact_inboxes(time_period) | ||
|
||
log_stale_contact_inboxes_deletion(contact_inboxes_to_delete, time_period) | ||
|
||
# Since the number of records to delete is very high, | ||
# delete_all would be faster than destroy_all since it operates at database level | ||
# and avoid loading all the records in memory | ||
# Transaction and batching is used to avoid deadlock and memory issues | ||
ContactInbox.transaction do | ||
contact_inboxes_to_delete | ||
.find_in_batches(batch_size: 10_000) do |group| | ||
ContactInbox.where(id: group.map(&:id)).delete_all | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def remove_stale_contact_inbox_job_enabled? | ||
job_status = ENV.fetch('REMOVE_STALE_CONTACT_INBOX_JOB_STATUS', false) | ||
return false unless ActiveModel::Type::Boolean.new.cast(job_status) | ||
|
||
true | ||
end | ||
|
||
def stale_contact_inboxes(time_period) | ||
ContactInbox.stale_without_conversations(time_period) | ||
end | ||
|
||
def log_stale_contact_inboxes_deletion(contact_inboxes, time_period) | ||
count = contact_inboxes.count | ||
Rails.logger.info "Deleting #{count} stale contact inboxes older than #{time_period}" | ||
|
||
# Log the SQL query without executing it | ||
sql_query = contact_inboxes.to_sql | ||
Rails.logger.info("SQL Query: #{sql_query}") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
spec/services/internal/remove_stale_contact_inboxes_service_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# spec/services/remove_stale_contact_inboxes_service_spec.rb | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Internal::RemoveStaleContactInboxesService do | ||
describe '#perform' do | ||
it 'does not delete stale contact inboxes if REMOVE_STALE_CONTACT_INBOX_JOB_STATUS is false' do | ||
# default value of REMOVE_STALE_CONTACT_INBOX_JOB_STATUS is false | ||
create(:contact_inbox, created_at: 3.days.ago) | ||
create(:contact_inbox, created_at: 91.days.ago) | ||
create(:contact_inbox, created_at: 92.days.ago) | ||
create(:contact_inbox, created_at: 93.days.ago) | ||
create(:contact_inbox, created_at: 94.days.ago) | ||
|
||
service = described_class.new | ||
expect { service.perform }.not_to change(ContactInbox, :count) | ||
end | ||
|
||
it 'deletes stale contact inboxes' do | ||
with_modified_env REMOVE_STALE_CONTACT_INBOX_JOB_STATUS: 'true' do | ||
create(:contact_inbox, created_at: 3.days.ago) | ||
create(:contact_inbox, created_at: 91.days.ago) | ||
create(:contact_inbox, created_at: 92.days.ago) | ||
create(:contact_inbox, created_at: 93.days.ago) | ||
create(:contact_inbox, created_at: 94.days.ago) | ||
|
||
service = described_class.new | ||
expect { service.perform }.to change(ContactInbox, :count).by(-4) | ||
end | ||
end | ||
end | ||
end |