feat: add job to remove stale contacts and contact_inboxes (#11186)
- Add a job to remove stale contacts and contact_inboxes across all accounts Stale anonymous contact is defined as - have no identification (email, phone_number, and identifier are NULL) - have no conversations - are older than 30 days --------- Co-authored-by: Pranav <pranav@chatwoot.com> Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
This commit is contained in:
18
app/jobs/internal/process_stale_contacts_job.rb
Normal file
18
app/jobs/internal/process_stale_contacts_job.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# housekeeping
|
||||
# remove stale contacts for all accounts
|
||||
# - have no identification (email, phone_number, and identifier are NULL)
|
||||
# - have no conversations
|
||||
# - are older than 30 days
|
||||
|
||||
class Internal::ProcessStaleContactsJob < ApplicationJob
|
||||
queue_as :scheduled_jobs
|
||||
|
||||
def perform
|
||||
Account.find_in_batches(batch_size: 100) do |accounts|
|
||||
accounts.each do |account|
|
||||
Rails.logger.info "Enqueuing RemoveStaleContactsJob for account #{account.id}"
|
||||
Internal::RemoveStaleContactsJob.perform_later(account)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
13
app/jobs/internal/remove_stale_contacts_job.rb
Normal file
13
app/jobs/internal/remove_stale_contacts_job.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
# housekeeping
|
||||
# remove contacts that:
|
||||
# - have no identification (email, phone_number, and identifier are NULL)
|
||||
# - have no conversations
|
||||
# - are older than 30 days
|
||||
|
||||
class Internal::RemoveStaleContactsJob < ApplicationJob
|
||||
queue_as :low
|
||||
|
||||
def perform(account, batch_size = 1000)
|
||||
Internal::RemoveStaleContactsService.new(account: account).perform(batch_size)
|
||||
end
|
||||
end
|
||||
@@ -128,6 +128,18 @@ class Contact < ApplicationRecord
|
||||
)
|
||||
}
|
||||
|
||||
# Find contacts that:
|
||||
# 1. Have no identification (email, phone_number, and identifier are NULL or empty string)
|
||||
# 2. Have no conversations
|
||||
# 3. Are older than the specified time period
|
||||
scope :stale_without_conversations, lambda { |time_period|
|
||||
where('contacts.email IS NULL OR contacts.email = ?', '')
|
||||
.where('contacts.phone_number IS NULL OR contacts.phone_number = ?', '')
|
||||
.where('contacts.identifier IS NULL OR contacts.identifier = ?', '')
|
||||
.where('contacts.created_at < ?', time_period)
|
||||
.where.missing(:conversations)
|
||||
}
|
||||
|
||||
def get_source_id(inbox_id)
|
||||
contact_inboxes.find_by!(inbox_id: inbox_id).source_id
|
||||
end
|
||||
|
||||
19
app/services/internal/remove_stale_contacts_service.rb
Normal file
19
app/services/internal/remove_stale_contacts_service.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
class Internal::RemoveStaleContactsService
|
||||
pattr_initialize [:account!]
|
||||
|
||||
def perform(batch_size = 1000)
|
||||
contacts_to_remove = @account.contacts.stale_without_conversations(30.days.ago)
|
||||
total_deleted = 0
|
||||
|
||||
Rails.logger.info "[Internal::RemoveStaleContactsService] Starting removal of stale contacts for account #{@account.id}"
|
||||
|
||||
contacts_to_remove.find_in_batches(batch_size: batch_size) do |batch|
|
||||
contact_ids = batch.map(&:id)
|
||||
|
||||
ContactInbox.where(contact_id: contact_ids).delete_all
|
||||
Contact.where(id: contact_ids).delete_all
|
||||
total_deleted += batch.size
|
||||
Rails.logger.info "[Internal::RemoveStaleContactsService] Deleted #{batch.size} contacts (#{total_deleted} total) for account #{@account.id}"
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user