Restructure omni services and add Chatwoot research snapshot
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
class Internal::CheckNewVersionsJob < ApplicationJob
|
||||
queue_as :scheduled_jobs
|
||||
|
||||
def perform
|
||||
return unless Rails.env.production?
|
||||
|
||||
@instance_info = ChatwootHub.sync_with_hub
|
||||
update_version_info
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_version_info
|
||||
return if @instance_info['version'].blank?
|
||||
|
||||
::Redis::Alfred.set(::Redis::Alfred::LATEST_CHATWOOT_VERSION, @instance_info['version'])
|
||||
end
|
||||
end
|
||||
|
||||
Internal::CheckNewVersionsJob.prepend_mod_with('Internal::CheckNewVersionsJob')
|
||||
27
research/chatwoot/app/jobs/internal/delete_accounts_job.rb
Normal file
27
research/chatwoot/app/jobs/internal/delete_accounts_job.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
class Internal::DeleteAccountsJob < ApplicationJob
|
||||
queue_as :scheduled_jobs
|
||||
|
||||
def perform
|
||||
delete_expired_accounts
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def delete_expired_accounts
|
||||
accounts_pending_deletion.each do |account|
|
||||
AccountDeletionService.new(account: account).perform
|
||||
end
|
||||
end
|
||||
|
||||
def accounts_pending_deletion
|
||||
Account.where("custom_attributes->>'marked_for_deletion_at' IS NOT NULL")
|
||||
.select { |account| deletion_period_expired?(account) }
|
||||
end
|
||||
|
||||
def deletion_period_expired?(account)
|
||||
deletion_time = account.custom_attributes['marked_for_deletion_at']
|
||||
return false if deletion_time.blank?
|
||||
|
||||
DateTime.parse(deletion_time) <= Time.current
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,39 @@
|
||||
# housekeeping
|
||||
# remove stale contacts for subset of accounts each day
|
||||
# - have no identification (email, phone_number, and identifier are NULL)
|
||||
# - have no conversations
|
||||
# - are older than 30 days
|
||||
|
||||
class Internal::ProcessStaleContactsJob < ApplicationJob
|
||||
queue_as :housekeeping
|
||||
|
||||
# Number of day-based groups to split accounts into
|
||||
DISTRIBUTION_GROUPS = 5
|
||||
# Max accounts to process in one batch
|
||||
MAX_ACCOUNTS_PER_BATCH = 20
|
||||
|
||||
# Process only a subset of accounts per day to avoid flooding the queue
|
||||
def perform
|
||||
return unless ChatwootApp.chatwoot_cloud?
|
||||
|
||||
# Use the day of the month to determine which accounts to process
|
||||
day_of_month = Date.current.day
|
||||
remainder = day_of_month % DISTRIBUTION_GROUPS
|
||||
|
||||
# Count total accounts for logging
|
||||
total_accounts = Account.count
|
||||
log_message = "ProcessStaleContactsJob: Processing accounts with ID % #{DISTRIBUTION_GROUPS} = "
|
||||
log_message += "#{remainder} (out of #{total_accounts} total accounts)"
|
||||
Rails.logger.info log_message
|
||||
|
||||
# Process only accounts where ID % 5 = remainder for today
|
||||
# This ensures each account is processed approximately once every 5 days
|
||||
Account.where("id % #{DISTRIBUTION_GROUPS} = ?", remainder).find_each(batch_size: MAX_ACCOUNTS_PER_BATCH) do |account|
|
||||
Rails.logger.info "Enqueuing RemoveStaleContactsJob for account #{account.id}"
|
||||
|
||||
# Add a small delay between jobs to further reduce queue pressure
|
||||
delay = rand(1..10).minutes
|
||||
Internal::RemoveStaleContactsJob.set(wait: delay).perform_later(account)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,12 @@
|
||||
# housekeeping
|
||||
# remove contact inboxes that does not have any conversations
|
||||
# and are older than 3 months
|
||||
|
||||
class Internal::ProcessStaleRedisKeysJob < ApplicationJob
|
||||
queue_as :low
|
||||
|
||||
def perform(account)
|
||||
removed_count = Internal::RemoveStaleRedisKeysService.new(account_id: account.id).perform
|
||||
Rails.logger.info "Successfully cleaned up Redis keys for account #{account.id} (removed #{removed_count} keys)"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,11 @@
|
||||
# housekeeping
|
||||
# remove conversations that do not have a contact_id
|
||||
# orphan conversations without contact cannot be accessed or used
|
||||
|
||||
class Internal::RemoveOrphanConversationsJob < ApplicationJob
|
||||
queue_as :housekeeping
|
||||
|
||||
def perform
|
||||
Internal::RemoveOrphanConversationsService.new.perform
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
@@ -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 :housekeeping
|
||||
|
||||
def perform(account, batch_size = 1000)
|
||||
Internal::RemoveStaleContactsService.new(account: account).perform(batch_size)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
# housekeeping
|
||||
# ensure stale ONLINE PRESENCE KEYS for contacts are removed periodically
|
||||
# should result in 50% redis mem size reduction
|
||||
|
||||
class Internal::RemoveStaleRedisKeysJob < ApplicationJob
|
||||
queue_as :scheduled_jobs
|
||||
|
||||
def perform
|
||||
Account.find_in_batches(batch_size: 100) do |accounts|
|
||||
accounts.each do |account|
|
||||
Rails.logger.info "Enqueuing ProcessStaleRedisKeysJob for account #{account.id}"
|
||||
Internal::ProcessStaleRedisKeysJob.perform_later(account)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
7
research/chatwoot/app/jobs/internal/seed_account_job.rb
Normal file
7
research/chatwoot/app/jobs/internal/seed_account_job.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class Internal::SeedAccountJob < ApplicationJob
|
||||
queue_as :low
|
||||
|
||||
def perform(account)
|
||||
Seeders::AccountSeeder.new(account: account).perform!
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user